1

In my database I have categories and sizes, they have a table to match them together called category_sizes which looks like:

table fields screenshot

I want to get an array with all size_id where category_id = 4 so I did:

$catSizes = CategorySize::where('category_id', 4)->value('size_id');
return($catSizes);

But it returns one result only. How can I get an array with all size_ids so I can use it later in a where_in statement.

I also tried pluck but this gives also just one row.

$catSizes = CategorySize::where('category_id', 4)->pluck('size_id');
dd($catSizes);

My CategoySize model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CategorySize extends Model
{
    protected $table = 'category_sizes';
}

1 Answer 1

2

Try this:

DB::table('category_sizes')->where('category_id', 4)->get('size_id');

Or:

DB::table('category_sizes')->select('size_id')->where('category_id', 4)->get();
Sign up to request clarification or add additional context in comments.

2 Comments

Just for my interest: Is there a reason why you suggested DB::table instead of using the model?
@Peh, I guess it's possible to use Eloquent here too (->pivot and ->withPivot methods): laravel.com/docs/5.1/eloquent-relationships#many-to-many - but I couldn't post working Eloquent code without testing (I don't have enough time for this right now, sorry).

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.