Helpers

HTTP Exceptions

// page not found
abort(404);

Generate an HTTP exception response using status code

Error Handling

public function isValid($value)
{
    try {
        // Validate the value...
    } catch (Throwable $e) {
        report($e);
        return false;
    }
}

Report an exception but continue handling the current request

Named Route URL

$url = route('profile');

See Named Route

URL Generation

Generate arbitrary URLs for your application that will automatically use the scheme (HTTP or HTTPS) and host from the current request

$post = App\Models\Post::find(1);
echo url("/posts/{$post->id}");
// http://example.com/posts/1

#Current URL

// Get the current URL without the query string...
echo url()->current();
// Get the current URL including the query string...
echo url()->full();
// Get the full URL for the previous request...
echo url()->previous();

routes

#Named route

$url = route('profile');

With parameters

// Route::get('/user/{id}/profile', /\*...\*/ )->name('profile);
$url = route('profile', ['id' => 1]);
// /user/1/profile/

With query string

// Route::get('/user/{id}/profile', /\*...\*/ )->name('profile);
$url = route('profile', ['id' => 1, 'photos'=>'yes']);
// /user/1/profile?photos=yes

#Redirects

// Generating Redirects...
return redirect()->route('profile');

#Eloquent Models

echo route('post.show', ['post' => $post]);

The route helper will automatically extract the model's route key. See Routing

Comments