Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel have Two foreign Key from the same table

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

like image 342
Evan Avatar asked Nov 29 '18 11:11

Evan


People also ask

Can a table have two foreign keys from same table?

A table can have multiple foreign keys based on the requirement.

Can two foreign keys reference the same primary key?

Yes, it is okay to have two fk to the same pk in one table.

How to create foreign key in same table MySQL?

To create a new table containing a foreign key column that references another table, use the keyword FOREIGN KEY REFERENCES at the end of the definition of that column. Follow that with the name of the referenced table and the name of the referenced column in parentheses.

Can foreign key be NULL MySQL?

MySQL essentially implements the semantics defined by MATCH SIMPLE , which permits a foreign key to be all or partially NULL . In that case, a (child table) row containing such a foreign key can be inserted even though it does not match any row in the referenced (parent) table.


1 Answers

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

like image 145
Lucas Ferreira Avatar answered Oct 09 '22 09:10

Lucas Ferreira