Laravel Cheatsheet
This is a highly personal & opinionated list of useful commands and codesnippets.
Auth
Auth::check(); // Check if the user is logged in
Auth::user();
Auth::attempt(array('email' => $email, 'password' => $password));
Auth::attempt($credentials, true); // Remember user login
Auth::once($credentials); // Log in for a single request
Auth::login(User::find(1));
Auth::loginUsingId(1);
Auth::logout();
Auth::validate($credentials);
Auth::basic('username');
Auth::onceBasic();
Password::remind($credentials, function($message, $user){});
Strings
Str::ascii($value) // Transliterate a UTF-8 value to ASCII
Str::camel($value)
Str::contains($haystack, $needle)
Str::endsWith($haystack, $needles)
Str::finish($value, $cap) // Cap a string with a single instance of a given value.
Str::is($pattern, $value)
Str::length($value)
Str::limit($value, $limit = 100, $end = '...')
Str::lower($value)
Str::words($value, $words = 100, $end = '...')
Str::plural($value, $count = 2)
Str::quickRandom($length = 16) // Generate a "random" alpha-numeric string.
Str::random($length = 16) // Generate a more truly "random" alpha-numeric string.
Str::upper($value)
Str::title($value)
Str::singular($value)
Str::slug($title, $separator = '-')
Str::snake($value, $delimiter = '_')
Str::startsWith($haystack, $needles)
Str::studly($value) // Convert a value to studly caps case.
Str::macro($name, $macro)
URLs
URL::full();
URL::current();
URL::previous();
URL::to('foo/bar', $parameters, $secure);
URL::action('FooController@method', $parameters, $absolute);
URL::route('foo', $parameters, $absolute);
URL::secure('foo/bar', $parameters);
URL::asset('css/foo.css', $secure);
URL::secureAsset('css/foo.css');
URL::isValidUrl('http://example.com');
URL::getRequest();
URL::setRequest($request);
URL::getGenerator();
URL::setGenerator($generator);
Localization
/*
* Handy currentLocale and isLocale methods on the App facade to determine the current locale or check if the locale is a given value
*/
use Illuminate\Support\Facades\App;
$locale = App::currentLocale();
if (App::isLocale('en')) {
// ...
}
Artisan
# Put the app into maintenance / demo mode
php artisan down [--redirect [REDIRECT]] [--render [RENDER]] [--retry [RETRY]] [--refresh [REFRESH]] [-# -secret [SECRET]] [--with-secret] [--status [STATUS]]
# Options
# redirect - The path that users should be redirected to. Optional
# refresh - The number of seconds after which the browser may refresh. Optional
# render - The view that should be prerendered for display during maintenance mode. Optional
# retry - The number of seconds after which the request may be retried. Optional
# secret - The secret phrase that may be used to bypass maintenance mode. Optional
# status - The status code that should be used when returning the maintenance mode response. Optional
# with-secret - Generate a random secret phrase that may be used to bypass maintenance mode. Optional
# Bring the application out of maintenance mode
php artisan up
# Run the database migrations
php artisan migrate [--database [DATABASE]] [--force] [--path [PATH]] [--realpath] [--schema-path [SCHEMA-PATH]] [--pretend] [--seed] [--seeder [SEEDER]] [--step] [--graceful] [--isolated [ISOLATED]]
# Options
# database - The database connection to use. Optional
# force - Force the operation to run when in production. Optional
# graceful - Return a successful exit code even if an error occurs. Optional
# isolated - Do not run the command if another instance of the command is already running. Optional
# path - The path(s) to the migrations files to be executed. Optional
# pretend - Dump the SQL queries that would be run. Optional
# realpath - Indicate any provided migration file paths are pre-resolved absolute paths. Optional
# schema-path - The path to a schema dump file. Optional
# seed - Indicates if the seed task should be re-run. Optional
# seeder - The class name of the root seeder. Optional
# step - Force the migrations to be run so they can be rolled back individually. Optional
# Create a new migration file
php artisan make:migration [--create [CREATE]] [--table [TABLE]] [--path [PATH]] [--realpath] [--fullpath] [--] <name>
# Options
# create - The table to be created. Optional
# fullpath - Output the full path of the migration (Deprecated). Optional
# path - The location where the migration file should be created. Optional
# realpath - Indicate any provided migration file paths are pre-resolved absolute paths. Optional
# table - The table to migrate. Optional
# Arguments
# name - The name of the migration. Required
# Flush the application cache
php artisan cache:clear [--tags [TAGS]] [--] [<store>]
# Options
# tags - The cache tags you would like to clear. Optional
# Arguments
# store - The name of the store you would like to clear. Optional
Development
npm run dev # compile scripts & styles
Deployment
npm run prod # compile and minify scripts & styles
php artisan view:cache # precompile all of the views utilized by your application
php artisan view:clear # clear the view cache
php artisan optimize [-e|--except [EXCEPT]] # Cache framework bootstrap, configuration, and metadata to increase performance
php artisan config:cache # Create a cache file for faster configuration loading
php artisan config:clear # Remove the configuration cache file
Livewire
php artisan make:livewire [component] // Create a livewire component
Blade
@auth
// The user is authenticated...
@endauth
@guest
// The user is not authenticated...
@endguest
Hello, {!! $name !!}. // display var value unescaped (!!never with user data!!)
Check out all Artisan commands, including documentation @ https://artisan.page/