Laravel Helper Functions Cheat Sheet
This cheat sheet presents Laravel's Helper utilities, covering array and object methods, path helpers, string functions, URL generators, and miscellaneous helpers, each illustrated with concise code examples to streamline backend development in PHP.
Helper is a Laravel utility class that offers a collection of powerful functions to accelerate backend development, reducing repetitive code and improving productivity.
Array & Object
Arr::add(['name' => 'Desk'], 'price', 100); // => ['name' => 'Desk', 'price' => 100]
Arr::collapse([[1, 2, 3], [4, 5, 6]]); // => [1, 2, 3, 4, 5, 6]
Arr::divide(['key1' => 'val1', 'key2' => 'val2']); // => [['key1','key2'],['val1','val2']]
Arr::dot($array);
Arr::except($array, ['key']);
Arr::first($array, function ($key, $value) {}, $default);
Arr::flatten(['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]);
Arr::forget($array, 'foo');
Arr::forget($array, 'foo.bar');
Arr::get($array, 'foo', 'default');
Arr::has($array, 'products.desk');
Arr::only($array, ['key']);
Arr::pluck($array, 'key');
Arr::pull($array, 'key');
Arr::set($array, 'key', 'value');
Arr::set($array, 'key.subkey', 'value');
Arr::sort($array, function () {});
Arr::sortRecursive();
Arr::where();
Arr::shuffle($array, 'I-AM-GROOT');
Arr::wrap($array);
head($array);
last($array);Path Helpers
app_path(); // full path to the app directory
base_path(); // full path to the project root
config_path(); // full path to the config directory
database_path(); // full path to the database directory
elixir(); // path to versioned Elixir file
public_path(); // full path to the public directory
storage_path(); // full path to the storage directoryString Helpers
Str::camel($value); // convert to camelCase
class_basename($class); // class name without namespace
e('
'); // htmlentities
Str::startsWith('Foo bar.', 'Foo');
Str::endsWith('Foo bar.', 'bar');
Str::snake('fooBar'); // => foo_bar
Str::studly('foo_bar'); // => FooBar
trans('foo.bar'); // translate using localization files
trans_choice('foo.bar', $count); // pluralizationURLs and Links
action('FooController@method', $parameters);
asset('img/photo.jpg', $title, $attributes); // HTTP/HTTPS aware
secure_asset('img/photo.jpg', $title, $attributes);
route($route, $parameters, $absolute = true);
url('path', $parameters = [], $secure = null);Other Helpers
auth()->user(); // get authenticated user
back(); // redirect to previous location
bcrypt('my-secret-password'); // hash a password
collect(['taylor', 'abigail']); // create a collection
config('app.timezone', $default); // get config value
{!! csrf_field() !!} // CSRF hidden input
@csrf // Blade directive for CSRF
$token = csrf_token(); // get CSRF token
dd($value); // dump and die
dump($value); // dump variable
$env = env('APP_ENV'); // get environment variable
event(new UserRegistered($user)); // fire event
$user = factory(App\User::class)->make(); // model factory
{!! method_field('delete') !!} // spoof HTTP verb
@method('delete')
$value = old('value'); // retrieve old input
return redirect('/home'); // redirect response
$value = request('key', $default = null); // get request input
return response('Hello World', 200, $headers); // create response
$value = session('key'); // get session value
session()->put('key', $value); // store in session
value(function () { return 'bar'; }); // resolve value
return view('auth.login'); // render view
$value = with(new Foo)->work();By using these helpers, developers can write cleaner, more expressive code and focus on business logic rather than low‑level implementation details.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.