Want to fetch data from three tables in laravel 5.2 - php

I have a little query in which I need your help, Please have a look below.
I want to fetch all the data from the products table with some conditions like city, price, Type, category.
I can fetch all the data but I can't fetch the category data from the product model with all other conditions.
Below is my tables and Eloquent relations. I am using Laravel 5.2.
products ->foreign_key('subcategory_id')
subcategories ->foreign_key('category_id')
category
users: ->foreign_key('product_id')
users table columns: ->city, price, product_id
Relations:
User Model:
public function product(){
return $this->hasMany('App\Product');
}
Product Model:
public function subcategory(){
return $this->belongsTo('App\Subcategory', 'subcategory_id');
}
Subcategory Model:
public function product(){
return $this->hasMany('App\Product', 'subcategory_id');
}
public function category(){
return $this->belongsTo('App\Category');
}
Category Model:
public function subCategory(){
return $this->hasMany('App\Subcategory');
}
public function product(){
return $this->hasManyThrough('App\Product', 'App\Subcategory');
}
Here is my query(in ProductsController.php).
$city_id, $category_id, $min_price, $max_price : demo data passed
//fetching city data
$products = Product::whereHas('user', function($query) use($city_id) {
$query->where('city_id', $city_id);
});
//fetching category data (I am not sure about this part)
$products = $products->whereHas('category', function($query) use($category_id) {
$query->where('id', $category_id);
});
//fetching price data
$products = $products->where('price', '>=', $min_price)->where('price', '<=', $max_price);
//sub category filtering
$products = $products->where('subcategory_id', 1);
$products = $products->get()->toArray();
return $products;
Any help would be appreciated. Thanks in Advance.

I think you can use with() method:
Product Model:Add the following method
public function subcategory($category_id){
return $this->belongsTo('App\Subcategory', 'subcategory_id')->with(`category`)->where('category_id',$category_id);
}
Now in Controller, you can check if the product belongs to that category,
$products = $products->subcategory($category_id);
This will get you category data also.

Related

Call to undefined method App\Product::whereHas() in laravel

Hi try to get my products list in subcategory page but i'm getting this error
Call to undefined method App\Product::whereHas()
here is my function
// Subategory page included posts
public function productsubcategory($slug)
{
$products = Product::whereHas('subcategory.category', function($q) use($slug){
$q->where('slug',$slug);
})->paginate(6);
return view('frontend.subcategories', compact('products'));
}
this is my route:
Route::get('/category/{slug}/subcategory/{subslug}', ['as' => 'showbysub', 'uses' => 'IndexController#productsubcategory']);
my product model:
public function categories(){
return $this->belongsTo(Category::class);
}
public function subcategories(){
return $this->belongsTo(Subcategory::class);
}
category model:
public function products(){
return $this->hasMany(Product::class);
}
public function subcategories(){
return $this->hasMany(Subcategory::class);
}
subcategory model:
public function category(){
return $this->belongsTo(Category::class, 'category_id');
}
public function products(){
return $this->hasMany(Product::class);
}
any idea?
UPDATE:
I changed my function to:
public function productsubcategory($slug)
{
$subcategories = Subcategory::where('slug','=',$slug)->with('products')->paginate(6);
return view('frontend.subcategories', compact('subcategories'));
}
and now my page is loading but will not show any item (product).
UPDATE 2
I changed my function to this:
public function productsubcategory($slug)
{
$products = Product::with('subcategories')
->orderBy('id', 'desc')
->get();
return view('frontend.subcategories', compact('products'));
}
Now I can get products but the problem is all products will show even
if they're included other categories.
How to solve that?
I don't understand what do you mean in this lines
$products = Product::whereHas('subcategory.category', function($q) use($slug){
$q->where('slug',$slug);
})->paginate(6);
Refer to https://laravel.com/docs/5.4/eloquent-relationships. If you are trying to querying relationship, you can do like this:
$products = Product::whereHas('subcategories', function($q) use($slug){
$q->where('slug',$slug);
})->paginate(6);
or
$products = Product::whereHas('categories', function($q) use($slug){
$q->where('slug',$slug);
})->paginate(6);
The relationship name must the same with relationship method in your Product model
SOLVED
public function productsubcategory($slug, $subslug)
{
$products = Product::whereHas('subcategory', function($q) use($subslug){
$q->where('slug',$subslug);
})->get();
return view('frontend.subcategories', compact('products'));
}
and product model from subcategories change to subcategory

How to get Category info in detail page in laravel

I have a page where i show products under their subcategory url's and in top of my page i want to print the subcategory title and subcategory image, how can I do that?
here is my function:
public function productsubcategory($slug, $subslug)
{
$products = Product::whereHas('subcategory', function($q) use($subslug){
$q->where('slug',$subslug);
})->paginate(12);
return view('frontend.subcategories', compact('products'));
}
and I show my products with foreach like #foreach($products as $product) ....
The best way you doing is One to Many relationship. Use ORM for that. You will get details here: https://laravel.com/docs/5.0/eloquent
In your Category.php Model
public function subcategories()
{
return $this->hasMany('App\Subcategory', 'category_id');
}
In you Subcategory.php Model
public function category()
{
return $this->belongsTo('App\Category', 'category_id');
}
public function products()
{
return $this->hasMany('App\Product', 'subcategory_id');
}
And your Product.php Model
public function subcategory()
{
return $this->belongsTo('App\Subcategory', 'subcategory_id');
}
And you will get subcategory list
$subcategories = Category::find($id)->subcategories;
And You get products
foreach($subcategories as $subCat)
{
//your code
}
And here is another link for One to Many relationship. If you read details it will help you. https://laravel.com/docs/5.5/eloquent-relationships
SOLVED
I added another query in my function so totally become like this:
public function productsubcategory($slug, $subslug)
{
$products = Product::whereHas('subcategory', function($q) use($subslug){
$q->where('slug',$subslug);
})->paginate(12);
$productss = Product::whereHas('subcategory', function($q) use($subslug){
$q->where('slug',$subslug);
})->first();
return view('frontend.subcategories', compact('products', 'productss'));
}
And here is how i show my subcategory info in their pages.
{{$productss->Subcategory->title}}
Hope this help others.

Eager load relationships in laravel with conditions on the relation

I have categories related to each other in a tree. Each category hasMany children. Each end category hasMany products.
The products also belongsToMany different types.
I want to eager load the categories with their children and with the products but I also want to put a condition that the products are of a certain type.
This is how my categories Model looks like
public function children()
{
return $this->hasMany('Category', 'parent_id', 'id');
}
public function products()
{
return $this->hasMany('Product', 'category_id', 'id');
}
The Product Model
public function types()
{
return $this->belongsToMany(type::class, 'product_type');
}
In my database I have four tables:
category, product, type, and product_type
I've tried eager loading like so but it loads all the products and not just the ones that fulfil the condition:
$parentLineCategories = ProductCategory::with('children')->with(['products'=> function ($query) {
$query->join('product_type', 'product_type.product_id', '=', 'product.id')
->where('product_type.type_id', '=', $SpecificID);
}]])->get();
Instead of the current query, try if this fits your needs.
(I modified my answer as follows with your comment)
$parentLineCategories = ProductCategory::with([
'children' => function ($child) use ($SpecificID) {
return $child->with([
'products' => function ($product) use ($SpecificID) {
return $product->with([
'types' => function ($type) use ($SpecificID) {
return $type->where('id', $SpecificID);
}
]);
}
]);
}
])->get();
You can use whereHas to limit your results based on the existence of a relationship as:
ProductCategory::with('children')
->with(['products' => function ($q) use($SpecificID) {
$q->whereHas('types', function($q) use($SpecificID) {
$q->where('types.id', $SpecificID)
});
}])
->get();

Laravel Eloquent 5 Tables

I have 5 tables.
Users
Categories
Products
Product_categories
Order Details
A user purchases an an item and in my order details table I store the quantities etc.
I wanted to return all items that are of the main category = 'Testing' via the user.
$user = Auth::user();
return $user->items();
I have the following relationship on my user model.
public function items()
{
return $this->hasMany('App\OrderDetail','user_id')->selectRaw('item_description,count(quantity) as count')->where('item_description','<>','Carriage')->groupBy('item_id')->get();
}
I know I've not associated the the categories table here but I'm wondering how I would pull all the users order details where item category is "testing". The item can be related to many categories hence the product_categories table.
I'm not after someone writing the answer I'd like to know where I start to look at linking these via the model?
Would I be right in saying I have to do a function within my model relation?
According to your requirements & structure, your table should be structured like this:
users
id
name
...
categories
id
name
...
products
id
name
cost
...
category_product
id
category_id
product_id
order_details
id
user_id
cost
...
product_order_detail
id
product_id
order_detail_id
Your models should be structured like this:
class User extends Model
{
public function orderDetails()
{
return $this->hasMany(OrderDetail::class);
}
}
class Product extends Model
{
public function categories()
{
return $this->belongsToMany(Category::class, 'category_product');
}
public function orderDetails()
{
return $this->belongsToMany(Order::class, 'product_order_detail');
}
}
class Category extends Model
{
public function product()
{
return $this->belongsToMany(Product::class, 'category_product');
}
}
class OrderDetail extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function products()
{
return $this->belongsToMany(Product::class, 'product_order_detail');
}
}
and to fetch all the items / products who belongs to the category named Testing and belongs to the user, who've ordered it:
$items = Product::whereHas('categories', function($q) {
$q->where('name', '=', 'Testing');
})->whereHas('orderDetails', function($q) use($user) {
$q->whereHas('user', function($q) use($user) {
$q->where('id', $user->id);
});
})->get();
Hope this helps!

Fetch records from three distant related table

I have a little query in which I need your help, Please take a below look.
Below is my tables and Eloquent relations. I am using Laravel 5.2.
products {id, subcategory_id}
subcategories {id, category_id}
category {id, name}
Product Model:
public function subcategory(){
return $this->belongsTo('App\Subcategory', 'subcategory_id');
}
Subcategory Model:
public function product(){
return $this->hasMany('App\Product', 'subcategory_id');
}
public function category(){
return $this->belongsTo('App\Category');
}
Category Model:
public function subCategory(){
return $this->hasMany('App\Subcategory');
}
public function product(){
return $this->hasManyThrough('App\Product', 'App\Subcategory');
}
How can I fetch all the records from a particular category using category_id.
Thanks For your help.
It may solve your issue
$products = DB::table("products")
->join('subcategories', 'subcategories.id', '=', 'products.subcategory_id')
->join('category ', 'category.id', '=', 'subcategories .category_id')
->get();

Categories