一个用户(管理员)对laravel中的所有产品关系

时间:2020-02-10 11:29:54

标签: laravel relationship

我想获取所有具有admin用户的设备列表。我有device_user表。但是我没有任何带有管理员ID的行。我不想通过数据库将任何设备分配给管理员。我已经尝试过了。


    public function devices(){
        if (Gate::allows('isUser')) {
            return $this->belongsToMany('App\Device','device_user','user_id','device_id');
        }elseif(Gate::allows('isAdmin')){
            return Device::all();
        }
    }
$data['list_devices'] = User::where('id', $id)->with('devices')->first();
        $list = [];
        foreach ($data['list_devices']->devices as $element) {
            $list[] = $element->last_information_id;
        }

error page

但是当我以其他用户在表中提到的方式登录时,它可以正常工作。 enter image description here

0 个答案:

没有答案