skip to Main Content

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


  1. Please try it this query first use select then join and last your where statement

    public function search(Request $request){
     $query = $request->query;
     $result = DB::table('categories')
      ->select(''catagories.name','products.*','products.name as products_name')
      ->join('products', 'products.id', '=', 'catagories.catagory_id')
      ->where('catagories.name','like',"%$query%")
      ->orWhere('size','like',"%$query%")
      ->orWhere('email','like',"%$query%")
      ->orWhere('description', 'like', "%$query%")
      ->get();
    
    return view('search.results')->with('result', $result);
    

    also when search before using dd($result); to confirm your query result.

    Login or Signup to reply.
  2. Try This out

    public function search(Request $request)
        {
            $query = $request->input('query');
        
            // Validate the input query
            if (empty($query)) {
                return redirect()->back()->withErrors(['query' => 'Search query cannot be empty']);
            }
        
            // Build the search query
            $result = Catagory::where('catagories.name', 'like', "%$query%")
                ->orWhere('catagories.size', 'like', "%$query%")
                ->orWhere('catagories.email', 'like', "%$query%")
                ->orWhere('catagories.description', 'like', "%$query%")
                ->join('products', 'products.id', '=', 'catagories.catagory_id')
                ->select('products.*', 'catagories.name as catagory_name', 'catagories.size', 'catagories.email', 'catagories.description')
                ->get();
        
            // Return the results to the view
            return view('search.results')->with('result', $result);
        }
    

    Then on your view

    @if($result->isEmpty())
            <p>No results found.</p>
        @else
            <table border="1">
                <thead>
                    <tr>
                        <th>Product Name</th>
                        <th>Category Name</th>
                        <th>Size</th>
                        <th>Email</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($result as $item)
                        <tr>
                            <td>{{ $item->products_name }}</td>
                            <td>{{ $item->catagory_name }}</td>
                            <td>{{ $item->size }}</td>
                            <td>{{ $item->email }}</td>
                            <td>{{ $item->description }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        @endif
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search