Two foreign keys pointing to the same table / model

Andrew

I have two models:

  • TRUCK
  • DRIVER

TRUCK has two fields which are FKs. Driver (FK) and Driver2 (FK).

When I try to get the truck with driver and driver2, I get two same records.

    $truck = $this->instance->truck()->where('id', $id)
            ->with(['driver', 'driver2',])
            ->firstOrFail();

My Truck Model:

class Truck extends Model
{
    use SoftDeletes;       
    protected $table = 'trucks';
    protected $guarded = ['id'];
    protected $dates = ['deleted_at'];

    public function driver()
    {
        return $this->hasOne('App\Models\Driver');
    }

    public function driver2()
    {
        return $this->hasOne('App\Models\Driver');
    }   

My Driver Model:

class Driver extends Model
{
    use SoftDeletes;    
    protected  $table = 'drivers';
    protected $guarded = ['id'];
    protected $dates = ['deleted_at'];

    public function truck()
    {
        return $this->belongsTo('App\Models\Truck');
    }

I am still new to laravel and something I get stuck. Should I create another model instead maybe?

devnull

By default laravel will use default foreign key,

Eloquent assumes the foreign key of the relationship based on the model name #Further reading

So both relation are pointing to the same FK, So you need to specify the foreign key as below

   return $this->hasOne('App\Models\Driver', 'Driver');
   return $this->hasOne('App\Models\Driver', 'Driver2');

Full code

class Truck extends Model
{
    use SoftDeletes;       
    protected $table = 'trucks';
    protected $guarded = ['id'];
    protected $dates = ['deleted_at'];

    public function driver()
    {
        return $this->hasOne('App\Models\Driver', 'Driver');
    }

    public function driver2()
    {
        return $this->hasOne('App\Models\Driver', 'Driver2');
    }  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Table with two foreign keys pointing to the same column of another table

How to use Factory boy when two foreign keys are pointing to the same table

Django, adding two foreign keys to the same model

JPA Hibernate two foreign keys to the same table

Mysql: using two foreign keys to the same table

Django Aggregation with a Model that has two foreign key fields pointing to same foreign Model?

How to build a (My)SQL table structure for a table containing multiple foreign keys pointing to same table

sql for a table with two foreign keys to same table causing Cartesian product

How to write associations for a model with two foreign keys for the same relationship?

how to create a select statement with 2 foreign keys in one table pointing to two different tables

How to create a sequelize model with 2 foreign keys referencing the same table

EFCore Linq ThenInclude Two Foreign Keys To Same Table

MYSQL: select by two foreign keys from the same table

Eloquent relation setup, two foreign keys to same table laravel 5.2

Entity Framework Code First - two Foreign Keys from same table

mysql queries on two foreign keys referencing the same table

SQL query with two columns as foreign keys of the same table

join on two foreign keys from same table in SQL

EF 6 how to set two foreign keys to same table

Entity Framework Core Two Foreign Keys - Same Table

Select name of two foreign keys referring to same primary key table

EF Core 2.2 - Two foreign keys to same table

two foreign keys on same table to display parent fields

Two foreign keys on same table: how to implement on delete cascade?

How to create two foreign keys to the same table with SQLite in Xamari Form

How to add two foreign keys in the same class (table) in EF

Join on two of the same foreign keys

Duplicate Django Model Instance and All Foreign Keys Pointing to It

SQL Multiple foreign keys pointing to same primary key