라라벨/laravelbnb

28. 예약 모델만들기 관계설정

땀모 2020. 3. 4. 06:14

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);
}
}