Laravel - The PHP Framework For Web Artisans

6 min read Original article ↗

Build and ship
software with tools crafted for
productivity

Laravel provides a complete ecosystem for web artisans. Our open source PHP framework, products, packages, and starter kits offer everything you need to build, deploy, and monitor web applications.

Video Thumbnail

Watch video 2 mins

Trusted by thousands of companies around the world

Ecosystem

A PHP framework with a robust ecosystem

Out of the box, Laravel has elegant solutions for the common features needed by all modern web applications. Our first-party packages offer opinionated solutions for specific problems so you don't need to reinvent the wheel.

Backend

Code that speaks for itself

Simple, elegant syntax powers amazing functionality. Every feature has been considered to create a thoughtful and cohesive development experience.

Authentication

1Add an authentication middleware to your Laravel route

                                                    

1Route::get('/profile', ProfileController::class)

2 ->middleware('auth');

2You can access the authenticated user via the Auth facade

                                                    

1use Illuminate\Support\Facades\Auth;

2 

3$user = Auth::user();

Read Authentication docs

Authorization

1Define a policy for your model

                                                    

1public function update(User $user, Post $post): Response

2{

3 return $user->id === $post->user_id

4 ? Response::allow()

5 : Response::deny('You do not own this post.');

6}

2Use the policy via the Gate facade

                                                    

1Gate::authorize('update', $post);

Read Authorization docs

Eloquent

1Define a model for your database table

                                                    

1<?php

2 

3namespace App\Models;

4 

5use Illuminate\Database\Eloquent\Model;

6 

7class Flight extends Model

8{

9 // ...

10}

2Use Eloquent to retrieve records from the database

                                                    

1use App\Models\Flight;

2 

3foreach (Flight::all() as $flight) {

4 echo $flight->name;

5}

Read Eloquent docs

Migrations

1Create a migration for your database table

                                                    

1php artisan make:migration create_posts_table --create=posts

2Define the schema in your migration file

                                                    

1class CreatePostsTable extends Migration

2{

3 public function up()

4 {

5 Schema::create('posts', function (Blueprint $table) {

6 $table->id();

7 $table->string('title');

8 $table->text('content');

9 $table->foreignId('user_id')->constrained()->onDelete('cascade');

10 $table->timestamps();

11 });

12 }

13}

Read Migration docs

Validation

1Define validation rules in your controller

                                                    

1public function store(Request $request)

2{

3 $validated = $request->validate([

4 'title' => 'required|max:255',

5 'content' => 'required',

6 'email' => 'required|email',

7 ]);

8}

2Handle validation errors in your view

                                                    

1@if ($errors->any())

2 <div class="alert alert-danger">

3 <ul>

4 @foreach ($errors->all() as $error)

5 <li>{{ $error }}</li>

6 @endforeach

7 </ul>

8 </div>

9@endif

Read Validation docs

Notification and mail

1Define the notification content

                                                    

1class PostCreated extends Notification

2{

3 public function via()

4 {

5 return ['mail', 'database']; // Send via mail and store in database

6 }

7 

8 public function toMail($notifiable)

9 {

10 return (new MailMessage)

11 ->subject('New Post Created')

12 ->line('A new post has been created: ' . $this->post->title)

13 ->action('View Post', url('/posts/' . $this->post->id))

14 ->line('Thank you for using our application!');

15 }

16}

2Send the notification to the user

                                                    

1public function store(Request $request)

2{

3 $post = Post::create($request->all());

4 

5 $request->user()->notify(new PostCreated($post));

6}

Read Notification and Mail docs

File Storage

1Configure your filesystem

2Handle file uploads in your controller

                                                    

1public function store(Request $request)

2{

3 if ($request->hasFile('image')) {

4 $path = $request->file('image')->store('images', 'public');

5 }

6}

Read File Storage docs

Job Queues

1Define the job logic

                                                    

1class ProcessPost implements ShouldQueue

2{

3 public function handle()

4 {

5 $this->post->update([

6 'rendered_content' => Str::markdown($this->post->content)

7 ]);

8 }

9}

2Dispatch the job from your controller

                                                    

1public function store(Request $request)

2{

3 $post = Post::create($request->all());

4 

5 ProcessPost::dispatch($post);

6}

Read Queue docs

Task Scheduling

1Define the command logic

                                                    

1class SendEmails extends Command

2{

3 protected $signature = 'emails:send';

4 

5 protected $description = 'Send scheduled emails';

6 

7 public function handle()

8 {

9 // Send your emails...

10 }

11}

2Schedule the task

                                                    

1Schedule::command('emails:send')->daily();

Read Task Scheduling docs

Testing

1Write your test using Pest

                                                    

1it('can create a post', function () {

2 $response = $this->post('/posts', [

3 'title' => 'Test Post',

4 'content' => 'This is a test post content.',

5 ]);

6 

7 $response->assertStatus(302);

8 

9 $this->assertDatabaseHas('posts', [

10 'title' => 'Test Post',

11 ]);

12});

2Run the tests on the command line

Read Testing docs

Events and WebSockets

1Create your event

                                                    

1class PostCreated implements ShouldBroadcast

2{

3 use Dispatchable, SerializesModels;

4 

5 public $post;

6 

7 public function __construct(Post $post)

8 {

9 $this->post = $post;

10 }

11}

2Dispatch the event in your controller

                                                    

1public function store(Request $request)

2{

3 $post = Post::create($request->all());

4 

5 PostCreated::dispatch($post);

6}

3In your JavaScript file, listen for the event

                                                    

1Echo.channel("posts." + postId).listen("PostCreated", (e) => {

2 console.log("Post created:", e.post);

3});

Read Events docs

Frontend

Frontend for any stack

Whether you prefer a traditional PHP backend, a modern frontend using Laravel Livewire, or can't get enough React and Vue, Laravel allows you to deliver highly polished and maintainable applications in a fraction of the time.

                                                    

1class UserController

2{

3 public function index()

4 {

5 $users = User::active()

6 ->orderByName()

7 ->get(['id', 'name', 'email']);

8 

9 return Inertia::render('Users', [

10 'users' => $users,

11 ]);

12 }

13}

                                                    

1export default ({ users }) => {

2 return (

3 <div>

4 <h1>Users</h1>

5 <ul>

6 {users.map((user) => (

7 <li key={user.id}>{user.name}</li>

8 ))}

9 </ul>

10 </div>

11 );

12};

Laravel Inertia supercharges your Laravel experience and works seamlessly with React, Vue, and Svelte. Inertia handles routing and transferring data between your backend and frontend, with no need to build an API or maintain two sets of routes.

Read Inertia docs

                                                    

1class Counter extends Component

2{

3 public $count = 1;

4 

5 public function increment()

6 {

7 $this->count++;

8 }

9 

10 public function decrement()

11 {

12 $this->count--;

13 }

14 

15 public function render()

16 {

17 return view('livewire.counter');

18 }

19}

                                                    

1<div>

2 <h1>{{ $count }}</h1>

3 

4 <button wire:click="increment">+</button>

5 

6 <button wire:click="decrement">-</button>

7</div>

Livewire

Super Powered Blade

Laravel Livewire transforms your Laravel applications by bringing dynamic, reactive interfaces directly to your Blade templates. Livewire seamlessly bridges the gap between server-side rendering and client-side interactivity, allowing you to create modern, interactive components without leaving the comfort of Laravel.

Read Livewire docs

                                                    

1Route::get('/api/user', function (Request $request) {

2 return $request->user();

3})->middleware('auth:sanctum');

SPA and Mobile Apps

Built-in Auth

Laravel empowers developers to build robust backends for single-page applications (SPAs) and mobile apps with ease and efficiency. With built-in support for RESTful APIs, authentication, and database management, Laravel simplifies the process of connecting your backend to modern frontend frameworks like Vue.js or React.

Read Sanctum docs

Deploy

Managed or self-hosted deployment platforms

Laravel Cloud provides a fully managed application platform for Laravel applications, while Forge allows you to self-manage VPS servers running Laravel applications.

A fully managed application platform built for developers and teams who just want to ship their next big idea.

Pay as you go pricing

Provision VPS servers and deploy unlimited PHP applications on DigitalOcean, Akamai, Vultr, Amazon, Hetzner, and more.

Plans from $12.00 / month

Monitor

Application monitoring,
logging, and testing

Every Laravel application can achieve enterprise-grade quality with monitoring, observability, and testing tools that empower you to ship with confidence.

Monitoring built for Laravel developers and teams that need to know exactly what's happening in their application.

Get started for free

At-a-glance insights into your application's performance and usage. Track down bottlenecks like slow jobs and endpoints, find your most active users, and more.

Free

An elegant debug assistant providing insight into the requests coming into your application, exceptions, log entries, database queries, queued jobs, and more.

Free

Pest is a testing framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.

Free

Community

Ready to create your next big idea?

Get started now and ship something amazing.