I have created a search with Laravel 11 that is in a controller and has a join to another table. However, with the join some of the results do not show .
This is my controller:
public function search(Request $request){
$query = $request->input('query');
$result = Catagory::where('catagories.name','like',"%$query%")
->orWhere('size','like',"%$query%")
->orWhere('email','like',"%$query%")
->orWhere('description', 'like', "%$query%")
->join('products', 'products.id', '=', 'catagories.catagory_id')
->select('products.*','products.name as products_name')
->get();
return view('search.results')->with('result', $result);
}
This is the blade:
<form action="{{route('search')}}" method= 'GET'>
@csrf
<input type="text" name="query" id="query">
<button class="btn btn-primary-sm" type="submit">Search</button>
This is the route:
Route::get('/search/search', [CatagoriesController::class,'search'])->name('search');
When I run the above searh I get the catagories.name
and (the join) the products_name. Furthermore, if I remove the join I get all the data size, email, description which I do not get with the join
As I said above removing the join, it work but without products_name, I checked the $request and the $query with a dd.
2
Answers
Please try it this query first use select then join and last your where statement
also when search before using
dd($result)
; to confirm your query result.Try This out
Then on your view