Laravel Cheat Sheet

In Laravel, PHP

Laravel is a popular open-source PHP web application framework that is designed to make web development easier and more efficient. It was created by Taylor Otwell in 2011 and has since become one of the most widely used PHP frameworks in the world.

Laravel is built on top of several powerful PHP libraries, including Symfony and Composer, and provides developers with a range of features and tools that make it easy to build complex web applications quickly and efficiently. Some of the key features of Laravel include a powerful routing system, an intuitive templating engine, and a robust ORM (Object-Relational Mapping) system that makes it easy to work with databases.

One of the biggest advantages of using Laravel is its focus on simplicity and ease of use. The framework is designed to be easy to learn and use, even for developers who are new to PHP or web development in general. This makes it a great choice for startups and small businesses that need to build web applications quickly and efficiently.

This cheat sheet provides a quick reference for Laravel developers. It is divided into different tables with headlines separating them.

Routing

SyntaxDescription
Route::get($uri, $callback)Define a route that responds to GET requests
Route::post($uri, $callback)Define a route that responds to POST requests
Route::put($uri, $callback)Define a route that responds to PUT requests
Route::delete($uri, $callback)Define a route that responds to DELETE requests
Route::any($uri, $callback)Define a route that responds to any HTTP request method
Route::match(['get', 'post'], $uri, $callback)Define a route that responds to multiple HTTP request methods
Route::redirect($from, $to)Define a route that redirects from one URI to another
Route::view($uri, $view, $data)Define a route that returns a view
Route::fallback($callback)Define a fallback route that responds to any URI that does not match any other route

Controllers

SyntaxDescription
php artisan make:controller MyControllerCreate a new controller
Route::get('/uri', '[MyController::class, 'methodName']')Route to a controller method
$request->input('key')Get a value from the request input
$request->all()Get all values from the request input
$request->has('key')Check if a key exists in the request input
$request->only(['key1', 'key2'])Get only specified keys from the request input
$request->except(['key1', 'key2'])Get all keys except specified ones from the request input
return view('viewName', ['key' => 'value'])Return a view with data

Middleware

SyntaxDescription
php artisan make:middleware MyMiddlewareCreate a new middleware
Route::middleware(['MyMiddleware'])->get('/uri', function () { ... })Apply middleware to a route
public function handle($request, Closure $next)Define middleware logic
app('Illuminate\Contracts\Http\Kernel')->pushMiddleware(MyMiddleware::class)Add middleware to the global middleware stack

Eloquent ORM

SyntaxDescription
php artisan make:model MyModelCreate a new model
MyModel::all()Get all records from the table
MyModel::find($id)Get a record by ID
MyModel::where('column', 'value')->get()Get records by a column value
MyModel::create(['column' => 'value'])Create a new record
$model->update(['column' => 'value'])Update a record
$model->delete()Delete a record
$model->columnGet a column value
$model->column = 'value'Set a column value
$model->save()Save changes to a record

Blade Templates

SyntaxDescription
@extends('layoutName')Extend a layout
@section('sectionName') ... @endsectionDefine a section
@yield('sectionName')Yield a section
@include('partialName')Include a partial
{{ $variable }}Output a variable
@if ($condition) ... @elseif ($condition) ... @else ... @endifConditional statements
@foreach ($items as $item) ... @endforeachLoop through items
@forelse ($items as $item) ... @empty ... @endforelseLoop through items with a fallback
@while ($condition) ... @endwhileLoop while a condition is true
@php ... @endphpEmbed PHP code

References