简体   繁体   English

Laravel Eloquent Model :: find不起作用

[英]Laravel Eloquent Model::find doesn't work

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. 我正在使用Laravel 5.2.36。

I had the same problem. 我有同样的问题。 I sorted out it, using the function intval() to transform $id to an integer value: 我整理了它,使用函数intval()$id转换为integer数值:

$id = intval($id);
$object = Model::find($id);
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. 但这里的id应该是模型中的可填充属性。 for example 例如

protected $fillable = ['id'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM