File: /var/www/ipsremont-demo/database/migrations/2025_02_10_000000_create_managers_branches.php
<?php
use App\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateManagersBranches extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('managers_branches', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned()->comment('id мененджера');
$table->bigInteger('branch_id')->unsigned()->comment('id подразделения');
$table->timestamp('created_at')->nullable()->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrent();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('branch_id')->references('id')->on('branches')->onDelete('cascade');
});
$managers = User::query()->whereNotNull('branch_id')->get();
foreach ($managers as $manager) {
DB::table('managers_branches')->insert([
'user_id' => $manager->id,
'branch_id' => $manager->branch_id,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('managers_branches');
}
}