我的项目有两个作用
管理员必须查看所有城市的所有数据。 注册后,专家必须查看自己城市的所有数据。
public function index()
{
$schools = SchoolsList::latest()->paginate(25);
$city_id = SchoolsList::where('city_id')->first();
$expert = Role::where('id', '=', 2);
if ($expert){
return view('Admin.inspection-failed.all', compact('schools')->where(($city_id)));
}
else{
return view('Admin.inspection-failed.all', compact('schools'));
}
}
学校表
Schema::create('schools', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('city_id')->unsigned();
$table->foreign('city_id')->references('id')->on('cities');
$table->string('school_name');
$table->string('status');
$table->string('gender');
$table->string('notes');
$table->string('member_name');
$table->string('type');
$table->string('file_number');
$table->string('phone');
$table->string('address');
});
我要当专家登录。专家显示数据仅拥有自己的专家城市。
我收到此错误。
调用数组中的成员函数where()
答案 0 :(得分:1)
此代码中出现错误compact('schools')->where(($city_id))
;因为compact('schools')
等效于['schools' => $schools]
。
请参阅文档http://php.net/manual/en/function.compact.php。
在您的情况下,您的代码等效于['schools' => $schools]->where(($city_id))
。
要修复它,您必须使用
简短答案
$schools = $schools->where('city_id' => $city_id->id);
return view('Admin.inspection-failed.all', compact('schools'));
长答案
$city = City::first(); // you must be fix it yourself
$expert = Role::where('id', '=', 2); // you must be change it
if ($expert) {
$schools = SchoolsList::latest()->paginate(25);
} else {
$schools = SchoolsList::where('city_id', $city->id)->latest()->paginate(25);
}
return view('Admin.inspection-failed.all', compact('schools'));