Basic CRUD with Eloquent
Eloquent provides elegant methods for Create, Read, Update, and Delete operations. Every operation returns model instances (not raw arrays), giving you access to relationships, mutators, and model methods on the results.
CRUD Operations
// CREATE
$post = Post::create([
'title' => 'My First Post',
'body' => 'Hello World',
'user_id' => auth()->id(),
]);
// READ
$posts = Post::all(); // All posts
$post = Post::find(1); // By primary key
$post = Post::findOrFail(1); // 404 if not found
$posts = Post::where('published', true)->get(); // Filtered
$post = Post::where('slug', $slug)->firstOrFail(); // First match or 404
$count = Post::where('published', true)->count(); // Count
// UPDATE
$post = Post::findOrFail(1);
$post->update(['title' => 'Updated Title']);
// Or
$post->title = 'Updated Title';
$post->save();
// Mass update
Post::where('published', false)->update(['published' => true]);
// DELETE
$post->delete(); // Soft delete (if SoftDeletes trait)
$post->forceDelete(); // Permanent delete
Post::destroy([1, 2, 3]); // Delete by IDs
// firstOrCreate / updateOrCreate
$user = User::firstOrCreate(
['email' => 'john@example.com'], // Search criteria
['name' => 'John', 'password' => '...'] // Create if not found
);Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Industry Best Practices
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class
- Profile performance with Laravel Debugbar (composer require barryvdh/laravel-debugbar) during development - it shows query counts, memory usage, and timeline for every request
- Keep dependencies up to date: run composer audit regularly to check for security vulnerabilities in your package dependencies
Tip
Tip
Practice Basic CRUD with Eloquent in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Define the relationship, Eloquent builds the SQL automatically
Practice Task
Note
Practice Task - (1) Write a working example of Basic CRUD with Eloquent from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Basic CRUD with Eloquent is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready laravel code.
Key Takeaways
- Eloquent provides elegant methods for Create, Read, Update, and Delete operations.
- Always write tests for this functionality - it's the most reliable way to catch regressions when you refactor or update packages
- Document your implementation decisions in code comments, especially when you deviate from Laravel conventions - future team members (including yourself) will thank you
- Follow the single responsibility principle: each class does one thing. If a method grows beyond 20 lines, consider extracting helper methods or moving logic to a service class