So which is the main table that they should be joined to ? Linked?
Oct 18, 2022
12
Level 1
How to join more than one table while performing search query in Laravel?
I'm trying to perform a search that involves 3 tables. Please help me to achieve this.
Code from the controller. Thank you in advance.
public function index(Request $request)
{
$linked = Linked::paginate(10); // Table-1
$customers = Customer::all(); // Table-2
$item = Item::all(); // Table-3
$query = $request->search;
if($query){
DB::table('linkeds as linked')->select(['customer_id'])->join('linkeds', 'customers.id', '=', 'linkeds.customer_id');
$customers = Customer::where('customer_name', 'like', "%{$request->search}%")->paginate(10);
$linked = Linked::where('supplier_ref_no', 'like', "%{$request->search}%")->orWhere('supplier_barcode', 'like', "%{$request->search}%")->paginate(10);
}
return view('linked.index', compact('linked', 'customers'));
}
Level 102
@Syed1980 Just prefix it with the table name
->select('linkeds.item_image')
//or
->select('linkeds.*')
Please or to participate in this conversation.