9

I understand how to use Eloquent for basic queries and relationships, but I start getting confused when selecting information based on relationships in multiple tables.

For example, i can get the data I need from the database using the the query builder as follows:

    $data['products'] = DB::table('product')
    ->select('product.id', 'product.ref_num', 'productdetails.name')
    ->join('productdetails', function($join)
    {
        $join->on('product.id', '=', 'productdetails.product_id')
             ->where('productdetails.website_id', '=', '7');
    })
    ->leftJoin('product_category', function($join) use($submenu_id){
        $join->on('product.id', '=', 'product_category.product_id')
            ->where('product_category.category_id', '=', $submenu_id);
    })
    ->leftJoin('product_type', function($join) use($type_id){
        $join->on('product.id', '=', 'product_type.product_id')
            ->where('product_type.type_id', '=', $type_id);
    })
    ->get();

Basically, i'm getting data from the product and productdetails tables based on which category the product is part of and what type of product it is; These are defined by inner joins to pivot tables product_type and product_category.

Now assuming i have the eloquent relationships set up correctly, how would i go about doing this in Eloquent?

Here are the relevant parts of the Eloquent Models

Product

class Product extends Eloquent{

public function productdetails()
{
    return $this->hasMany('Productdetail');

public function categories()
{
    return $this->belongsToMany('Category', 'product_category', 'product_id', 'category_id');
}

public function types()
{
    return $this->belongsToMany('Producttype', 'product_type', 'product_id', 'type_id');
}
}

Product Details

class Productdetail extends Eloquent
{


public function product()
{
    return $this->belongsTo('Product');
}
}

ProductType

class ProductTypes extends Eloquent{


function products()
{
    return $this->belongsToMany('products', 'product_id', 'type_id', 'product_id');
}

Category

class Category extends Eloquent{

public function products()
{
    return $this->belongsToMany('product', 'product_category', 'category_id', 'product_id');
}
}

Thanks in advance

2 Answers 2

15

Assuming your relations are correct and related table names are: categories and types, this will do the job:

Product::with('productdetails')
    ->whereHas('categories', function ($query) use ($submenu_id) {
          $query->where('categories.id', '=', $submenu_id);
    })
    ->whereHas('types', function ($query) use ($type_id) {
          $query->where('types.id', $type_id); // side note: operator '=' is default, so can be ommited
    })
    ->get();

It will run 2 queries ( first for getting appropriate products, second for product details related to them) and return Eloquent Collection

Sign up to request clarification or add additional context in comments.

2 Comments

after a few hours of endless search you saved my day, thanks.
Using whereHas has a downside that it slows your query and if there are a greater number of records in your table the whereHas will broke
-6

//routes.php

 /* relationship is : content has images(one to many). And I am extracting the images based on the name that is given in the search input ($keyword = Input::get('keyword') */


   $dbresults = DB::table('contents')->join('images', 'images.content_id', '=', 'contents.id')->where('contents.Name', 'LIKE', '%' .$keyword. '%')->get();

   return View::make('/results')->with("results", $dbresults);

View

@foreach($results as $result)
{{ $result->image_1 }}
@endforeach

1 Comment

The question is about Eloquent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.