1. 모델 마이그레이션 생성

php artisan make:model Booking -m


2. TIMESTAMP_create_bookings_table.php

public function up()
{
Schema::create('bookings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();

$table->date('from');
$table->date('to');

$table->unsignedBigInteger('bookable_id')->index();
$table->foreign('bookable_id')->references('id')->on('bookables');
});
}


3. 마이그레이션

migrate


4. 관계설정

Bookaing.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Booking extends Model
{
public function bookables()
{
return $this->belongsTo(Bookable::class);
}
}


Bookable.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Bookable extends Model
{
public function bookings()
{
return $this->hasMany(Booking::class);
}
}


'라라벨 > laravelbnb' 카테고리의 다른 글

30. 싱글액션컨트롤러만들기  (0) 2020.03.05
29. Booking팩토리만들기  (0) 2020.03.05

+ Recent posts