Laravel 能言善辩的模型拉威尔得到一对多的内部循环
我有一个拉威尔项目。。 我必须得到用户 这是我的Laravel 能言善辩的模型拉威尔得到一对多的内部循环,laravel,model,Laravel,Model,我有一个拉威尔项目。。 我必须得到用户 这是我的 <table class='table table-responsive table-hover table-responsive draggable' border='5'> <thead> <tr class='warning'> <input id='searchAjaxText' type='text' c
<table class='table table-responsive table-hover table-responsive draggable' border='5'>
<thead>
<tr class='warning'>
<input id='searchAjaxText' type='text' class='form-control' placeholder='رقم الهاتف أو اسم المستخدم أو الايميل'/>
</tr>
</thead>
<tbody id='searchAjaxResult'>
</tbody>
</table>
这是我的路线代码
Route::get("/users/search/{keyword}",'UsersController@searchScoop');
这是我的控制器代码
public function searchScoop($keyword)
{
$user = User::searchScoop($keyword);
return $user;
}
这是我在模型中的searchoop函数
public static function searchScoop($keyword)
{
$users = User::where('username','like','%'.$keyword.'%')->
orwhere('email','like','%'.$keyword.'%')->
orwhere('phone','like','%'.$keyword.'%')
->get(['id','username','phone','division','permission']);
return $users;
}
现在我把它放在我的控制台上
Array[1]
0
:
Object
division : 3
id : 15
permission : 3
phone :"0788511619"
username : "bashar"
现在权限和划分是数字。。
如何从权限模型中获取权限名称,并通过id从users表中获取部门模型的部门名称 使用以下方法加载每个用户的权限: 关系的定义如下:
public function permissions()
{
return $this->hasMany('App\Permission', 'permission', 'id');
}
$users = User::where('username', 'like', '%'.$keyword.'%')
->orwhere('email', 'like', '%'.$keyword.'%')
->orwhere('phone', 'like', '%'.$keyword.'%')
->with('permissions', 'divisions')
->get(['id', 'username', 'phone', 'division', 'permission']);
public function permissions()
{
return $this->hasMany('App\Permission', 'permission', 'id');
}