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.
use App\Models\Post;
// Create
$post = new Post();
$post->title = 'Hello';
$post->save();| Step | Action | Model State | Database State | Output/Result |
|---|---|---|---|---|
| 1 | Create new Post instance | Post object with no attributes set | No new record | No output |
| 2 | Set title attribute to 'Hello' | Post object with title='Hello' | No new record | No output |
| 3 | Call save() to insert record | Post object saved | New record with title='Hello' added | Record ID assigned |
| 4 | Read record by ID | Post object with title='Hello' | Record exists | Post data retrieved |
| 5 | Update title to 'Hello World' | Post object with title='Hello World' | Record still original | No output |
| 6 | Call save() to update record | Post object saved with new title | Record updated with title='Hello World' | No output |
| 7 | Call delete() on Post | Post object marked deleted | Record removed from DB | No output |
| 8 | Try to read deleted record | No Post object found | No record | Null returned |
| 9 | End of CRUD operations | - | - | - |
| Variable | Start | After Step 2 | After Step 3 | After Step 5 | After Step 6 | After Step 7 | After Step 8 |
|---|---|---|---|---|---|---|---|
| $post | null | Post(title='Hello') | Post(saved with ID) | Post(title='Hello World') | Post(saved updated) | Post(deleted) | null |
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.
create()create() method in Eloquent is designed to add a new record to the database by passing an array of attributes.find() retrieves a record, delete() removes a record, and update() modifies an existing record. None of these add new records.create() -> Option Acreate() [OK]User::find($id) is correct.User->find($id) is invalid because User is a class, not an object. findById() does not exist. User.find() is not PHP syntax.$user = User::find($id); -> Option C$user = User::find(5);
if ($user) {
$user->name = 'Alice';
$user->save();
echo 'Updated';
} else {
echo 'Not found';
}
User::find(5) returns an Eloquent model instance, making $user truthy.if ($user) condition passes, so name is set to 'Alice', save() persists it to the database, and 'Updated' is echoed.$user = User::find($id);
$user->delete($id);
delete() is called on the model instance without any arguments to remove that record.$id to delete(), which expects no parameters. This causes an error.$user = User::find($id);checks if
if ($user && !empty($newEmail)) {
$user->email = $newEmail;
$user->save();
}
$user exists and $newEmail is not empty before updating, preventing errors or empty updates.$user = User::find($id);skips user existence check, risking errors if user is null.
if ($newEmail) {
$user->email = $newEmail;
$user->save();
}
$user = User::findOrFail($id);uses
$user->email = $newEmail;
$user->save();
findOrFail() which throws exception if user missing, no email empty check. $user = User::where('id', $id)->update(['email' => $newEmail]); updates directly but does not check if $newEmail is empty.$user = User::find($id);-> Option B
if ($user && !empty($newEmail)) {
$user->email = $newEmail;
$user->save();
}
Pass the test and this pattern moves unknown