php - Laravel - Query Model if values contain a certain string (taken from search input) -
i implementing search using laravel , ajax. have product belongs tag , subcategory. on other hand subcategory belongs category. want check of properties (field values) , check if contain given string. searching found out have use like. here tried:
$products = product::where('name_en', 'like', $search)->get(); however products if search string matches value. want match if contains it. how can proceed belongsto relationships? how can check propreties of tag , subcategory well? how chain achieve desired result? in advance.
you doing 1 thing wrong, query returns exact matches because given exact string. query should this.
$products = product::where('name_en', 'like', '%'.$search.'%')->get(); above query gives products contains searched string.
and if want search in relational tables can user laravel method join(). there 1 more method wherehas avoiding method, because creates complex query. heavy. can use join() method add inner join relational table.
here example of join:
$products = product::join('tags', function($builder) { $builder->on('tags.id', '=', 'products.tag_id'); // here can add more conditions on tags table. }) join('sub_categories', function($builder) { $builder->on('sub_categories.id', '=', 'products.tag_id'); // here can add more conditions on subcategories table. }) ->where('name_en', 'like', '%'.$search.'%') ->get(); this basic example, can use according requirement.
Comments
Post a Comment