Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 能言善辩的模型拉威尔得到一对多的内部循环_Laravel_Model - Fatal编程技术网

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');
}