Laravel CRUD Examples

Things you should know

  • Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs. The @method Blade directive can create this field for you:

Methods

Destroy

This is not livewire form, to correctly send a `DELETE` request in Laravel, you need to encapsulate your `DELETE` action within a form with a submit button.
public function destroy(Course $course) {
    $course->delete();
    return redirect()->route('courses.index');
}

Forms

Create From

<form method="POST" action="{{ route('resource.store') }}">
    @csrf
    ...
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

Edit Form

<form method="POST" action="{{ route('resource.update', $resource) }}">
    @csrf
    @method('PUT')
    ...
    <button type="submit" class="btn btn-primary">Update</button>
</form>

Delete Form

<form method="POST" action="{{ route('resource.destroy', $resource) }}">
    @csrf
    @method('DELETE')
    ...
    <button type="submit" class="btn btn-danger">Delete</button>
</form>

Update vs Store Methods

The save() method performs an INSERT

$flight = new Flight;
$flight->name = $request->name;
$flight->save(); // it will INSERT a new record

$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save(); //this will UPDATE the record with id=1

The update method expects an array of column and value pairs representing the columns that should be updated.

App\Flight::where('active', 1)
          ->where('destination', 'San Diego')
          ->update(['delayed' => 1]); // this will also update the record

Creating Records

The difference between save and create is that save accepts a full Eloquent model instance while create accepts a plain PHP array:

TIP: If the data is not persisting as expected check you have set the `guarded` or `fillable` attributes in the model.

Insert with Store Method

public function store(Request $request, User $user) {
    // set model attributes
    $user->name = request('name');
    $user->age = request('age');
    // persist the database
    $user->save();
}

Insert with Create Method

public function store(Request $request) {
    User::create([
        'name' => request('name'),
        'age' => request('age'),
    ]);
}

Updating Records

Update with Save Method

public function update(Course $course) {
    $course->title = 'New Course Title');
    $course->description = 'Course Description Goes Here!';
    $course->save();
}

Tips and Tricks

Update Using 'switch' Statement

public function update(Course $course) {

    switch(request()->input('action')) {

        case 'save':
            $course->update(request()->validate([ ]));
            return redirect("/course-modules/$course->id/edit");
            break;

        case 'save_close':
            $course->update(request()->validate([ ]));
            return redirect("/courses/$course->course_id/edit");
            break;

        case 'cancel':
            return redirect("/courses/$course->course_id/edit");
            break;
}