Eg. Region and City are two models. Relations are defined as below:
Region.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Region extends Model
{
public function cities() {
return $this->hasMany('App\City');
}
}
City.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class City extends Model
{
public $timestamps = false;
public function region() {
return $this->belongsTo('App\Region');
}
}
Region can have multiple cities but a city can be associated with a single Region only. For this, I am having a list of cities already added but want to attach city with the region on the details page of a region just like if we have a many-to-many relationship. How to validate and not allow to attach city to region which is already attached to any other region?