Master database operations with our guide to CRUD in Laravel. Learn how to save, fetch, update, and delete records using the powerful Eloquent ORM.
Previously in this course, we covered working with Eloquent models, where we learned how to define a model and link it to a database table. In this lesson, we are moving from simply defining models to actually manipulating the data inside them: performing basic CRUD operations.
CRUD stands for Create, Read, Update, and Delete—the four fundamental actions required for any data-driven application. In Laravel, Eloquent makes these operations incredibly intuitive by allowing us to interact with database rows as if they were standard PHP objects.
To create a new record, you instantiate your model as a new object, set its attributes, and call the save() method.
PHPuse App\Models\Task; $task = new Task; $task->title = 'Learn Laravel Eloquent'; $task->description = 'Master the basics of CRUD operations.'; $task->save();
When you call save(), Laravel translates this object into an INSERT SQL statement and executes it. After saving, the model instance will automatically have its primary key (the id column) populated, which is useful if you need to redirect the user to that specific record immediately.
Eloquent provides a fluent interface for fetching data. You’ve already seen basic retrieval, but it’s helpful to understand the variations:
Task::all() returns a collection of all records.Task::find(1) returns a single model instance or null if not found.Task::findOrFail(1) returns the model or throws a 404 exception, which is perfect for controller actions where you don't want to handle null manually.Task::where('completed', false)->get() allows you to chain conditions before executing the query.Updating a record follows a similar pattern to creating one. First, retrieve the instance, change the desired attributes, and call save() again.
PHP$task = Task::find(1); $task->completed = true; $task->save();
Laravel is smart enough to know that because the model already exists in the database, calling save() should perform an UPDATE statement instead of an INSERT.
Once you have a model instance, deleting it is as simple as calling the delete() method.
PHP$task = Task::find(1); $task->delete();
You can also delete a record directly by its primary key without retrieving the object first: Task::destroy(1);. This is an efficient shortcut when you only have the ID available.
To practice these operations, open your project and use php artisan tinker in your terminal. Tinker is an interactive shell that lets you run Laravel code in real-time.
$t = new App\Models\Task; $t->title = 'Test'; $t->save();$t = App\Models\Task::latest()->first();$t->title = 'Updated Title'; $t->save();$t->delete();Observe the output in your terminal after each command. You'll see the SQL queries being executed behind the scenes.
$task->title = 'New') but forgetting to call ->save(). Without that final method call, the change never reaches the database.new Task(['title' => '...']) is convenient, it requires you to configure the $fillable property on your model to protect against security vulnerabilities. We will cover this in detail in a later lesson on preventing mass assignment.all() on large tables: If your tasks table grows to thousands of records, Task::all() will consume a massive amount of memory. Always prefer pagination for lists.Eloquent provides a clean, object-oriented syntax for your database operations. By leveraging save(), find(), and delete(), you can manage your Task Manager's data with minimal boilerplate. Remember that these methods are the building blocks for the more complex logic we will implement in our controllers.
Up next: We will learn about seeding the database to populate our application with dummy data for testing.
Learn how to display database data in your Laravel Task Manager. We'll connect your Eloquent models to your Blade views to render real, dynamic tasks.
Read moreLearn how to use Eloquent, Laravel's powerful ORM, to interact with database tables as clean, object-oriented models for your Task Manager application.
Performing Basic CRUD Operations
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation