Jefsama Jefsama - 5 months ago 11
PHP Question

Laravel Reproduce an array given by the eloquent model

I am new in laravel. Right now i am struggling on how to redeclare the array given by the eloquent model.

PHP code

$data = Commission::select(['uploads_id'])->where([
'affiliate_code' => $id

var_dump Output

array(3) {
array(1) {
string(2) "24"
array(1) {
string(2) "26"
array(1) {
string(2) "27"

i want the
to be redeclare like this.

$data = [24 , 25 , 26]

is there a way on it or different approach?


When you call get() method on Laravel Builder Object, it will return a Collection Object to you. And there are so many nice helpers you can use instead of getting your hands dirty with php array. You can find references here.

In your scenario, this can be done by

    'affiliate_id' => $id

Pluck() is a Collection method and it's documentation can be found here. Remember to cast it to php array with ->all() or ->toArray(). It's useful to be familiar with Collection in Laravel.

Good luck!