diff --git a/app/Models/City.php b/app/Models/City.php index 7239666..160ecfd 100644 --- a/app/Models/City.php +++ b/app/Models/City.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class City extends Model { @@ -15,6 +16,11 @@ class City extends Model 'url', ]; + public function companies(): BelongsToMany + { + return $this->belongsToMany(Company::class, 'cities_companies', 'city_id', 'company_id'); + } + public static function validate_with_name(string $name) { $name = $name ?? ''; diff --git a/app/Models/Company.php b/app/Models/Company.php index 0fe26f8..9010974 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Company extends Model { @@ -17,6 +18,11 @@ class Company extends Model 'description', ]; + public function cities(): BelongsToMany + { + return $this->belongsToMany(City::class, 'cities_companies', 'company_id', 'city_id'); + } + public function categories(): HasMany { return $this->hasMany(Category::class); diff --git a/database/migrations/2023_06_19_174521_create_cities_companies_table.php b/database/migrations/2023_06_19_174521_create_cities_companies_table.php new file mode 100644 index 0000000..18dc844 --- /dev/null +++ b/database/migrations/2023_06_19_174521_create_cities_companies_table.php @@ -0,0 +1,35 @@ +id(); + $table->unsignedBiginteger('city_id')->unsigned(); + $table->unsignedBiginteger('company_id')->unsigned(); + + $table->foreign('city_id')->references('id') + ->on('cities')->onDelete('cascade'); + $table->foreign('company_id')->references('id') + ->on('companies')->onDelete('cascade'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('cities_companies'); + } +};