php Eloquent:find() 和 where() 用法 laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40725353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eloquent: find() and where() usage laravel
提问by Awa Melvine
I am trying to get a record from a posts
database table using its id. I've been banging my head on the find() method for quite sometime now, confused as to why it wasn't working. Here is my query that looks correct to me but didn't work:
我正在尝试posts
使用其 id从数据库表中获取记录。一段时间以来,我一直对 find() 方法感到困惑,不知道为什么它不起作用。这是我的查询,在我看来正确但不起作用:
$post = Post::find($id);
$post->delete();
Reluctantly i did this:
我不情愿地这样做了:
$post = Post::where('id', $id);
$post->delete();
and surprisingly enough, it worked but I have no idea how.
令人惊讶的是,它有效,但我不知道如何。
I also know that unlike find()
, where()
is a query builder and so I could also use it like this:
Post::where('id', $id)->first()
我也知道与find()
,不同的where()
是,它 是一个查询构建器,所以我也可以像这样使用它:
Post::where('id', $id)->first()
Any ideas about the difference in the way the methods work?
关于方法工作方式的差异有什么想法吗?
回答by craig_h
Your code looks fine, but there are a couple of things to be aware of:
您的代码看起来不错,但需要注意以下几点:
Post::find($id);
acts upon the primary key, if you have set your primary key in your model to something other than id
by doing:
Post::find($id);
作用于主键,如果您已将模型中的主键设置为不id
执行以下操作:
protected $primaryKey = 'slug';
then find
will search by that key instead.
然后find
将按该键搜索。
Laravel also expects the id
to be an integer, if you are using something other than an integer (such as a string) you need to set the incrementing property on your model to false:
Laravel 还期望id
是一个整数,如果您使用的不是整数(例如字符串),则需要将模型上的 incrementing 属性设置为 false:
public $incrementing = false;
回答by SlyDave
To add to craig_h's comment above (I currently don't have enough rep to add this as a comment to his answer, sorry), if your primary key is not an integer, you'll also want to tell your model what data type it is, by setting keyType at the top of the model definition.
要添加到上面 craig_h 的评论(我目前没有足够的代表将此作为评论添加到他的答案中,抱歉),如果您的主键不是整数,您还需要告诉您的模型是什么数据类型是,通过在模型定义的顶部设置 keyType。
public $keyType = 'string'
Eloquent understands any of the types defined in the castAttribute()
function, which as of Laravel 5.4 are: int, float, string, bool, object, array, collection, date and timestamp.
Eloquent 理解castAttribute()
函数中定义的任何类型,从 Laravel 5.4 开始,它们是:int、float、string、bool、object、array、collection、date 和 timestamp。
This will ensure that your primary key is correctly cast into the equivalent PHP data type.
这将确保您的主键正确转换为等效的 PHP 数据类型。
回答by Miqayel
Not Found Exceptions
未找到异常
Sometimes you may wish to throw an exception if a model is not found. This is particularly useful in routes or controllers. The findOrFail
and firstOrFail
methods will retrieve the first result of the query. However, if no result is found, a Illuminate\Database\Eloquent\ModelNotFoundException
will be thrown:
有时,如果找不到模型,您可能希望抛出异常。这在路由或控制器中特别有用。该findOrFail
和firstOrFail
方法将检索查询的第一个结果。但是,如果未找到结果,Illuminate\Database\Eloquent\ModelNotFoundException
则会抛出 a:
$model = App\Flight::findOrFail(1);
$model = App\Flight::where('legs', '>', 100)->firstOrFail();
If the exception is not caught, a 404 HTTP response is automatically sent back to the user. It is not necessary to write explicit checks to return 404 responses when using these methods:
如果未捕获到异常,则会自动向用户发送 404 HTTP 响应。使用这些方法时,没有必要编写显式检查来返回 404 响应:
Route::get('/api/flights/{id}', function ($id) {
return App\Flight::findOrFail($id);
});