laravel 访问模型属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22157863/
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
laravel access model properties
提问by valkan
I am looking for solution how to access eloquent model items by 'alias' field. There is no problem accessing items by 'id'. But building a custom query I find myself unable to access item properties.
我正在寻找如何通过“别名”字段访问 eloquent 模型项的解决方案。通过“id”访问项目没有问题。但是构建自定义查询我发现自己无法访问项目属性。
This piece of code works perfect
这段代码完美运行
$cat = Category::find(1);
return $cat->title;
But if I am querying items with any other argument - properties are inaccessible
但是如果我用任何其他参数查询项目 - 属性是不可访问的
This code
这段代码
$cat = Category::where('alias','=','vodosnab')->get();
return $cat->title;
throws an exception
抛出异常
Undefined property: Illuminate\Database\Eloquent\Collection::$title
Could you please help.
能否请你帮忙。
回答by The Alpha
You already got the answer but here are some insights, when you use get()
or all()
, it returns a collection of model objects, which is an instance of Illuminate\Database\Eloquent\Collection
, so here you'll get a Collection
object
你已经得到了答案,但这里有一些见解,当你使用get()
or 时all()
,它返回一个模型对象的集合,它是 的一个实例Illuminate\Database\Eloquent\Collection
,所以在这里你会得到一个Collection
对象
$cat = Category::where('alias','=','vodosnab')->get();
Now, you can use, $cat->first()
to get the first item (Category Model
) from the collection and you may also use $cat->last()
to get the last item or $cat->get(1)
to get the second item from the collection. These methods are available in the Collection
object.
现在,您可以使用,从集合中$cat->first()
获取第一项 ( Category Model
),也可以使用$cat->last()
获取最后一项或$cat->get(1)
从集合中获取第二项。这些方法在Collection
对象中可用。
Using the first()
method like Category::where('alias','=','vodosnab')->first();
will return you only a single (the first mathing item) model which is an instance of your Category
model. So, use all()
or get()
to get a collection of model objects and you can loop through the collection like:
使用first()
like 方法Category::where('alias','=','vodosnab')->first();
只会返回一个(第一个数学项)模型,它是模型的一个实例Category
。因此,使用all()
或get()
获取模型对象的集合,您可以循环遍历该集合,例如:
foreach(Category::all() as $cat) { // or Category::get()
$cat->propertyName;
}
Or you may use:
或者你可以使用:
$categories = Category::where('alias','=','vodosnab')->get();
foreach($categories as $category) {
$category->propertyName;
}
Also, you may use:
此外,您可以使用:
$categories = Category::where('alias','=','vodosnab')->get();
$firstModel = $categories->first();
$lastModel = $categories->last();
$thirdModel = $categories->get(2); // 0 is first
If you need to get only one then you may directly use:
如果您只需要获得一个,那么您可以直接使用:
$category = Category::where('alias','=','vodosnab')->first();
$category->fieldname;
Remember that, if you use get()
you'll get a collection of Model
objects even if there is only one record available in the database. So, in your example here:
请记住,如果您使用get()
,Model
即使数据库中只有一条记录可用,您也会获得一组对象。因此,在您的示例中:
$cat = Category::where('alias','=','vodosnab')->get();
return $cat->title;
You are trying to get a property from the Collection
object and if you want you may use:
您正在尝试从Collection
对象中获取属性,如果需要,可以使用:
$cat = Category::where('alias','=','vodosnab')->get();
return $cat->first()->title; // first item/Category model's title
return $cat->last()->title; // last item/Category model's title
return $cat->get(0)->title; // first item/Category model's title
You may read this articlewritten on Laravel
's Collection
object.
您可以阅读写在's object上的这篇文章。Laravel
Collection
回答by Can Geli?
get()
returns a Collection
of items. You probably need first()
that returns a single item.
get()
返回一个Collection
项目。您可能需要first()
返回单个项目。