简体   繁体   中英

how do I get records from pivot table attached to specific user_id into other table

I am using laravel. I have many to many relationships for vehicles and users table. So there're 3 tables.

vehicles(id, vrm_id, info, created_at, updated_at)

users(id, name, created_at, updated_at)

vehicle_user(id, user_id, vehicle_id, created_at, updated_at)

Now, I got one vehicle, and one user. But in vehicle_users there are 5 records. same user_id and vehicle_id for each of them.

Question : I want to achieve this with laravel eloquent without joins.

How do I get all of those 5 records from pivot table with their corresponding created_at values and also some columns from vehicles table and after that, i should also be able to provide where user_id = 5 .

I tried : Vehicle::with('users') but this only returns 1 record from vehicles table.

I am having to assume you have set your models relations correctly since you have not provided model code to view.

It is worth noting that Laravel automatically tries to work out what the pivot table will be based on the 2 main tables, in alphabetical order, and since u comes before v the default pivot would be user_vehicle not vehicle_user . You can keep it this way round, but you would need to update the relationship in your model to reflect that.

Now, I got one vehicle, and one user. But in vehicle_users there are 5 records. same user_id and vehicle_id for each of them.

This seems odd. If you only have 1 record in each of your user and vehicle tables, you should only have 1 record in your pivot as well. Maybe an issue with your validation?

How do I get all of those 5 records from pivot table with their corresponding created_at values and also some columns from vehicles table and after that, i should also be able to provide where user_id = 5

If the main thing you want to return is the pivot table table, and neither of the vehicle or user table data, I would consider making the pivot a model in its own right. With hasOne relations to both users and vehicles. then you could do something like

UserVehicle::with('vehicle')->whereHas('user', function($query){
    $query->where('id', 5);
})->get();

This would give you back the main pivot table data, with its related vehicle model columns, where user id = 5.

Try something like this:

In User Model

public function vehicles()
{
    return $this->belongsToMany('App\Vehicle')
        ->withTimestamps();
}

You can inverse the relation if you need.

Now if you want to get the vehicles of a user in controller

public function getVehicles($id)
    {
        $user = User::find($id);
        foreach ($user->vehicles as $vehicle)
        {
            echo $vehicle->pivot->created_at; //this is coming from the pivot table
            echo $vehicle->info; //this is from the vehicle table
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM