Models & Queries in PHP

From the Laravel cheat sheet · Eloquent ORM · verified Jul 2026

Models & Queries

Eloquent model basics and querying

php
<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = ['title', 'body', 'user_id'];

    // Basic queries
    $posts = Post::all();
    $post = Post::find(1);
    $post = Post::where('status', 'published')->first();
    $posts = Post::where('views', '>', 100)->get();

    // Create, update, delete
    $post = Post::create(['title' => 'New Post']);
    $post->update(['title' => 'Updated']);
    $post->delete();
}
💡 Use fillable or guarded to protect against mass assignment
⚡ Casts automatically convert attributes to specified types
📌 Soft deletes keep records in database with deleted_at timestamp
🔥 Use chunking for processing large datasets efficiently

More PHP tasks

Back to the full Laravel cheat sheet