Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 - Eloquent Relationship Update

I have a question on updating a table in Laravel. I have a User and Car Model. Example as below,

User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function cars()
    {
        return $this->hasMany(Car::class);
    }
}

Car.php

<?php

namespace App;

class Car extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }    
}

For the update, I am using below codes on my controller,

public function update(Request $request, $id)
{
      // validations here

      $car = Car::find($id);

      $carDetail = Car::where('id', $id)
      ->update([
          'vehicle_no'=> $request->vehicle_no,
          'location_id' => $request->location_id,
          'created_at' => $request->created_at,
      ]);

      $user = $car->user()
      ->update([
          'name'=> $request->name,
          'ic_number'=> $request->ic_number,
      ]);

      return back();
}

I am able to update the table but want to know if I am doing it right.

Is there an accurate or a better way of doing it.

like image 219
Khairul Avatar asked Nov 20 '17 08:11

Khairul


People also ask

How can I update my relationship in Laravel?

$item= Item::where('name', $result->name)->firstOrFail(); $variant = $item->variants()->where('text', $result->variant)->get(); $variant->price = $result->price; $item->save();

Does Laravel 8 have one relation?

hasOne relationship in laravel is used to create the relation between two tables. hasOne means create the relation one to one. For example if a article has comments and we wanted to get one comment with the article details then we can use hasOne relationship or a user can have a profile table.

What is hasMany in Laravel?

September 12, 2021 by Trilok Singh 1 Comment. One to Many relationship is to define relationships where a single model owns any amount of other models. For example, a post may have multiple comments so we can get use the hasMany or belongsTo to get the all comments of a post.


1 Answers

When you use relation between two models it's better if you update them as they are in the relation. so yes you'r almost right . but for more information it's better if you use separate REQUEST file other than in the controller. one more thing based on experience it's better when you update the relation first . overall it will be something like this :

 public function update(SomeRequestFile $request, $id)
 {

     $car = Car::find($id);

     $user = $car->user()
         ->update([
             'name'=> $request->name,
             'ic_number'=> $request->ic_number,
         ]);
     $carDetail = Car::where('id', $id)
         ->update([
             'vehicle_no'=> $request->vehicle_no,
             'location_id' => $request->location_id,
             'created_at' => $request->created_at,
         ]);
     return back();
}
like image 189
Alireza Amrollahi Avatar answered Oct 05 '22 20:10

Alireza Amrollahi