0
0
Laravelframework~10 mins

CRUD with Eloquent in Laravel - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CRUD with Eloquent
Create Model Instance
Set Attributes
Save to DB
Read Data
Update Attributes
Save Changes
Delete Record
End
This flow shows how to create, read, update, and delete records using Eloquent models step-by-step.
Execution Sample
Laravel
use App\Models\Post;

// Create
$post = new Post();
$post->title = 'Hello';
$post->save();
This code creates a new Post record with the title 'Hello' and saves it to the database.
Execution Table
StepActionModel StateDatabase StateOutput/Result
1Create new Post instancePost object with no attributes setNo new recordNo output
2Set title attribute to 'Hello'Post object with title='Hello'No new recordNo output
3Call save() to insert recordPost object savedNew record with title='Hello' addedRecord ID assigned
4Read record by IDPost object with title='Hello'Record existsPost data retrieved
5Update title to 'Hello World'Post object with title='Hello World'Record still originalNo output
6Call save() to update recordPost object saved with new titleRecord updated with title='Hello World'No output
7Call delete() on PostPost object marked deletedRecord removed from DBNo output
8Try to read deleted recordNo Post object foundNo recordNull returned
9End of CRUD operations---
💡 CRUD cycle ends after delete and failed read of deleted record
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6After Step 7After Step 8
$postnullPost(title='Hello')Post(saved with ID)Post(title='Hello World')Post(saved updated)Post(deleted)null
Key Moments - 3 Insights
Why does the Post object exist after save() but before delete()?
Because save() writes the data to the database but the object in memory still exists until delete() is called (see steps 3 and 7 in execution_table).
What happens if you try to read a record after deleting it?
The read returns null because the record no longer exists in the database (see step 8 in execution_table).
Does changing attributes update the database immediately?
No, changes to attributes only affect the model in memory until save() is called to update the database (see steps 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the database state after step 3?
ANo new record
BRecord updated with title='Hello World'
CNew record with title='Hello' added
DRecord removed from DB
💡 Hint
Check the 'Database State' column for step 3 in the execution_table.
At which step does the Post object get deleted from the database?
AStep 5
BStep 7
CStep 8
DStep 3
💡 Hint
Look for the 'delete()' action in the 'Action' column of execution_table.
If you update the title but forget to call save(), what happens to the database?
ADatabase remains unchanged
BRecord is deleted
CDatabase updates automatically
DNew record is created
💡 Hint
Refer to steps 5 and 6 in execution_table to see when database updates occur.
Concept Snapshot
CRUD with Eloquent:
- Create: new Model(); set attributes; save()
- Read: Model::find(id) or queries
- Update: change attributes; save()
- Delete: call delete() on model
Remember: save() writes changes to DB, delete() removes record.
Full Transcript
This visual execution trace shows how to perform CRUD operations using Laravel's Eloquent ORM. First, a new model instance is created and attributes are set in memory. Calling save() inserts the record into the database. Reading retrieves the record by ID. Updating changes attributes in memory and requires save() to update the database. Deleting removes the record from the database. Trying to read a deleted record returns null. The variable tracker shows the $post model's state through each step. Key moments clarify that save() is needed to persist changes and that the model object exists separately from the database record. The quiz tests understanding of when database changes happen and the effect of delete(). This step-by-step flow helps beginners see how Eloquent manages data in code and database.