雄辩的ORM:获得" id"而不是" user_id"在创建新用户时

时间:2014-05-08 10:18:00

标签: orm laravel eloquent

我有一张桌子

Schema::create('users', function(Blueprint $table) {
        $table->increments('user_id');
        $table->string('user_login')->unique();
        $table->string('user_password');
        $table->string('user_email');
        $table->string('user_type');
        $table->timestamps();
    });

然后我创建了一个用户

$data = Input::all();

    $user = new User;
    $user->user_login    = $data['user_login'];
    $user->user_email    = $data['user_email'];
    $user->user_type     = $data['user_type'];
    $user->user_password = Hash::make($data['user_password']);

    $user->save();

    return Response::json($user);

然后我有

{"user_login":"User5","user_email":"user@mail.ru","user_type":"1","user_password":"$2y$10$lqnruQT2EIfqw3m9fi8S8OskmmuQ4mfVuvw0Dbn5fb5.yuw8E7WAa","updated_at":"2014-05-08 11:03:01","created_at":"2014-05-08 11:03:01","id":6}

如何使用Laravel方式在我的回复中获取“user_id”而不是“id”,而不使用$ user-> user_id = $ user-> id?

之类的结构

或者它只是ORM的工作方式,而且无法更改?

1 个答案:

答案 0 :(得分:3)

您需要在User型号上指定主键:

class User extends \Eloquent {

  protected $primaryKey = 'user_id';

  ...

}