[英]Laravel eloquent search for name relation
Okay, so I'm a bit stuck. 好吧,所以我有点卡住了。 I've created a group system in my project.
我在项目中创建了一个小组系统。 The group system is 5 models:
分组系统有5种型号:
What I'm trying to do is a search for members in a group by username input. 我正在尝试通过用户名输入搜索组中的成员。 But I only store the user_id in the GroupMember model, and not username wich is stored in the User model.
但是我只将user_id存储在GroupMember模型中,而不将用户名wich存储在User模型中。 I do not have any relations to groups in the User model yet.
我与用户模型中的组没有任何关系。
Is there some kind of relation I have to put in? 我必须建立某种联系吗? I've not come to understand the Morph relation yet.
我还没有了解Morph关系。
Here are a list of the relations for the Group some of the models needed. 以下是该集团与某些模型所需的关系清单。
Group model 组模型
public function category() {
return $this -> belongsTo('App\GroupCategory', 'category_id');
}
public function owner() {
return $this -> hasOne('App\GroupMember', 'group_id') -> where('owner', true);
}
public function mods() {
return $this -> hasMany('App\GroupMember', 'group_id') -> where('mod', true);
}
public function members() {
return $this -> hasMany('App\GroupMember', 'group_id');
}
public function posts() {
return $this -> hasMany('App\GroupPost', 'group_id');
}
public function comments() {
return $this -> hasMany('App\GroupPostComment', 'group_id');
}
GroupMember model GroupMember模型
public function groups() {
return $this -> belongsToMany('App\Group');
}
public function posts() {
return $this -> hasMany('App\GroupPost');
}
public function comments() {
return $this -> hasMany('App\GroupPostComment');
}
public function user() {
return $this -> belongsTo('App\User');
}
Just to clearify. 只是为了澄清。 I have an input field where the admin can type in a letter or parts of the username he's looking for, then it does a AJAX search where it looks for only members in the group, but it looks by username.
我有一个输入字段,管理员可以在其中输入字母或他要查找的用户名的一部分,然后执行AJAX搜索,其中只查找组中的成员,但按用户名查找。 And username is not in the GroupMember model, but in the User model.
用户名不在GroupMember模型中,而是在用户模型中。
How would I do this? 我该怎么做?
Thanks in advance. 提前致谢。
Edit: The controller method. 编辑:控制器方法。
/**
* @param Group $group
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*
* Manage users
*/
public function getManageUsers(Group $group) {
$group = $group -> with('members.user');
// Usernames can be "test0", "test1", "test2"
// If i search "1" only "test1" should be in the result
$yourSearchQuery = '1';
// The query
$result = $group -> whereHas('members', function ($query) use ($yourSearchQuery) {
$query -> whereHas('user', function ($query) use ($yourSearchQuery) {
$query -> where('name', 'LIKE', '%'. $yourSearchQuery .'%');
});
}) -> get();
// Check the results manually
// It still returns all members of the group
dd($result);
}
So I finally fixed it! 所以我终于解决了! Just had to modify the answer from Odin Thunder a bit.
只需修改一下Odin Thunder的答案即可。
This was the query that works. 这是有效的查询。
$result = $group -> members() -> whereHas('user', function($query) use($q) {
$query -> where('name', 'LIKE', '%'. $q .'%');
}) -> get();
You can get data through relations, try this code for example: 您可以通过关系获取数据,请尝试以下代码:
$group = Group::first();
return $group->members->user
Don`t forget: 不要忘记:
use (namespace of group model)
For search: 搜索:
$yourSearchQuery - string what we must find $ yourSearchQuery-字符串必须找到的内容
$group = Group::with('members.user');
$result = $group -> whereHas('members', function ($query) use ($yourSearchQuery) {
$query -> whereHas('user', function ($query) use ($yourSearchQuery) {
$query->where('name', 'LIKE', '%'. $yourSearchQuery .'%');
}
})->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.