php Laravel 查询生成器返回对象还是数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28176292/
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 query builder returns object or array?
提问by Tiago
I'm building a very simple web app with Laravel.
我正在用 Laravel 构建一个非常简单的网络应用程序。
I've built two separate Controllers, which each return two separate views, as follows:
我构建了两个单独的控制器,每个控制器返回两个单独的视图,如下所示:
ProfileController:
配置文件控制器:
class ProfileController extends BaseController {
public function user($name)
{
$user = User::where('name', '=', $name);
if ($user->count())
{
$user = $user->first();
$workout = DB::table('workouts')->where('user_id', '=', $user->id)->get();
Return View::make('profile')
->with('user', $user)
->with('workout', $workout);
}
return App::abort(404);
}
}
WorkoutController:
锻炼控制器:
class WorkoutController extends BaseController {
public function workout($name)
{
$workout = DB::table('workouts')->where('name', '=', $name)->first();
if ($workout)
{
Return View::make('add-exercise')
->with('workout', $workout);
}
return App::abort(404);
}
}
What is confusing me is what I had to do in order to pass a single workout
object to each view. As you might have noticed the query builders for workout
are different:
让我感到困惑的是我必须做些什么才能将单个workout
对象传递给每个视图。正如您可能已经注意到的那样,查询构建器workout
是不同的:
$workout = DB::table('workouts')->where('user_id', '=', $user->id)->get();
and
和
$workout = DB::table('workouts')->where('name', '=', $name)->first();
On the profile
view, I get an object using the ->get();
method, but on the add-exercise
view, I must use ->first();
or I will otherwise get an array with only one index, where I can then access the object, i.e. $workout[0]->name
instead of $workout->name
.
在profile
视图中,我使用该->get();
方法获取了一个对象,但在add-exercise
视图中,我必须使用,->first();
否则我将获取一个只有一个索引的数组,然后我可以在其中访问该对象,即$workout[0]->name
代替$workout->name
.
Why is this? Shouldn't I be able to use either get
and/or first
in both controllers and expect the same type of result from both since I want the same thing from the same table?
为什么是这样?我不应该能够在两个控制器中使用一个get
和/或first
两个控制器并期望两者得到相同类型的结果,因为我想要同一张表中的相同内容吗?
回答by patricus
get()
returns a collection of objects every time. That collection may have 0 or more objects in it, depending on the results of the query.
get()
每次都返回一个对象集合。该集合中可能有 0 个或多个对象,具体取决于查询的结果。
first()
calls get()
under the hood, but instead of returning the collection of results, it returns the first entry in the collection (if there is one).
first()
get()
在后台调用,但不是返回结果集合,而是返回集合中的第一个条目(如果有)。
Which method you use depends on what you need. Do you need the collection of all the results (use get()
), or do you just want the first result in the collection (use first()
)?
您使用哪种方法取决于您的需要。您需要所有结果的集合(使用get()
),还是只想要集合中的第一个结果(使用first()
)?
回答by joseph roxas
- Model::find(numeric); returns a object
- Model::whereId(numeric)->first(); returns a object
- Model::whereId(numeric)->get(); - returns a collection
- Model::whereId(numeric); - returns a builder
- 模型::查找(数字);返回一个对象
- Model::whereId(numeric)->first(); 返回一个对象
- Model::whereId(numeric)->get(); - 返回一个集合
- 型号::whereId(数字); - 返回一个建造者