Skip to content

Helpers and Utilities

Helper Purpose
env($key, $default = null) read an environment variable (true, false, null, empty are converted)
config($key = null, $default = null) read configuration with dot notation
app($abstract = null) the application, or resolve a class from the container
base_path($path = '') absolute path inside the project
storage_path($path = '') absolute path inside storage/
e($value) HTML-escape (UTF-8)
response($body = '', $status = 200, $headers = []) an HTML response
json($data, $status = 200, $headers = []) a JSON response
redirect($to, $status = 302) a redirect response
view($name, $data = [], $status = 200) render a template into a response
route($name, $params = [], $absolute = false) generate a URL for a named route
csrf_token() / csrf_field() the session token / a hidden input
method_field($method) hidden _method input
json_for_html($value) JSON that is safe inside HTML and <script>
now() the current time as a DateTimeImmutable (uses the PSR-20 clock and app.timezone)
logger($message = null, $context = []) the logger, or log an info message
storage($disk = null) a storage disk
dispatch($job) dispatch a queued job
http() the HTTP client
nosql($connection = null) a document store
collect($items = []) a Collection

snake(), studly(), camel(), slug($value, $separator = '-'), plural(), singular(), random($length = 32), classBasename().

Str::slug('Hello World'); // hello-world
Str::studly('user_profile'); // UserProfile

Dot-notation helpers for arrays: get(), has(), set(), forget(), only(), except(), isList().

An array wrapper returned by queries. It is iterable, countable, array-accessible and JSON-serializable.

Group Methods
Transform map, filter, each, values, keys, pluck, keyBy, groupBy, sortBy, unique, merge, push
Read all, toArray, first, last, contains, isEmpty, isNotEmpty, count, sum
Post::all()->pluck('title');
Post::all()->groupBy('user_id');
collect([3, 1, 2])->sortBy(fn ($n) => $n)->values()->all();

The .env loader behind env(). Env::get(), Env::set() (explicit values win), Env::load() and Env::parse().

A small data generator for factories (names, emails, words, sentences, UUIDs, dates, numbers). Fake::seed(42) makes the output reproducible. See Factories and seeders.

SystemClock and FrozenClock implement PSR-20, so time-dependent code is easy to test.