Reputation: 162
I'm setting up a database where we are tracking the 'trips' a vehicle takes. Each trip has only one starting point and one ending point, so my first idea was:
trips:
id (int) - primary key
location_start (int) - foreign key
location_end (int) - foreign key
locations:
id (int) - primary key
name(text)
location_start and location_end would store the id from the location table. However, I don't see how to manage this in Eloquent, and when I build this structure using vertabelo.com, I get an error message of "Reference Name Must be Unique"
Should I be using a pivot table for this? If so, I don't see how to enforce the relationship of each trip can have only two locations, a start and end point.
Upvotes: 0
Views: 537
Reputation: 8415
Read the documentation.
In your case, create a migration contain something like this
Schema::table('trips', function ($table) {
$table->integer('location_start')->unsigned();
$table->foreign('location_start')->references('id')->on('locations');
});
The last things, foreign key definitions is defined at schema builder, not in Eloquent.
Upvotes: 1