0

Trying to add array to a laravel object with ->push on the object however it is not working. What do I appear to be doing wrong? The object is unchanged.

   $cottages = Cottage::all();

    foreach ($cottages as $cottage) {

        //returns array
        $gallery = $this->getCottageGallery($cottage['folder']);
        //should append to $cottage object
        $cottage->push($gallery);
    }

   dump($cottages);

2 Answers 2

1

I actually just used the $cottage->setAttribute('gallery', $gallery); to assign it to the object!

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

Comments

0

You can do it directly in the collection getting rid of the foreachstatement:

$cottages = Cottage::all()->map(function ($cottage){
             $cottage->setAttribute('gallery', $this->getCottageGallery($cottage['folder']);

            return $cottage;
        });

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.