简体   繁体   中英

How to apply search query on related models in Laravel?

I have a problem.

I am doing a search function where users will enter the products they want to search via a form and will return the products.

This is the form.

<form action="{{ route('shopAll') }}">
    <input type="text" name="search" placeholder="Search Products...">
    <button type="submit"><i class="ti-search"></i></button>
</form>

Product.php

    class Product extends Model{
        protected $fillable = [
        'name','added_by', 'user_id', 'category_id', 'subcategory_id', 'subsubcategory_id', 'brand_id', 'video_provider', 'video_link', 'unit_price',
        'purchase_price', 'unit', 'slug', 'colors', 'choice_options', 'variations', 'current_stock'
      ];

    public function category(){
        return $this->belongsTo(Category::class);
    }

    public function subcategory(){
        return $this->belongsTo(SubCategory::class);
    }

    public function subsubcategory(){
        return $this->belongsTo(SubSubCategory::class);
    }

    public function brand(){
        return $this->belongsTo(Brand::class);
    }
}

SearchController.php

    if($request->search){
        $data['products'] = 
            Product::where('name', 'LIKE', '%' . $request->search . '%')
            ->orWhere('tags', 'LIKE', '%' . $request->search . '%')
            ->orWhere('meta_title', 'LIKE', '%' . $request->search . '%')
            ->orWhere('slug', 'LIKE', '%' . $request->search . '%')
            ->get();
    } else {
        $data['products'] = Product::paginate(20);
    }
    
    return view('frontend.shop-all', $data);

I want to make the query to include "orWhere" on the category, subcategory, subsubcategory and brand so that when users want to search for a product, they can search the brand or any of the attributes related to the product. Can someone help me to solve this problem?

Thank you very much.

You might want to take a look at the whereHas/has methdos for querying relationships. Assuming you want to search against the name attribute of your relationships (category, subcategory, subsubcategory, brand etc.), at the top of your SearchController.php, add this line:

use Illuminate\Database\Eloquent\Builder;

and then update your query like this:

$data['products'] = 
        Product::where('name', 'LIKE', '%' . $request->search . '%')
        ->orWhere('tags', 'LIKE', '%' . $request->search . '%')
        ->orWhere('meta_title', 'LIKE', '%' . $request->search . '%')
        ->orWhere('slug', 'LIKE', '%' . $request->search . '%')
        ->orWhereHas('category', function (Builder $query) use ($request) {
            $query->where('name', 'like', '%' . $request->search . '%');
        })
        ->orWhereHas('subcategory', function (Builder $query) use ($request) {
            $query->where('name', 'like', '%' . $request->search . '%');
        })
        ->orWhereHas('subsubcategory', function (Builder $query) use ($request) {
            $query->where('name', 'like', '%' . $request->search . '%');
        })
        ->orWhereHas('brand', function (Builder $query) use ($request) {
            $query->where('name', 'like', '%' . $request->search . '%');
        })
        ->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM