Skip to content

Helpers

Amirul Islam Anirban edited this page Nov 5, 2023 · 7 revisions

Table of Contents

General Helpers:

Get Application instance

app()

Get config data

config('app', 'timezone');

Get env data

env('APP_NAME', 'default');

View the data in details then exit the code

dd([1,2,3]);

View the data in details

dump();

Set page title

setTitle();

Example:

<?php setTitle('login');?>

Get page title, Use it in title tags

getTitle();

Example:

<title><?= getTitle() ?></title>

Get session instance

session();

Get flushMessage instance

flushMessage();

Get auth instance

auth();

Example:

// By default "web" will get the guard.
auth()->user();
// When using multiple guards, you can switch guards.
auth()->guard('editor')->user();

Get bcrypt password

passwordHash('password');

Get event instance

event();

Get app base path

basePath();

Get app url

url();

Form Helpers:

Set new CSRF value

setCsrf();

Example:

<form>
    <?=setCsrf()?>
</form>

Check CSRF is valid or not, return bool

isCsrfValid();

Set form method, like put/patch/delete

setMethod('delete');

Example:

<form>
    <?=setMethod('delete')?>
</form>

Get errors data from flush session by key

errors($key);

Example:

<?=errors($key)?>

Get error data form flush session

error()

Example:

<?=error()?>

Check if the error is set to session, return bool

hasError();

Get success data form flush session

success();

Example:

<?=success()?>

Check if the success is set to session, return bool

hasSuccess();

Request Helpers:

Get request instance

request();

Example:

request()->input();

Response Helpers:

The 'abort' function will display a view contents file with an HTTP error code For example if you pass 404 to the function it will display a view file of 404 and throw the status code 404.

return abort(404, 'optional message');

Redirect link

return redirect('/redirect-link');

Create a new redirect response to the previous location

return back();

Finds route by route name and redirect this route

return toRoute('users');

Get the evaluated view contents for the given view

return view('welcome');

Set the response status code with the view content

status();

Example:

return status(200)->view('welcome');

Set the view layout

layout();

Example:

return layout('app')->view('welcome');