Sven van den Boogaart
Sven van den Boogaart

Reputation: 12323

Laravel returning only single column

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';
}

Upvotes: 1

Views: 49

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

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();

Upvotes: 2

Related Questions