Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #298
    dan
    Participant

    In the Laravel documentation in Eloquent ORM section, it only shows one way to update a model:

    use App\Models\Flight;

    $flight = Flight::find(1);

    $flight->name = ‘Paris to London’;

    $flight->save();

    I didn’t know you can use the update method in query builder to update a model as follows:

    $flight = Flight::find(1);

    $flight -> update([
    ‘name’ => ‘Paris to London’;
    ]);

    #299

    One you have already an instance of an Eloquent model, you can use any of the available method in the Query Builder.

    Check this documentation:

    https://laravel.com/docs/10.x/eloquent#retrieving-models

    Note:

    “Since Eloquent models are query builders, you should review all of the methods provided by Laravel’s query builder. You may use any of these methods when writing your Eloquent queries.”

    and that includes update method of the Query Builder of course.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.