auxiliary function
brief introduction
Method List
Arrays&Objects
use Illuminate\Support\Arr; $array = Arr::add(['name' => 'Desk'], 'price', 100); // ['name' => 'Desk', 'price' => 100] $array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100); // ['name' => 'Desk', 'price' => 100]
use Illuminate\Support\Arr; $array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
use Illuminate\Support\Arr; [$keys, $values] = Arr::divide(['name' => 'Desk']); // $keys: ['name'] // $values: ['Desk']
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $flattened = Arr::dot($array); // ['products.desk.price' => 100]
use Illuminate\Support\Arr; $array = ['name' => 'Desk', 'price' => 100]; $filtered = Arr::except($array, ['price']); // ['name' => 'Desk']
use Illuminate\Support\Arr; $array = [100, 200, 300]; $first = Arr::first($array, function ($value, $key) { return $value >= 150; }); // 200
use Illuminate\Support\Arr; $first = Arr::first($array, $callback, $default);
use Illuminate\Support\Arr; $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; $flattened = Arr::flatten($array); // ['Joe', 'PHP', 'Ruby']
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, 'products.desk'); // ['products' => []]
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $price = Arr::get($array, 'products.desk.price'); // 100
use Illuminate\Support\Arr; $discount = Arr::get($array, 'products.desk.discount', 0); // 0
use Illuminate\Support\Arr; $array = ['product' => ['name' => 'Desk', 'price' => 100]]; $contains = Arr::has($array, 'product.name'); // true $contains = Arr::has($array, ['product.price', 'product.discount']); // false
use Illuminate\Support\Arr; $array = [100, 200, 300, 110]; $last = Arr::last($array, function ($value, $key) { return $value >= 150; }); // 300
use Illuminate\Support\Arr; $last = Arr::last($array, $callback, $default);
use Illuminate\Support\Arr; $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $slice = Arr::only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100]
use Illuminate\Support\Arr; $array = [ ['developer' => ['id' => 1, 'name' => 'Taylor']], ['developer' => ['id' => 2, 'name' => 'Abigail']], ]; $names = Arr::pluck($array, 'developer.name'); // ['Taylor', 'Abigail']
use Illuminate\Support\Arr; $names = Arr::pluck($array, 'developer.name', 'developer.id'); // [1 => 'Taylor', 2 => 'Abigail']
use Illuminate\Support\Arr; $array = ['one', 'two', 'three', 'four']; $array = Arr::prepend($array, 'zero'); // ['zero', 'one', 'two', 'three', 'four']
use Illuminate\Support\Arr; $array = ['price' => 100]; $array = Arr::prepend($array, 'Desk', 'name'); // ['name' => 'Desk', 'price' => 100]
use Illuminate\Support\Arr; $array = ['name' => 'Desk', 'price' => 100]; $name = Arr::pull($array, 'name'); // $name: Desk // $array: ['price' => 100]
use Illuminate\Support\Arr; $value = Arr::pull($array, $key, $default);
use Illuminate\Support\Arr; $array = [1, 2, 3, 4, 5]; $random = Arr::random($array); // 4 - (retrieved randomly)
use Illuminate\Support\Arr; $items = Arr::random($array, 2); // [2, 5] - (retrieved randomly)
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; Arr::set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
use Illuminate\Support\Arr; $array = ['Desk', 'Table', 'Chair']; $sorted = Arr::sort($array); // ['Chair', 'Desk', 'Table']
use Illuminate\Support\Arr; $array = [ ['name' => 'Desk'], ['name' => 'Table'], ['name' => 'Chair'], ]; $sorted = array_values(Arr::sort($array, function ($value) { return $value['name']; })); /* [ ['name' => 'Chair'], ['name' => 'Desk'], ['name' => 'Table'], ] */
use Illuminate\Support\Arr; $array = [ ['Roman', 'Taylor', 'Li'], ['PHP', 'Ruby', 'JavaScript'], ['one' => 1, 'two' => 2, 'three' => 3], ]; $sorted = Arr::sortRecursive($array); /* [ ['JavaScript', 'PHP', 'Ruby'], ['one' => 1, 'three' => 3, 'two' => 2], ['Li', 'Roman', 'Taylor'], ] */
use Illuminate\Support\Arr; $array = [100, '200', 300, '400', 500]; $filtered = Arr::where($array, function ($value, $key) { return is_string($value); }); // [1 => '200', 3 => '400']
use Illuminate\Support\Arr; $string = 'Laravel'; $array = Arr::wrap($string); // ['Laravel']
use Illuminate\Support\Arr; $nothing = null; $array = Arr::wrap($nothing); // []
$data = ['products' => ['desk' => ['price' => 100]]]; data_fill($data, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 100]]] data_fill($data, 'products.desk.discount', 10); // ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]
$data = [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2'], ], ]; data_fill($data, 'products.*.price', 200); /* [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2', 'price' => 200], ], ] */
$data = ['products' => ['desk' => ['price' => 100]]]; $price = data_get($data, 'products.desk.price'); // 100
$discount = data_get($data, 'products.desk.discount', 0); // 0
$data = ['products' => ['desk' => ['price' => 100]]]; data_set($data, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
$data = [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2', 'price' => 150], ], ]; data_set($data, 'products.*.price', 200); /* [ 'products' => [ ['name' => 'Desk 1', 'price' => 200], ['name' => 'Desk 2', 'price' => 200], ], ] */
$data = ['products' => ['desk' => ['price' => 100]]]; data_set($data, 'products.desk.price', 200, false); // ['products' => ['desk' => ['price' => 100]]]
$array = [100, 200, 300]; $first = head($array); // 100
$array = [100, 200, 300]; $last = last($array); // 300
Path function
$path = app_path(); $path = app_path('Http/Controllers/Controller.php');
$path = base_path(); $path = base_path('vendor/bin');
$path = config_path(); $path = config_path('app.php');
$path = database_path(); $path = database_path('factories/UserFactory.php');
$path = mix('css/app.css');
$path = public_path(); $path = public_path('css/app.css');
$path = resource_path(); $path = resource_path('sass/app.scss');
$path = storage_path(); $path = storage_path('app/file.txt');
String function
echo __('Welcome to our application'); echo __('messages.welcome');
$class = class_basename('Foo\Bar\Baz'); // Baz
echo e('<html>foo</html>'); // <html>foo</html>
$string = 'The event will take place between :start and :end'; $replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string); // The event will take place between 8:30 and 9:00
use Illuminate\Support\Str; $slice = Str::after('This is my name', 'This is'); // ' my name'
use Illuminate\Support\Str; $slice = Str::before('This is my name', 'my name'); // 'This is '
use Illuminate\Support\Str; $converted = Str::camel('foo_bar'); // fooBar
use Illuminate\Support\Str; $contains = Str::contains('This is my name', 'my'); // true
use Illuminate\Support\Str; $contains = Str::contains('This is my name', ['my', 'foo']); // true
use Illuminate\Support\Str; $result = Str::endsWith('This is my name', 'name'); // true
use Illuminate\Support\Str; $adjusted = Str::finish('this/string', '/'); // this/string/ $adjusted = Str::finish('this/string/', '/'); // this/string/
use Illuminate\Support\Str; $matches = Str::is('foo*', 'foobar'); // true $matches = Str::is('baz*', 'foobar'); // false
use Illuminate\Support\Str; $converted = Str::kebab('fooBar'); // foo-bar
use Illuminate\Support\Str; $truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20); // The quick brown fox...
use Illuminate\Support\Str; $truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)'); // The quick brown fox (...)
use Illuminate\Support\Str; return (string) Str::orderedUuid();
use Illuminate\Support\Str; $plural = Str::plural('car'); // cars $plural = Str::plural('child'); // children
use Illuminate\Support\Str; $plural = Str::plural('child', 2); // children $plural = Str::plural('child', 1); // child
use Illuminate\Support\Str; $random = Str::random(40);
use Illuminate\Support\Str; $string = 'The event will take place between ? and ?'; $replaced = Str::replaceArray('?', ['8:30', '9:00'], $string); // The event will take place between 8:30 and 9:00
use Illuminate\Support\Str; $replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog'); // a quick brown fox jumps over the lazy dog
use Illuminate\Support\Str; $replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog'); // the quick brown fox jumps over a lazy dog
use Illuminate\Support\Str; $singular = Str::singular('cars'); // car $singular = Str::singular('children'); // child
use Illuminate\Support\Str; $slug = Str::slug('Laravel 5 Framework', '-'); // laravel-5-framework
use Illuminate\Support\Str; $converted = Str::snake('fooBar'); // foo_bar
use Illuminate\Support\Str; $adjusted = Str::start('this/string', '/'); // /this/string $adjusted = Str::start('/this/string', '/'); // /this/string
use Illuminate\Support\Str; $result = Str::startsWith('This is my name', 'This'); // true
use Illuminate\Support\Str; $converted = Str::studly('foo_bar'); // FooBar
use Illuminate\Support\Str; $converted = Str::title('a nice title uses the correct case'); // A Nice Title Uses The Correct Case
use Illuminate\Support\Str; return (string) Str::uuid();
echo trans('messages.welcome');
echo trans_choice('messages.notifications', $unreadCount);
URL function
$url = action(' HomeController@index '); $url = action([HomeController::class, 'index']);
$url = action(' UserController@profile ', ['id' => 1]);
$url = asset('img/photo.jpg');
// ASSET_URL= http://example.com/assets $url = asset('img/photo.jpg'); // http://example.com/assets/img/photo.jpg
$url = secure_asset('img/photo.jpg');
$url = route('routeName');
$url = route('routeName', ['id' => 1]);
$url = route('routeName', ['id' => 1], false);
echo secure_url('user/profile'); echo secure_url('user/profile', [1]);
echo url('user/profile'); echo url('user/profile', [1]);
echo url()->current(); echo url()->full(); echo url()->previous();
Other functions
abort(403);
abort(403, 'Unauthorized.', $ headers);
abort_if(! Auth::user()->isAdmin(), 403);
abort_unless(Auth::user()->isAdmin(), 403);
$container = app();
$api = app('HelpSpot\API');
$user = auth()->user();
$user = auth('admin')->user();
return back($status = 302, $headers = [], $fallback = false); return back();
$password = bcrypt('my-secret-password');
blank(''); blank(' '); blank(null); blank(collect()); // true blank(0); blank(true); blank(false); // false
broadcast(new UserRegistered($user));
$value = cache('key'); $value = cache('key', 'default');
cache(['key' => 'value'], 5); cache(['key' => 'value'], now()->addSeconds(10));
$traits = class_uses_recursive(App\User::class);
$collection = collect(['taylor', 'abigail']);
$value = config('app.timezone'); $value = config('app.timezone', $default);
config(['app.debug' => true]);
$cookie = cookie('name', 'value', $minutes);
{{ csrf_field() }}
$token = csrf_token();
dd($value); dd($value1, $value2, $value3, ...);
$decrypted = decrypt($encrypted_value);
dispatch(new App\Jobs\SendEmails);
$result = dispatch_now(new App\Jobs\SendEmails);
dump($value); dump($value1, $value2, $value3, ...);
Note: You can use Artisan's
dump-server Command to block all
dump Call and display them in the console window.
$encrypted = encrypt($unencrypted_value);
$env = env('APP_ENV'); //If the variable does not exist, return the default value $env = env('APP_ENV', 'production');
Note: If you execute
config:cache Command, you need to ensure that only the
env , once the configuration is cached,
.env The file will not be loaded, so all pairs of
env All function calls will return
null 。
event(new UserRegistered($user));
$user = factory(App\User::class)->make();
filled(0); filled(true); filled(false); // true filled(''); filled(' '); filled(null); filled(collect()); // false
info('Some helpful information!');
info('User login attempt failed.', ['id' => $user->id]);
logger('Debug message');
logger('User has logged in.', ['id' => $user->id]);
logger()->error('You are not allowed here.');
<form method="POST"> {{ method_field('DELETE') }} </form>
$now = now();
$value = old('value'); $value = old('value', 'default');
return optional($user->address)->street; {!! old('name', optional($user)->name) !!}
return optional(User::find($id), function ($user) { return new DummyUser; });
$policy = policy(App\User::class);
return redirect($to = null, $status = 302, $headers = [], $secure = null); return redirect('/home'); return redirect()->route('route.name');
report($e);
$request = request(); $value = request('key', $default);
return rescue(function () { return $this->method(); });
return rescue(function () { return $this->method(); }, false); return rescue(function () { return $this->method(); }, function () { return $this->failure(); });
$api = resolve('HelpSpot\API');
return response('Hello World', 200, $headers); return response()->json(['foo' => 'bar'], 200, $headers);
return retry(5, function () { // Attempt 5 times while resting 100ms in between attempts... }, 100);
$value = session('key');
session(['chairs' => 7, 'instruments' => 3]);
$value = session()->get('key'); session()->put('key', $value);
$user = tap(User::first(), function ($user) { $user->name = 'taylor'; $user->save(); });
$user = tap($user)->update([ 'name' => $name, 'email' => $email, ]);
throw_if(! Auth::user()->isAdmin(), AuthorizationException::class); throw_if( ! Auth::user()->isAdmin(), AuthorizationException::class, 'You are not allowed to access this page' );
throw_unless(Auth::user()->isAdmin(), AuthorizationException::class); throw_unless( Auth::user()->isAdmin(), AuthorizationException::class, 'You are not allowed to access this page' );
$today = today();
$traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class);
$callback = function ($value) { return $value * 2; }; $result = transform(5, $callback); // 10
$result = transform(null, $callback, 'The value is blank'); // The value is blank
$validator = validator($data, $rules, $messages);
$result = value(true); // true $result = value(function () { return false; }); // false
return view('auth.login');
$callback = function ($value) { return (is_numeric($value)) ? $ value * 2 : 0; }; $result = with(5, $callback); // 10 $result = with(null, $callback); // 0 $result = with(5, null); // 5