Laravel Cheatsheet

Installation and Configuration

composer create-project laravel/laravel example-app

Alternatively you can install the Laravel installer and use it to create a new project:

composer global require laravel/installer
laravel new example-app

Tack on the --pest flag to use Pest as the testing framework:

composer create-project laravel/laravel example-app --pest

Maintenance Mode

# Enable maintenance mode
php artisan down
# Disable maintenance mode
php artisan up
# Display a message when in maintenance mode
php artisan down --secret="apple" --render="errors::coming-soon"
# Bypassing Maintenance Mode
php artisan down --secret="apple"
# Pre-Rendering The Maintenance Mode View
php artisan down --render="errors::503"
# Redirect Maintenance Mode Requests
php artisan down --redirect=/

Blade and Views

Determining If A View Exists

Choose between local or package view

// check local view
if (view()->exists("layouts.$this->layout")) {
    return view("layouts.$this->layout");
}

// if not exists, use package view
return view("package::layouts.$this->layout");
$viewPath = view()->exists("layouts.$this->layout")
    ? "layouts.$this->layout"
    : "package::components.$this->layout";

return view($viewPath)->with(['data' => $data]);
if (view()->exists('user.dashboard-layout')) {
    return view('user.dashboard-layout');
} else {
    return view('authit::user.dashboard-layout');
}

Tips and Techniques

Validate option is 'in' a dropdown or simulate control

const STATUSES = [
    'success' => 'Success',
    'failed' => 'Failed',
    'processing' => 'Processing',
];
'status' => 'required|in:'.collect(Model::STATUSES)->keys()->implode(',')
<x-gt-select wire:model.defer="editing.layout" for="editing.layout"
    label="Layout" placeholder="Please Select...">
    @foreach (App\Models\Model::STATUSES as $value => $label)
        <option value="{{ $value }}">{{ $label }}</option>
    @endforeach
</x-gt-select>

Routes

Redirect

Route::redirect('/here', '/there', 301);
return redirect()->route('login');
Route::get('/dashboard', function () {
    return redirect('/home/dashboard');
});