Update the Record of Related Model in Laravel

laravel eloquent update
laravel model example
laravel model attributes
laravel eloquent one to many example
laravel model events
laravel update data
findorfail laravel
laravel one to many relationship with pivot table

How to update a record in the related table model by chain expression?

This is what I currently do (and it works)

$user = User::find(1);

$token = Token::where('user_id', $user->id)->first();
$token->token = $request->token;
$token->save();

But can I do the above in a more elegant way, such as?

$user = User::find(1);

$user->token()->token = $new_token;
$user->token()->save();

My User Model

public function token()
{
    return $this->hasOne('App\Token');
}

How to update a related model in Laravel Eloquent, How do I update above two columns in teacher table instead of inserting a new record. share. Laravel - Update Records - We can update the records using the DB facade with update method. The syntax of update method is as shown in the following table.

Yo can do it like this :

User::find(1)->token()->update(['token' => $new_token]);

Or do it in youApp\Token class like this :

User::find(1)->token()->update_token($new_token);

And create update_token function in App\Token class:

public function update_token(string $new_token)
{
      $this->update(['token'=>$new_token]);
}

Eloquent ORM - Laravel, Inserting & Updating Related Models. The save Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method. Again, the updated_at timestamp will automatically be updated, so there is no need to manually set its value:

$user = User::with('token')->findOrFail(1);
$user->token->update(['token' => $request->token]);

Eloquent: Relationships - Laravel, Hello, I am new to Laravel and have an issue in one of my models. I have the It insert the account record in the database, but fail with the relation table. I've got a table for a sports team. The record shows the team selection and some other information. I want to update the record with the team selection. My model is thus: class Selection extends M

Eloquent: Getting Started - Laravel, Inserting & Updating Related Models. The save Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. I'm quite new to laravel and I'm trying to update a record from form's input. However I see that to update the record, first you need to fetch the record from database. Isn't is possible to something like to update a record (primary key is set):

3 related Models, how to insert update delete?, If this is the intended relationship between these two entities then to update the See more about relations at http://laravel.com/docs/4.2/eloquent#relationships. Eloquent has one less-known function called withCount(): it helps to get the amount of related records inside of the main object. It also works with two layers deep, inside of hasManyThrough relations. Let's take a look.

Eloquent relationships, We've already seen that without Eloquent, to insert a new record into the database we https://laravel.com/docs/5.3/eloquent#inserting-and-updating- models. Updates. In addition to inserting records into the database, the query builder can also update existing records using the update method. The update method, like the insert method, accepts an array of column and value pairs containing the columns to be updated. You may constrain the update query using where clauses:

Comments
  • I think your approach is pretty good
  • Being a nerd, I was hoping that there is more practical way of doing that
  • Well, you could do this as well on one line: User::find(1)->token()->update(['token' => $new_token]);
  • I would accept it as correct answer cos I like it
  • I've elaborated a little bit more as an answer