Hi Dear Friends,
In this post We would like to share infinity knowladge My Good experience and Best solution For .
Try this it will help you and for more information click here: Laravel Examples.
We hope it can help you...
In this post We would like to share infinity knowladge My Good experience and Best solution For .
Laravel Examples
Whilst
developing the back-end for my final year project (redesign of
Panasonic Global), I ran in to the problem of how best to search
multiple tables at once through the relationship models.
Here's some basic context to the issue. We have two models ('Product', and 'Category'). They are joined on a one-to-many relationship:
(The public functions in each model provide us with a method to
search a table through another table. For example, we can search for all
categories via a product, which would only give us the categories that
that individual product is in. This will be useful later.)
The data is stored in tables as such:
The
Of course this could be done relatively easily using multiple queries, however where possible it's best to minimize the number of database queries we're making.
But let's break it down a bit.
So first, we search for all products that have a related category. This is using the 'category()' function that defined earlier in the Product Model. We use this to see if our query is 'like' any of the related category names.
If this returns no results, then we fall back to the second query,
where we query the product directly, checking if the product name is
like the search query:
Here's a quick breakdown in pseudocode:
The Problem
Okay, so that's probably not the best way to describe it. We have a search field that allows users to search for products by either the product name, or the product category. So searching for 'cameras', would show all of the cameras, where as searching for 'Lumix DMC-GM1' should only show that individual product.Here's some basic context to the issue. We have two models ('Product', and 'Category'). They are joined on a one-to-many relationship:
// app/models/Product.php
class Product {
...
public function category() {
return $this->belongsTo('Category', 'product_category');
}
}
// app/models/Category.php
class Category {
...
public function product() {
return $this->hasMany('Product', 'category_id');
}
}
The data is stored in tables as such:
Products | Categories |
---|---|
productid_ | categoryid_ |
product_name | category_name |
product_category * | category_slug |
... | ... |
product_category
refers to the category_id
.Of course this could be done relatively easily using multiple queries, however where possible it's best to minimize the number of database queries we're making.
The Solution
Here's the final code:$products = Product::whereHas('category', function($query) use($term) {
$query->where('category_name', 'like', '%'.$term.'%');
})->orWhere('product_name','LIKE','%'.$term.'%')->orderBy($order, 'asc')->get();
So first, we search for all products that have a related category. This is using the 'category()' function that defined earlier in the Product Model. We use this to see if our query is 'like' any of the related category names.
Product::whereHas('category', function($query) use($term) {
$query->where('category_name', 'like', '%'.$term.'%');
})
->orWhere('product_name','LIKE','%'.$term.'%')->get();
- User searches for something
- First, find all products that are in categories, and match the search query against each category name.
- If this returns any results, great!
- Next, find all products that have a name containing the search query
- If we have results, show them. Otherwise, show something else.
Read :
I hope you like this Post, Please feel free to comment below, your Any Idea, suggestion and problems if you face - I am here to Resolve your Any problems.We hope it can help you...
No comments :
Post a Comment