Laravel 5.4
Awesome Laravel
- Awesome Laravel (Chirag Gude)
Prologue
- Release Notes
- Upgrade Guide
Getting Started
- Installation
- Configuration
- Directory Structure
- Laravel Homestead
- valet
Architecture Concepts
- Request Lifecycle
- Service Container
- Service Providers
- Facades
The Basics
- Routing
- Errors & Logging
- Middleware
- CSRF Protection
- Controllers
- HTTP Requests
- HTTP Responses
- Views
- HTTP Session
- Validation
Frontend
- Blade Templates
- Localization
- JavaScript & CSS Scaffolding
- Compiling Assets (Laravel Mix)
Security
- Authentication
- API Authentication (Passport)
- Authorization
- Encryption
- Hashing
- Resetting Passwords
Digging Deeper
- Artisan Console
- Queues
- Package Development
- Task Scheduling
- Broadcasting
- Cache
- Collections
- Events
- File Storage
- helpers
- Notifications
Database
- Database Getting Started
- Database Query Builder
- Database Pagination
- Database Migrations
- Database Seeding
- Redis
Eloquent ORM
- Eloquent Getting Started
- Eloquent Relationships
- Eloquent Collections
- Eloquent Mutators
- Eloquent Serialization
Testing
- Testing Getting Started
- HTTP Tests
- Browser Tests (Laravel Dusk)
- Database Testing
- Mocking
- redirect
Official Packages
- Laravel Cashier
- Envoy Task Runner
- Laravel Scout
helpers
Introduction
Laravel includes a variety of global “helper” PHP functions. Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.
Available Methods
|
Arrays
array_add
array_collapse
array_divide
array_dot
array_except
array_first
array_flatten
array_forget
array_get
array_has
array_last
array_only
array_pluck
array_prepend
array_pull
array_set
array_sort
array_sort_recursive
array_where
array_wrap
head
last
Paths
Strings
camel_case
class_basename
e
ends_with
kebab_case
snake_case
str_limit
starts_with
str_contains
str_finish
str_is
str_plural
str_random
str_singular
str_slug
studly_case
title_case
trans
trans_choice
URLs
Miscellaneous
Method Listing
|
Arrays
array_add()
The array_add
function adds a given key / value pair to the array if the given key doesn’t already exist in the array:
array_collapse()
The array_collapse
function collapses an array of arrays into a single array:
array_divide()
The array_divide
function returns two arrays, one containing the keys, and the other containing the values of the original array:
array_dot()
The array_dot
function flattens a multi-dimensional array into a single level array that uses “dot” notation to indicate depth:
The array_except
function removes the given key / value pairs from the array:
array_first()
The array_first
function returns the first element of an array passing a given truth test:
A default value may also be passed as the third parameter to the method. This value will be returned if no value passes the truth test:
array_flatten()
The array_flatten
function will flatten a multi-dimensional array into a single level.
array_forget()
The array_forget
function removes a given key / value pair from a deeply nested array using “dot” notation:
array_get()
The array_get
function retrieves a value from a deeply nested array using “dot” notation:
The array_get
function also accepts a default value, which will be returned if the specific key is not found:
array_has()
The array_has
function checks that a given item or items exists in an array using “dot” notation:
array_last()
The array_last
function returns the last element of an array passing a given truth test:
array_only()
The array_only
function will return only the specified key / value pairs from the given array:
array_pluck()
The array_pluck
function will pluck a list of the given key / value pairs from the array:
You may also specify how you wish the resulting list to be keyed:
array_prepend()
The array_prepend
function will push an item onto the beginning of an array:
array_pull()
The array_pull
function returns and removes a key / value pair from the array:
array_set()
The array_set
function sets a value within a deeply nested array using “dot” notation:
array_sort()
The array_sort
function sorts the array by the results of the given Closure:
|
array_sort_recursive()
The array_sort_recursive
function recursively sorts the array using the sort
function:
|
array_where()
The array_where
function filters the array using the given Closure:
|
array_wrap()
The array_wrap
function will wrap the given value in an array. If the given value is already an array it will not be changed:
head()
The head
function returns the first element in the given array:
last()
The last
function returns the last element in the given array:
Paths
app_path()
The app_path
function returns the fully qualified path to the app
directory. You may also use the app_path
function to generate a fully qualified path to a file relative to the application directory:
base_path()
The base_path
function returns the fully qualified path to the project root. You may also use the base_path
function to generate a fully qualified path to a given file relative to the project root directory:
config_path()
The config_path
function returns the fully qualified path to the application configuration directory:
database_path()
The database_path
function returns the fully qualified path to the application’s database directory:
mix()
The mix
function gets the path to a versioned Mix file:
public_path()
The public_path
function returns the fully qualified path to the public
directory:
resource_path()
The resource_path
function returns the fully qualified path to the resources
directory. You may also use the resource_path
function to generate a fully qualified path to a given file relative to the resources directory:
storage_path()
The storage_path
function returns the fully qualified path to the storage
directory. You may also use the storage_path
function to generate a fully qualified path to a given file relative to the storage directory:
Strings
camel_case()
The camel_case
function converts the given string to camelCase
:
class_basename()
The class_basename
returns the class name of the given class with the class’ namespace removed:
e()
The e
function runs PHP’s htmlspecialchars
function with the double_encode
option set to false
:
ends_with()
The ends_with
function determines if the given string ends with the given value:
kebab_case()
The kebab_case
function converts the given string to kebab-case
:
snake_case()
The snake_case
function converts the given string to snake_case
:
str_limit()
The str_limit
function limits the number of characters in a string. The function accepts a string as its first argument and the maximum number of resulting characters as its second argument:
starts_with()
The starts_with
function determines if the given string begins with the given value:
str_contains()
The str_contains
function determines if the given string contains the given value:
You may also pass an array of values to determine if the given string contains any of the values:
str_finish()
The str_finish
function adds a single instance of the given value to a string:
str_is()
The str_is
function determines if a given string matches a given pattern. Asterisks may be used to indicate wildcards:
str_plural()
The str_plural
function converts a string to its plural form. This function currently only supports the English language:
str_random()
The str_random
function generates a random string of the specified length. This function uses PHP’s random_bytes
function:
str_singular()
The str_singular
function converts a string to its singular form. This function currently only supports the English language:
str_slug()
The str_slug
function generates a URL friendly “slug” from the given string:
studly_case()
The studly_case
function converts the given string to StudlyCase
:
title_case()
The title_case
function converts the given string to Title Case
:
trans()
The trans
function translates the given language line using your localization files:
trans_choice()
The trans_choice
function translates the given language line with inflection:
URLs
action()
The action
function generates a URL for the given controller action. You do not need to pass the full namespace to the controller. Instead, pass the controller class name relative to the App\Http\Controllers
namespace:
If the method accepts route parameters, you may pass them as the second argument to the method:
asset()
Generate a URL for an asset using the current scheme of the request (HTTP or HTTPS):
secure_asset()
Generate a URL for an asset using HTTPS:
route()
The route
function generates a URL for the given named route:
If the route accepts parameters, you may pass them as the second argument to the method:
secure_url()
The secure_url
function generates a fully qualified HTTPS URL to the given path:
url()
The url
function generates a fully qualified URL to the given path:
If no path is provided, a Illuminate\Routing\UrlGenerator
instance is returned:
Miscellaneous
abort()
The abort
function throws a HTTP exception which will be rendered by the exception handler:
You may also provide the exception’s response text:
abort_if()
The abort_if
function throws an HTTP exception if a given boolean expression evaluates to true
:
abort_unless()
The abort_unless
function throws an HTTP exception if a given boolean expression evaluates to false
:
auth()
The auth
function returns an authenticator instance. You may use it instead of the Auth
facade for convenience:
back()
The back()
function generates a redirect response to the user’s previous location:
bcrypt()
The bcrypt
function hashes the given value using Bcrypt. You may use it as an alternative to the Hash
facade:
cache()
The cache
function may be used to get values from the cache. If the given key does not exist in the cache, an optional default value will be returned:
You may add items to the cache by passing an array of key / value pairs to the function. You should also pass the number of minutes or duration the cached value should be considered valid:
collect()
The collect
function creates a collection instance from the given array:
config()
The config
function gets the value of a configuration variable. The configuration values may be accessed using “dot” syntax, which includes the name of the file and the option you wish to access. A default value may be specified and is returned if the configuration option does not exist:
The config
helper may also be used to set configuration variables at runtime by passing an array of key / value pairs:
csrf_field()
The csrf_field
function generates an HTML hidden
input field containing the value of the CSRF token. For example, using Blade syntax:
csrf_token()
The csrf_token
function retrieves the value of the current CSRF token:
dd()
The dd
function dumps the given variables and ends execution of the script:
If you do not want to halt the execution of your script, use the dump
function instead:
dispatch()
The dispatch
function pushes a new job onto the Laravel job queue:
env()
The env
function gets the value of an environment variable or returns a default value:
event()
The event
function dispatches the given event to its listeners:
factory()
The factory
function creates a model factory builder for a given class, name, and amount. It can be used while testing or seeding:
info()
The info
function will write information to the log:
An array of contextual data may also be passed to the function:
logger()
The logger
function can be used to write a debug
level message to the log:
An array of contextual data may also be passed to the function:
A logger instance will be returned if no value is passed to the function:
method_field()
The method_field
function generates an HTML hidden
input field containing the spoofed value of the form’s HTTP verb. For example, using Blade syntax:
old()
The old
function retrieves an old input value flashed into the session:
redirect()
The redirect
function returns a redirect HTTP response, or returns the redirector instance if called with no arguments:
request()
The request
function returns the current request instance or obtains an input item:
response()
The response
function creates a response instance or obtains an instance of the response factory:
retry()
The retry
function attempts to execute the given callback until the given maximum attempt threshold is met. If the callback does not throw an exception, it’s return value will be returned. If the callback throws an exception, it will automatically be retried. If the maximum attempt count is exceeded, the exception will be thrown:
session()
The session
function may be used to get or set session values:
You may set values by passing an array of key / value pairs to the function:
The session store will be returned if no value is passed to the function:
value()
The value
function’s behavior will simply return the value it is given. However, if you pass a Closure
to the function, the Closure
will be executed then its result will be returned:
view()
The view
function retrieves a view instance: