Page 29 - Laravel5_v1
P. 29
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('typebooks');
}
}
้
้
ิ
้
5. เปดไฟล์ xxx_create_books_table.php เพื อเขียนโคดในการสรางโครงสรางของตาราง เช่นเดียวกัน โดยตาราง books นี จะมี FK
้
เพื อ relation ไปที ตาราง typebooks ดวยว่าหนังสือเล่มนี มีประเภทหนังสืออะไร
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id'); //รหัสหนังสือ
$table->string('title'); //ชอหนังสือ
ื
$table->decimal('price',10,2); //ราคา
$table->integer('typebooks_id')->unsigned();
$table->foreign('typebooks_id')->references('id')->on('typebooks');
$table->string('image'); //เก็บชอภาพหนังสือ
ื
$table->timestamps();
});
}
/**
* Reverse the migrations.