Создадим нужные модели и миграции с помощью команд
php artisan make:model Chat -m
и
php artisan make:model Message -m
Далее заполним миграции. Миграция CreateChatsTable:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateChatsTable extends Migration { public function up(){ Schema::create('chats', function (Blueprint $table){ $table->id(); $table->string('name'); $table->timestamp('created_at')->useCurrent(); $table->timestamp('updated_at')->useCurrent(); }); } public function down(){ Schema::dropIfExists('chats'); } }
И миграция CreateMessagesTable
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateMessagesTable extends Migration { public function up(){ Schema::create('messages', function (Blueprint$table) { $table->id(); $table->unsignedBigInteger('user_id'); $table->unsignedBigInteger('chat_id'); $table->string('message'); $table->timestamp('created_at')->useCurrent(); $table->timestamp('updated_at')->useCurrent(); $table->foreign('user_id')->references('id')->on('users'); $table->foreign('chat_id')->references('id')->on('chats'); }); } public function down(){ Schema::dropIfExists('messages'); } }
Далее запускаем команду
php artisan migrate