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
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
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
A table can have multiple foreign keys based on the requirement.
Yes, it is okay to have two fk to the same pk in one table.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With