Eloquent: Quick Reference

Query time casting

  • Laravel allows you to cast dates direct in a query using query time casting which automatically casts the date to a Carbon instance. Docs
Command Action
$table->json('options'); creates a JSON equivalent column:

Aggregates

You can use the following methods to perform aggregate functions on the database.

$count = Flight::where('active', 1)->count();
$max = Flight::where('active', 1)->max('price');

Update vs Save Methods

save() Method

The save() method performs either an INSERT or an UPDATE based on the state of the model.

INSERT: When called on a new model instance, it creates a new record.

$flight = new Flight;
$flight->name = $request->name;
$flight->save(); // Performs INSERT

UPDATE: When called on an existing model instance (fetched from the database), it updates the corresponding record.

$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save(); // Performs UPDATE

update() Method

The update() method works on a query and directly performs an update operation in the database without retrieving model instances. It requires an array of column-value pairs for the columns to be updated.

App\Flight::where('active', 1)
            ->where('destination', 'San Diego')
            ->update(['delayed' => 1]); // Directly updates matching records

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;
}