1

I have a collection which I have extracted from the database using the findMany method in eloquent. When I dd the collection it looks like the attached image. And then I am trying to (in my blade) loop through each item in the array such that I am able to print each of the values in the actual records.

$question = Question::whereIn('id', [23,25])->get();

Then in my blade I am trying to do:

@foreach ($question as $row => $innerArray)
    @foreach ($innerArray as $innerRow => $value)
        {{$value->question}} //I was expecting "What is your favourite food" here
        @foreach ($value->option as $choice)
            <li>{{$choice}}</li> //I was expecting the list of the options here
        @endforeach 
    @endforeach
@endforeach

enter image description here

What am I doing wrong?

1
  • Shouldn't {{$value->question}} be {{$value['question']}} since is an array and not an object property ? And same for @foreach ($value->option as $choice) with $value['options'] with and 's' Commented May 23, 2020 at 14:02

2 Answers 2

1

All multi-result sets returned by Eloquent are instances of the Illuminate\Database\Eloquent\Collection object, including results retrieved via the get method or accessed via a relationship. The Eloquent collection object extends the Laravel base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models.

All collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:

$questions = Question::whereIn('id', [23, 25, ..])->get();

foreach ($questions as $question) {
    echo $question->title; // What is your favourite food
    echo $question->name; // food

    foreach ($question->options as $option) {
         echo $option; // Pizza
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this. I actually had this and it didn't work. So I thought what I had was wrong, turns out it was something else that was breaking it. Just to add for the options, i needed to json_decode it as this is an array stored as a string so effectively hads to use @foreach (json_decode($question->options) as $option)
Right, since what you have there is a string then you have to decode it. However, Laravel has something more elegant than this. Read a little about Attribute Casting
Would it be possible to conduct the loop without get() method? I am using yajra/laravel-datatables and wanted to keep the query as eloquent builder not collection instance.
1

The correct syntax is this:

@foreach ($question as $q)
    {{ $q->question }} //I was expecting "What is your favourite food" here
     @foreach ($q->options as $choice)
         <li>{{$choice}}</li> //I was expecting the list of the options here
     @endforeach 
@endforeach

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.