I want to show the search result 'devname' form table devices that is available in reports table so
public function show_report()
{
$device = device::all();
$reports = report::all();
if (request()->has('keyword')) {
$keyword = request()->keyword;
$reports = report::latest()
->where('title', 'like', "%$keyword%")
->orwhere($reports->device->devname, 'like', "%$keyword%")
->paginate(10);
} else {
$reports = report::orderBy('id', 'desc')->paginate(10);
}
return view('admin/reports.show_report', compact('dev', 'reports'));
}
SQLSTATE[42S22]: Column not found: 1054 Unknown column '$reports' in 'where clause'
CodePudding user response:
This is what you need:
public function show_report(Request $request)
{
$devices = Device::all();
$reports = Report::query();
if ($keyword = $request->keyword) { // here you can set variable $keyword
$reports = $reports
->join('devices', 'reports.device_id', '=', 'devices.id') // use join to take devices table
->where('reports.title', 'like', "%$keyword%")
->orwhere('devices.devname', 'like', "%$keyword%");
}
$reports = $reports->orderBy('id', 'desc')->paginate(10);
// in compact function the correct variable name is "devices", "dev" is an unknown variable
return view('admin/reports.show_report', compact('devices', 'reports'));
}
