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.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Laravel Helper Functions Cheat Sheet

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]</code>
<code>Arr::collapse([[1, 2, 3], [4, 5, 6]]); // => [1, 2, 3, 4, 5, 6]</code>
<code>Arr::divide(['key1' => 'val1', 'key2' => 'val2']); // => [['key1','key2'],['val1','val2']]</code>
<code>Arr::dot($array);</code>
<code>Arr::except($array, ['key']);</code>
<code>Arr::first($array, function ($key, $value) {}, $default);</code>
<code>Arr::flatten(['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]);</code>
<code>Arr::forget($array, 'foo');</code>
<code>Arr::forget($array, 'foo.bar');</code>
<code>Arr::get($array, 'foo', 'default');</code>
<code>Arr::has($array, 'products.desk');</code>
<code>Arr::only($array, ['key']);</code>
<code>Arr::pluck($array, 'key');</code>
<code>Arr::pull($array, 'key');</code>
<code>Arr::set($array, 'key', 'value');</code>
<code>Arr::set($array, 'key.subkey', 'value');</code>
<code>Arr::sort($array, function () {});</code>
<code>Arr::sortRecursive();</code>
<code>Arr::where();</code>
<code>Arr::shuffle($array, 'I-AM-GROOT');</code>
<code>Arr::wrap($array);</code>
<code>head($array);</code>
<code>last($array);

Path Helpers

app_path(); // full path to the app directory</code>
<code>base_path(); // full path to the project root</code>
<code>config_path(); // full path to the config directory</code>
<code>database_path(); // full path to the database directory</code>
<code>elixir(); // path to versioned Elixir file</code>
<code>public_path(); // full path to the public directory</code>
<code>storage_path(); // full path to the storage directory

String Helpers

Str::camel($value); // convert to camelCase</code>
<code>class_basename($class); // class name without namespace</code>
<code>e('<html>'); // htmlentities</code>
<code>Str::startsWith('Foo bar.', 'Foo');</code>
<code>Str::endsWith('Foo bar.', 'bar');</code>
<code>Str::snake('fooBar'); // => foo_bar</code>
<code>Str::studly('foo_bar'); // => FooBar</code>
<code>trans('foo.bar'); // translate using localization files</code>
<code>trans_choice('foo.bar', $count); // pluralization

URLs and Links

action('FooController@method', $parameters);</code>
<code>asset('img/photo.jpg', $title, $attributes); // HTTP/HTTPS aware</code>
<code>secure_asset('img/photo.jpg', $title, $attributes);</code>
<code>route($route, $parameters, $absolute = true);</code>
<code>url('path', $parameters = [], $secure = null);

Other Helpers

auth()->user(); // get authenticated user</code>
<code>back(); // redirect to previous location</code>
<code>bcrypt('my-secret-password'); // hash a password</code>
<code>collect(['taylor', 'abigail']); // create a collection</code>
<code>config('app.timezone', $default); // get config value</code>
<code>{!! csrf_field() !!} // CSRF hidden input</code>
<code>@csrf // Blade directive for CSRF</code>
<code>$token = csrf_token(); // get CSRF token</code>
<code>dd($value); // dump and die</code>
<code>dump($value); // dump variable</code>
<code>$env = env('APP_ENV'); // get environment variable</code>
<code>event(new UserRegistered($user)); // fire event</code>
<code>$user = factory(App\User::class)->make(); // model factory</code>
<code>{!! method_field('delete') !!} // spoof HTTP verb</code>
<code>@method('delete')</code>
<code>$value = old('value'); // retrieve old input</code>
<code>return redirect('/home'); // redirect response</code>
<code>$value = request('key', $default = null); // get request input</code>
<code>return response('Hello World', 200, $headers); // create response</code>
<code>$value = session('key'); // get session value</code>
<code>session()->put('key', $value); // store in session</code>
<code>value(function () { return 'bar'; }); // resolve value</code>
<code>return view('auth.login'); // render view</code>
<code>$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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPURLLaravelHelper
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.