Laravel have Two foreign Key from the same table

Evan

Hi I am not very good with Database Design so I would like to know if it's possible to create a table with two foreign key from the same table.

this is my effort on trying to show the relationship enter image description here

is it possible for me to do this using laravel migration I have tried but it's not working

Schema::create('events', function (Blueprint $table) {
        $table->increments('event_id');
        $table->integer('book_id')->unsigned();
        $table->integer('buyers_id')->unsigned();
        $table->integer('seller_id')->unsigned();
        $table->integer('status')->default(1);
        $table->timestamps();

        $table->foreign('book_id')->references('id')
        ->on('books')
        ->onDelete('cascade')
        ->onUpdate('cascade');
        $table->foreign('buyers_id')->references('id')
        ->on('users')
        ->onDelete('cascade')
        ->onUpdate('cascade');
        $table->foreign('seller_id')->references('id')
        ->on('users')
        ->onDelete('cascade')
        ->onUpdate('cascade');
    });

so the event table will have 3 FK

  • one FK from Books
  • two FK from Users

     Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
    
        $table->string('name')->unsigned();
    
        $table->integer('user_type');
    
        $table->integer('password');
    
        $table->timestamps();
    
    
    });
    

this is the schema of users table

Lucas Ferreira

If in your application your User can act both as a seller and a buyer, the role_id solution proposed in the comments of your quest will not work for you.

Following the solution you were already working on, it not seems to have anything wrong with your migrations, when you run php artisan migrate is it showing any error? If you post it, I can update this part of my response.

In the Event model, you have to describe the relationships like this:

public function buyer()
{
    return $this->belongsTo(User::class, 'buyers_id');
}

public function seller()
{
    return $this->belongsTo(User::class, 'seller_id');
}

So when you use your Event model, you can do it like this

$event = new Event;
$event->buyers_id = 1; // supposing there's a user with id 1
$event->seller_id = 2; // supposing there's a user with id 2
$event->save();

$event->buyer;
# => <User::class id=1>
$event->seller;
# => <User::class id=2>

[UPDATE] Add a little tip

Not related to your question, but I suggest you rename your buyers_id column to buyer_id. It's a common practice to keep your foreign keys name in the singular in Laravel.


[UPDATE] Link to the docs

For full documentation on the relationship' methods.

https://laravel.com/docs/5.7/eloquent-relationships#one-to-many-inverse

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Multiple Foreign Key from the same table

delete from 2 table in sql that have a foreign key in other tables

Joining 2 tables which have the same foreign key from same table but one is a one to many

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

Two foreign Key of same table in one table in sequelize

MYSQL foreign key to the same table?

Relationships with same foreign key to same table Laravel 5

MySQL two foreign key from same table

Using foreign key in the same table

Laravel Eloquent Relationship has the same foreign key on the same table

Join two table with two different Foreign Key for get same field

Laravel migration: adding foreign key to the same table where ID is a string

Foreign key on same table

Eloquent Foreign Key to Same Table

How to retrieve elements that have the same foreign key from a list?

Foreign key contraints in a table on two columns referncing a same table

Laravel Migration Two table foreign key dependencies

Multiple column foreign key referencing on the same key from a table

Two FOREIGN key constraints from single parent table not working

Two fields with foreign key from a table - Rails

Two columns in same table and same foreign key

Using Entity Framework Code First to have two Foreign Keys from same parent table without having to specify the collections on the parent entity

MySQL Using Same Foreign key for two different table columns

Foreign key same table laravel get tree view

is it possible for two rows of a column (primary key) of the table to have same character/value with same foreign key(another column)

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

Select 2 counts from the same table with a foreign key involved

how Inner join work on two foreign key from single table

How to make a foreign key within two columns is the same table?