1
 $getStrain = DB::connection('mysql')->select('
    SELECT
        
       b.yeast_class_id
    FROM 
        yeast_classes AS a
    LEFT JOIN
        classes_per_yeast AS b 
    ON 
        a.id = b.yeast_class_id
    WHERE
        b.yeast_id = "'.$id.'"
    GROUP BY
        b.yeast_class_id
');

dd($getStrain);

and it returns something like this

enter image description here

But what I want to do is get a response something like this

1,4,5,6,7

where all yeast_class_id will be combined together

0

2 Answers 2

4

From that array of objects you have you could pluck the field you want from them into an array:

Illuminate\Support\Arr::pluck($getStrain, 'yeast_class_id')

Or put the array into a Collection and use the pluck method on the Collection:

collect($getStrain)->pluck('yeast_class_id')->all()

If you want a comma separated string of the list:

collect($getStrain)->pluck('yeast_class_id')->join(',')

Laravel 8.x Docs - Helpers - Array & Objects - Arr::pluck

Laravel 8.x Docs - Helpers - Miscellaneous - collect

Laravel 8.x Docs - Collections - Available Methods - pluck

Laravel 8.x Docs - Collections - Available Methods - all

Laravel 8.x Docs - Collections - Available Methods - join

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

Comments

1

Check the helper values()

$getStrainValues = $getStrain->values();

Comments

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.