sal*_*lep 7 laravel eloquent laravel-5.1
https://github.com/Xethron/migrations-generator
我php artisan migrate:generate在上面的扩展的帮助下使用命令将我的数据库结构迁移到Laravel .但是有一个小问题,我的主键没有被命名为id,我宁愿使用不同的约定,为每个人添加一个前缀,如user_id,product_id,photo_id等.当然,所有这些都是自动递增的.
这是我的迁移文件夹中的当前create_users_table.php文件.我定义了user_id来覆盖默认的id选项,这是正确的用法吗?
<?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');
}
}
Run Code Online (Sandbox Code Playgroud)
我读到我需要添加类似下面的内容,但我不确定在哪里定义,protected $primaryKey因为我的类扩展了Migration而不是Eloquent.
class CreateUsersTable extends Eloquent {
protected $primaryKey = 'user_id';
}
Run Code Online (Sandbox Code Playgroud)
当我转到/ auth/login页面时,我收到以下错误,我认为这是因为user_id使用而不是id.我该如何解决?
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id' in 'where clause' (SQL: select * from `users` where `users`.`id` = 5 limit 1)
Run Code Online (Sandbox Code Playgroud)
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';
Run Code Online (Sandbox Code Playgroud)
You will need to do this for all Models that don't use id as their primary key.