0

Due to Laravel migration system, we can create database tables by running the following command lines.

php artisan migrate:make create_users_table

class CreateUserTable extends Migration {
  public function up() {
    Schema::create('users', function($table)
    {
      $table->increments('id');
      $table->string('email')->unique();
      $table->string('name');
      $table->timestamps();
   });
 }

 public function down() {
   Schema::drop('users');
   }
 }

php artisan migrate

If I work with relational database and have two tables, A and B. Table A is related to table B by a foreign key. Is it possible to create such database tables with Laravel Migration System? Or do I need to configure the relationship in phpmyadmin manually?

1
  • In addition to the two answers below, I'd also add that it's perfectly acceptable to use DB::statement() in the migrations for complex SQL. Commented Jul 9, 2014 at 13:33

2 Answers 2

1

yes its possible to create forgien keys in laravel migration

$table->foreign('fk_id')->references('primary_key')->on('fk_table')->onDelete('cascade');
Sign up to request clarification or add additional context in comments.

Comments

1

You can set foreign keys etc using the schema see here.

Example...

$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');

Also if you are creating tables I would advise using this site to help build your schema, much easier to visualise and work with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.