使用外键构建的架构

时间:2013-06-12 16:44:32

标签: laravel-4

我想知道是否有人可以告诉我如何在SQL架构构建器中编写以下Laravel 4

CREATE TABLE `conversation_reply` (
`cr_id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`reply` text,
`user_id_fk` int(11) NOT NULL,
`ip` varchar(30) NOT NULL,
`time` int(11) NOT NULL,
`c_id_fk` int(11) NOT NULL,
FOREIGN KEY (user_id_fk) REFERENCES users(user_id),
FOREIGN KEY (c_id_fk) REFERENCES conversation(c_id)
);

2 个答案:

答案 0 :(得分:6)

如果我明白你在问什么,那应该很接近;

Schema::table('conversation_reply', function($table)
{
    $table->increments('cr_id');
    $table->text('reply')->nullable();
    $table->foreign('user_id_fk')->references('user_id')->on('users');
    $table->string('ip', 30);
    $table->integer('time');
    $table->foreign('c_id_fk')->references('c_id')->on('conversation');
});

答案 1 :(得分:1)

遵循:

下面的Schema将进入public function up()

下的schema-migrations文件
//Create ** category table **

    Schema:Create ('category',function ($table)){

                //Relation: category table id used as FK -> to -> product table category_id
                $table->increments('category_id');
                //category name
                $table->string('category_name',40)->unique();
                //time staps: created,Updated
                $table->timestamps();
    }

比,内部产品架构:

            //Create ** product table **

            Schema::create('products', function ($table){

                //product id
                $table->increments('product_id');
                //product name
                $table->string('p_name')->unique();
                //product description
                $table->text('p_description');
                //created_at and  updated_at column
                $table->timestamps();
                //Foregine Key
                $table->integer('category_id_fk')->unsigned();
                //Foreign Key link to category table
              $table->foreign('category_id_fk')->references('category_id')->on('category');
         }
相关问题