BigJobbies
BigJobbies

Reputation: 4055

Schema building with foreign keys

I was wondering if someone could show me how to write the following SQL in Laravel 4Schema Builder`?

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

Upvotes: 2

Views: 5162

Answers (2)

Dixit
Dixit

Reputation: 13056

Do Following:

Below Schema will go inside schema-migrations file under public function up()

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

Than,Inside product schema:

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

Upvotes: 1

Joachim Isaksson
Joachim Isaksson

Reputation: 181017

If I understand what you're asking, this should be close;

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

Upvotes: 6

Related Questions