0

I'm using Query Builder to get results from multiple joins tables with this query :

$products = DB::table('products as p')
              ->select(
                  'p.id',
                  'p.name',
                  'p.reference',
                  'p.price',
                  'i.path',
                  'i.name'
                )
              ->join('products_r_images as pri', 'pri.product_id', '=', 'p.id')
              ->join('images as i', 'i.id', '=', 'prd.image_id')
              ->get();

One product can have multiples images associated.

With the above query, I obtains this result :

[
    {
        id: 3,
        name: "Test",
        reference: "ref-test",
        price: 123,
        image_path: "product/product-3/",
        image_name: "product_1.jpg"
    },
    {
        id: 3,
        name: "Test",
        reference: "ref-test",
        price: 123,
        image_path: "product/product-3/",
        image_name: "product_2.jpg"
    }
]

As you can see, two rows are returns for one product, while I would like to have documents datas returns in one row with something like that :

[
    {
        product_id: 3,
        name: "Test",
        reference: "ref-test",
        price: 123,
        image_path: "product/product-3/",
        image_name: 
        [
            "product_1.jpg", "product_2.jpg"
        ]
    }
]

Is there a way to do that directly with Query Builder, or another treatment is needed ?

6
  • Have you got Eloquent models set up for these tables? Commented Jul 13, 2017 at 11:31
  • Yes, I have set up for each table Commented Jul 13, 2017 at 11:42
  • 1
    And have you set the relationship up for images? Commented Jul 13, 2017 at 11:43
  • Do you mean a function in Product model returning hasMany(App\Images) ? If it is, no I didn't, because for this query I only use Query Builder. Commented Jul 13, 2017 at 11:48
  • Fair enough :) Just fyi, the relationship would be a belongsToMany not hasMany Commented Jul 13, 2017 at 11:55

1 Answer 1

3

If you just want to use Query Builder for this then you should be able to:

$products = DB::table('products as p')
    ->select(
        'p.id',
        'p.name',
        'p.reference',
        'p.price',
        'i.path',
        'i.name'
    )
    ->join('products_r_images as pri', 'pri.product_id', '=', 'p.id')
    ->join('images as i', 'i.id', '=', 'prd.image_id')
    ->get()
    ->groupBy('id')
    ->map(function ($products) {

        $product = $products->first();
        $product->image_name = $products->pluck('image_name');

        return $product;
    });

Hope this helps!

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

5 Comments

Thank you for your answer ! But I have an error : Call to undefined method Illuminate\Database\Query\Builder::map()
@Nenroz What version of Laravel are you using?
I'm using Laravel 5.4
@Nenroz Have you copied the query exactly i.e. you're still calling get() before the groupBy() and map()?
It was indeed the problem, thank you very much for you help !

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.