Laravel Essentials
Laravel is the most popular PHP framework — elegant syntax, powerful ORM (Eloquent), Blade templating, built-in authentication, and a rich ecosystem. It follows MVC and provides everything needed for modern web applications.
40 min•By Priygop Team•Last updated: Feb 2026
Laravel Code Patterns
Example
// Routes (routes/web.php)
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/{post}', [PostController::class, 'show']);
Route::middleware('auth')->group(function () {
Route::post('/posts', [PostController::class, 'store']);
Route::put('/posts/{post}', [PostController::class, 'update']);
Route::delete('/posts/{post}', [PostController::class, 'destroy']);
});
// Eloquent Model (app/Models/Post.php)
class Post extends Model {
protected $fillable = ['title', 'slug', 'content', 'status'];
public function author() {
return $this->belongsTo(User::class, 'author_id');
}
public function comments() {
return $this->hasMany(Comment::class);
}
public function tags() {
return $this->belongsToMany(Tag::class);
}
// Query scope
public function scopePublished($query) {
return $query->where('status', 'published');
}
}
// Controller (app/Http/Controllers/PostController.php)
class PostController extends Controller {
public function index() {
$posts = Post::published()
->with('author', 'tags')
->latest()
->paginate(10);
return view('posts.index', compact('posts'));
}
public function store(Request $request) {
$validated = $request->validate([
'title' => 'required|string|max:255',
'content' => 'required|string',
'status' => 'in:draft,published',
]);
$post = auth()->user()->posts()->create($validated);
return redirect()->route('posts.show', $post);
}
}
// Migration (database/migrations/create_posts_table.php)
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('author_id')->constrained('users');
$table->string('title');
$table->string('slug')->unique();
$table->text('content');
$table->enum('status', ['draft', 'published'])->default('draft');
$table->timestamps();
});