Yajra App\User 没有可用的引擎
Yajra No available engine for App\User
我已经为我的 cms 管理员角色做了
现在我有 2 个管理员角色超级管理员和普通管理员
我有一个管理用户的页面,超级管理员和普通管理员都可以看到它,但数据不同
这是控制器代码
public function index()
{
$this->super_admin_role_check();
return view('admin.users.index');
}
public function usersList(){
$super_admin_role = Role::where('name','super_admin')->first();
$is_super_admin=$this->isSuperAdmin();
if($is_super_admin){
$data = User::all();
}else{
// Admin can get all users except super admin
$super_admin = DB::table('role_user')->where('role_id',$super_admin_role->id)->first();
$data = User::where('id','!=',$super_admin->user_id)->first();
}
if(!$data){
$data=[];
}
return DataTables::of($data)->make(true);
}
// This function check if the super_admin roles exist or not
public function super_admin_role_check(){
$user = Auth::guard()->user();
$super_admin_role = Role::where('name','super_admin')->first();
if(!$super_admin_role){
\Artisan::call('db:seed');
// if there's no role factory create super_admin role and assign it to the super admin user
$super_admin_role = Role::where('name','super_admin')->first();
DB::table('role_user')->insert([
'user_id'=>$user->id,'role_id'=>$super_admin_role->id
]);
$permissions = Permission::all();
// here i give all permissions to the super admin
foreach($permissions as $permission){
DB::table('permission_role')->insert(['permission_id'=>$permission->id,
'role_id'=>$super_admin_role->id
]);
}
}
return $super_admin_role;
}
// This Function Check if the user is super admin?
public function isSuperAdmin(){
$user = Auth::guard()->user();
$super_admin_role = Role::where('name','super_admin')->first();
$is_super_admin = DB::table('role_user')->where('user_id',$user->id)->where('role_id',$super_admin_role->id)->first();
return $is_super_admin;
}
问题是,当我以超级管理员身份登录时,一切顺利,但当我以普通管理员身份登录时,出现错误 "No available engine for App\User"
在 \vendor\yajra\laravel-datatables-oracle\src\DataTables.php
有什么帮助吗?
用这个?
$data = User::where('id','!=',$super_admin->user_id)->get();
删除第一个()。由于您只返回一行。
$data = User::where('id','!=',$super_admin->user_id);
这应该有效
我注意到在使用 Yajra Laravel DataTables library, more specifically when you only retrieve a single model and pass it into the of(...) method 时会出现此错误。
下面是一些检索单个模型的示例,其中当检索到的模型直接传递到库的 DataTables::of(/* model here */)->make(true)
方法时,库将抛出错误 (No available engine for App\Models\ModelName at project-root-path\vendor\yajra\laravel-datatables-oracle\src\DataTables.php:63
)。
Retrieving Single Models / Aggregates
// Retrieve a model by its primary key...
$flight = Flight::find(1);
// Retrieve the first model matching the query constraints...
$flight = Flight::where('active', 1)->first();
// Alternative to retrieving the first model matching the query constraints...
$flight = Flight::firstWhere('active', 1);
我通过将检索到的模型包装在一个数组中解决了这个问题。即:
$model = User::where('id','!=',$super_admin->user_id)->first();
DataTables::of($model ? [$model] : [])
->make(true);
旁注:Eloquent的first()
、find()
和firstWhere()
方法returnnull
如果找不到模型。
测试:"yajra/laravel-datatables-oracle": "~9.0" // Specifically v9.17.1
我已经为我的 cms 管理员角色做了 现在我有 2 个管理员角色超级管理员和普通管理员 我有一个管理用户的页面,超级管理员和普通管理员都可以看到它,但数据不同
这是控制器代码
public function index()
{
$this->super_admin_role_check();
return view('admin.users.index');
}
public function usersList(){
$super_admin_role = Role::where('name','super_admin')->first();
$is_super_admin=$this->isSuperAdmin();
if($is_super_admin){
$data = User::all();
}else{
// Admin can get all users except super admin
$super_admin = DB::table('role_user')->where('role_id',$super_admin_role->id)->first();
$data = User::where('id','!=',$super_admin->user_id)->first();
}
if(!$data){
$data=[];
}
return DataTables::of($data)->make(true);
}
// This function check if the super_admin roles exist or not
public function super_admin_role_check(){
$user = Auth::guard()->user();
$super_admin_role = Role::where('name','super_admin')->first();
if(!$super_admin_role){
\Artisan::call('db:seed');
// if there's no role factory create super_admin role and assign it to the super admin user
$super_admin_role = Role::where('name','super_admin')->first();
DB::table('role_user')->insert([
'user_id'=>$user->id,'role_id'=>$super_admin_role->id
]);
$permissions = Permission::all();
// here i give all permissions to the super admin
foreach($permissions as $permission){
DB::table('permission_role')->insert(['permission_id'=>$permission->id,
'role_id'=>$super_admin_role->id
]);
}
}
return $super_admin_role;
}
// This Function Check if the user is super admin?
public function isSuperAdmin(){
$user = Auth::guard()->user();
$super_admin_role = Role::where('name','super_admin')->first();
$is_super_admin = DB::table('role_user')->where('user_id',$user->id)->where('role_id',$super_admin_role->id)->first();
return $is_super_admin;
}
问题是,当我以超级管理员身份登录时,一切顺利,但当我以普通管理员身份登录时,出现错误 "No available engine for App\User" 在 \vendor\yajra\laravel-datatables-oracle\src\DataTables.php 有什么帮助吗?
用这个?
$data = User::where('id','!=',$super_admin->user_id)->get();
删除第一个()。由于您只返回一行。
$data = User::where('id','!=',$super_admin->user_id);
这应该有效
我注意到在使用 Yajra Laravel DataTables library, more specifically when you only retrieve a single model and pass it into the of(...) method 时会出现此错误。
下面是一些检索单个模型的示例,其中当检索到的模型直接传递到库的 DataTables::of(/* model here */)->make(true)
方法时,库将抛出错误 (No available engine for App\Models\ModelName at project-root-path\vendor\yajra\laravel-datatables-oracle\src\DataTables.php:63
)。
Retrieving Single Models / Aggregates
// Retrieve a model by its primary key...
$flight = Flight::find(1);
// Retrieve the first model matching the query constraints...
$flight = Flight::where('active', 1)->first();
// Alternative to retrieving the first model matching the query constraints...
$flight = Flight::firstWhere('active', 1);
我通过将检索到的模型包装在一个数组中解决了这个问题。即:
$model = User::where('id','!=',$super_admin->user_id)->first();
DataTables::of($model ? [$model] : [])
->make(true);
旁注:Eloquent的first()
、find()
和firstWhere()
方法returnnull
如果找不到模型。
测试:"yajra/laravel-datatables-oracle": "~9.0" // Specifically v9.17.1