Question

I have a table

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

Then I create a user

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

And then I have

{"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}

How can I get "user_id" instead of "id" in my response using Laravel way, without constructions like $user->user_id = $user->id?

Or is it just the way ORM works, and it can't be changed?

Was it helpful?

Solution

You need to specify primary key on your User model:

class User extends \Eloquent {

  protected $primaryKey = 'user_id';

  ...

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top