Laravel版本:5.8
拉拉维尔管理员:1.7.9
我无法进行级联选择
我有表格:
用户:ID、名称
分类: ID, 姓名
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')]);
}
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;
}
它有效!