Laravel 5.1: Migrating an Existing Database

https://github.com/Xethron/migrations-generator

I migrated my database structure to Laravel using the php artisan migrate:generate command using the extension above. But there is a small problem, my primary key is not called id, I rather used a different convention, adding a prefix for each of them, for example user_id, product_id, photo_id, etc. All of them automatically increase, of course.

Here is my current create_users_table.php file inside my migration folder. I defined user_id to override the default id parameter, is the correct use?

 <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->primary('user_id'); $table->integer('user_id', true); $table->string('name', 500); $table->string('email', 500); $table->string('password', 500); } ); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('users'); } } 

I read that I need to add something like below, but I'm not sure where to define protected $primaryKey , since my class extends Migration, not Eloquent.

 class CreateUsersTable extends Eloquent { protected $primaryKey = 'user_id'; } 

I get the following error when going to the / auth / login page, which, in my opinion, causes due to the use of user_id rather than id. How can i fix this?

 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id' in 'where clause' (SQL: select * from `users` where `users`.`id` = 5 limit 1) 
+7
eloquent laravel
source share
1 answer

You need to specify your non-default primary key in your User model:

 namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { protected $primaryKey = 'user_id'; 

You will need to do this for all models that do not use id as the primary key.

+3
source share

All Articles