File: /var/www/quadcode-jobs/database/migrations/2023_04_07_160000_location_vacancy_relation_table.php
<?php
use App\Models\LocationVacancyRelation;
use App\Models\Vacancy;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class LocationVacancyRelationTable extends Migration
{
public function up()
{
Schema::create('location_vacancy_relations', function (Blueprint $table) {
$table->id();
$table->integer('location_id')->unsigned()->nullable();
$table->integer('vacancy_id')->unsigned()->nullable();
$table->unique(['location_id','vacancy_id'], 'unique_location_vacancy');
});
/** @var Vacancy[] $vacancies */
$vacancies = Vacancy::query()->get();
foreach ($vacancies as $vacancy) {
(new LocationVacancyRelation(['location_id' => $vacancy->location_id, 'vacancy_id' => $vacancy->id]))->save();
}
}
public function down()
{
Schema::dropIfExists('location_vacancy_relations');
}
}