1

so I have this code:

    $collectionOfProducts = \DB::table('products_specif')->whereIn('size',$arrayOfAttributes)->whereIn('color',$arrayOfAttributes)->whereIn('material',$arrayOfAttributes)->get('product_code');
    $product_codes = $collectionOfProducts->unique();
    dd($product_codes->toArray());
    $arrayOfProducts = \DB::table('products')->whereIn('product_code',$product_codes)->get();
    return view('pages.products', compact('arrayOfProducts','type'));

the dd() responds with:

array:4 [▼
  0 => {#1252 ▼
    +"product_code": "SocksCode"
  }
  4 => {#1257 ▶}
  9 => {#1262 ▶}
  13 => {#1266 ▶}
]

I would like to use the $product_codes in my whereIn statement, but I can't because of those numbers at the start(0,4,9,13). How do I convert this collection/array to just a simple "product_code" : "code" array?

1 Answer 1

1

You can use pluck method to get array of product_code

$product_codes= \DB::table('products_specif')
->whereIn('size',$arrayOfAttributes)
->whereIn('color',$arrayOfAttributes)
->whereIn('material',$arrayOfAttributes)->pluck('product_code')->toArray();

$arrayOfProducts = \DB::table('products')->whereIn('product_code',$product_codes)->get();
Sign up to request clarification or add additional context in comments.

1 Comment

select the answer please.. glad to help you :)\

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.