3

This don't work:

$object = Model::find($id);

This works:

$object = Model::where('id', '=', $id)->first();

It doesn't make sense. Am I missing something? I'm using Laravel 5.2.36.

Boris Brdarić
  • 4,674
  • 2
  • 24
  • 19

3 Answers3

3

id needs to be a primary key, see: https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Model.html#method_find

tanerkay
  • 3,819
  • 1
  • 19
  • 28
1

I had the same problem. I sorted out it, using the function intval() to transform $id to an integer value:

$id = intval($id);
$object = Model::find($id);
Hannele
  • 9,301
  • 6
  • 48
  • 68
0
public $primaryKey  = 'id';

make it a primary key in your model file or you can use

Model::firstOrNew(['id' => $id]);

but here id should be a fillable property in the model. for example

protected $fillable = ['id'];
Aditya Tomar
  • 841
  • 1
  • 13
  • 20