Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5
Ad
I thought that eloquent would (by default) enforce uniqueness accross the two id columns when generating a simple pivot (many-to-many) table?
Or am I doing something wrong?
Appointment model
public function client()
{
return $this->belongsToMany('App\Client')->withTimestamps();
}
Client model
public function appointment()
{
return $this->belongsToMany('App\Appointment')->withTimestamps();
}
Migration
$table->integer('appointment_id')->unsigned();
$table->foreign('appointment_id')->references('id')->on('appointments')->onDelete('cascade');
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->timestamps();
If eloquent doesn't do it by default, I'll have to add the index manually:
$table->unique(array('appointment_id', 'client_id'));
Ad
Answer
Ad
Eloquent can't enforce uniqueness accross the two id columns, because Eloquent is not responsible for the database schema/configuration.
Migrations are responsible for this stuff, so you have to define uniqueness in a migration.
Ad
source: stackoverflow.com
Related Questions
Ad
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms
Ad