php 使用 eloquent 从 laravel 5 中的刀片运行选择查询的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30759860/
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
Correct way to run a select query from blades in laravel 5 using eloquent
提问by echoashu
What is the Correct Way to retrieve a column value based on certain select filter on a Model variable availed by compact
method inside the blade. (Larevl 5)
I read that Its a bad practice to query database staright from views, and hence i followed the convention to avail the required data with compact
method to view
根据compact
刀片内方法使用的模型变量上的某些选择过滤器检索列值的正确方法是什么。(Larevl 5)
我读到从视图直接查询数据库是一种不好的做法,因此我遵循了约定来利用所需的数据和compact
方法来查看
However, In a scenario where I need to query another table based on certain column value returned in foreach loop inside a blade from first table, I am unable to figure out correct Approach
但是,在我需要根据第一个表中刀片内的 foreach 循环中返回的某些列值查询另一个表的情况下,我无法找出正确的方法
Example: I have two Models User
& Group
Schema User
Table
示例:我有两个模型User
和Group
架构 User
表
id,name,email,group_id
Scheme Group Tableid
,groupname
方案组表id
,groupname
Here is the UserController -> compact method
这是 UserController -> 紧凑方法
$users = \App\User::all(array('id','name','email','group_id'));
$groups = \App\Group::all(array('id','group_name'));
return view('user.index', compact('users','groups'));
Here how the blade needs them
这就是刀片如何需要它们
@foreach ($users as $user)
<tr>
<th>{{$user->id}}</th>
<th>{{$user->name}}</th>
<th>{{$user->email}}</th>
<th>
<!-- Here i need to run something like
select group_name from group where id = $user->id -->
{{$groups->where('id','=',$user->group_id) }}
</th>
<th>Actions</th>
</tr>
@endforeach
I know this returns an array , and I'have two questions here
我知道这会返回一个数组,我在这里有两个问题
- How to get the value for
group_name
column from the Group Model based on group.id
=$user->id
in a foreach loop - Since Its a bad practice to query db from blade, how would I avail the values from a model by passing data via compact from controller to blade, when the where clause parameter's are not yet known.
- 如何
group_name
从基于组的组模型中获取列的值。id
=$user->id
在 foreach 循环中 - 由于从刀片查询数据库是一种不好的做法,当 where 子句参数尚不清楚时,我将如何通过从控制器到刀片的压缩传递数据来利用模型中的值。
Edit 1:
编辑1:
I modified the last group query as
我将最后一个组查询修改为
<th>@if($groups->where('id','=',$user->group_id))
@foreach($groups as $group)
{{$group->group_name}}
@endforeach
@endif
</th>
And I was able to get the result, however this again isn't a correct approach , so question remain unanswered
我能够得到结果,但这又不是一个正确的方法,所以问题仍未得到解答
回答by chanafdo
In User
model
在User
模型中
public function group()
{
return $this->belongsTo('App\Group');
}
In Group
model
在Group
模型中
public function users()
{
return $this->hasMany('App\User');
}
In your controller
在您的控制器中
$users = \App\User::with('group')->get();
return view('user.index', compact('users'));
Now in your view you can do
现在在你看来你可以做
$user->group->name;
回答by Michel
I appreciate the fact that you know "It's bad practice to query from view". Why don't you use join.
我很欣赏您知道“从视图中查询是不好的做法”这一事实。你为什么不使用连接。
DB::table('users')->join('groups', 'users.group_id', '=', 'groups.id')->get();
Then pass the result to your view and loop through it. Here you will have each user data associated with his group data.
然后将结果传递给您的视图并循环遍历它。在这里,您将拥有与其组数据相关联的每个用户数据。