Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mglrahul's avatar

Want to fetch data from three tables in laravel 5.2

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.

0 likes
7 replies
ehben's avatar

Can you use the stackoverflow syntex so we can better read the code ? like

<?php
echo "test";
?>
mglrahul's avatar

How you manage to add the black theme in background of your answer?

Snapey's avatar

Put three backpacks ``` before and after your code block

You can go back and edit your original post

MalaniDerrick's avatar

$parent_id = 1;

$category = Category::with('products.reviews')
whereHas('products', function($q)
{
    $q->has('reviews');
})
->where('parent_id','=',$parent_id);
->get();

MalaniDerrick's avatar
$articles =DB::table('articles')
                ->join('categories', 'articles.id', '=', 'categories.id')
                ->join('users', 'users.id', '=', 'articles.user_id')
                ->select('articles.id','articles.title','articles.body','users.username', 'category.name')
                ->get();

MalaniDerrick's avatar
table movies
    id - integer
    title - string   (like )

table roles
    id - integer
    name - string    (like "Actor", "Director")

table factors
    id - integer
    name - sting  (like "John Doe", "Melanie Doe") 

table factor_movie_role
    factor_id - integer   1 john doe       2 Melanie Doe
    movie_id - integer   1 one movie    1 one movie
    role_id - integer      2 director       1 Actor 

so..

SELECT * FROM factor_movie_role
    INNER JOIN factors ON factor_movie_role.factor_id = factors.id
    INNER JOIN movies ON factor_movie_role.movie_id = movies.id
    INNER JOIN roles ON factor_movie_role.role_id = roles.id


Please or to participate in this conversation.