0
votes

In my Laravel project, default User extend as follows :

class User extends Authenticatable{
  use Notifiable;

  protected $fillable = [
      'id','name', 'email', 'password','type',
  ];

  protected $hidden = [
      'password', 'remember_token',
  ];
}

When I'm trying to get all registered users using this,

$user = User::all();
return $user;

[{"id":0,"name":"sdfsd","email":"[email protected]","type":"a","created_at":"2016-12-11 19:44:39","updated_at":"2016-12-11 19:44:39"},{"id":0,"name":"dcsdc","email":"[email protected]","type":"a","created_at":"2016-12-12 12:14:42","updated_at":"2016-12-12 12:14:42"}]

I got all the users but without their user ids.Id give as '0'.

And when I'm trying with 'where' constrain it gives a Exception.

So can I use User as a Eloquent model? How do I retrieve user table info in Laravel?

2
What kind of exception are you getting?Saumya Rastogi
Provide proper/full code for travel to causeAddWeb Solution Pvt Ltd
Edited. I get above result when use User::all() but id shown as 0. I have checked db and they are not 0. And using User::where('id', "NPC0001") I get error saying 'ErrorException in helpers.php line 519: htmlspecialchars() expects parameter 1 to be string'Sathindu Kavneth
the "id" column shouldn't be in your fillable array, this is a security risk.michaeltintiuc
ID is not a auto increment id. I have changed it to a string. In my app user have to give it.Sathindu Kavneth

2 Answers

1
votes

You must set $incrementing to false on your model.

public $incrementing = false;

Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically.

If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false.

Docs

1
votes

Find public $incrementing = true; into Illuminate\Database\Eloquent\Model.php path file and update it with false and check.

You can get the more detail about same from Eloquent Model Conventions Hope this should solve your problem