0

Laravel 版本:5.8

Laravel 管理员:1.7.9

我无法进行级联选择

我有桌子:

用户:身份证、姓名

类别:身份证、姓名

categories_users: id, categories_id, user_id

楷模:

class User extends Authenticatable {

 public function category()
    {
        return $this->belongsToMany(Categories::class, 'categories_users', 'categories_id');
    }
}



class Categories extends Model {

public function users()
    {
        return $this->belongsToMany(User::class, 'categories_users');
    }
}

我需要级联选择:

$form->select('user_id', 'User')->options($seller)->load('categories_id', '/admin/api/users')->required();

$form->select('categories_id', 'Categories of user')->required();

但它不起作用!

控制器:

public function getData(Request $request){

        $user_id = $request->get('q');

        return User::find($user_id)->category()->get(['id', DB::raw('name as text')]);
    }
4

1 回答 1

0
public function getData(Request $request){

    $user_id = $request->get('q');

    $q = DB::table('categories_users')->where('user_id', $user_id)->pluck('categories_id');

    $categories = Categories::whereIn('id', $q)->get(['id', DB::raw('name as text')]);

    return $categories;
}

这行得通!

于 2020-01-24T12:51:42.710 回答