vipin93's avatar
Level 13

How can i combine two objects?

I try to show some value using foreach loop but problem is that i have two objects and when i try to use in foreach loop its run 4 times 2+2 but i want only 2 times how can i combine them here is my code

 <tbody class="text-center">
                                 <?php $i = 0 ?>
                                @foreach($timetables as $classe_section)
                                 @foreach($subjectnames as $subject)
                                <?php $i++ ?>
                                <tr>
                                    <td>{{ $i }}</td>
                                    <td>{{ $classe_section->courses['name'] }}</td>
                                    <td>{{ $classe_section->sections['name'] }}</td>
                                    <td>{{ $subject['name'] }}</td>
                                    <td>
                                      @foreach($testnames as $testname)
                                        <a class="btn btn-primary" href="/teacher/student/{{$classe_section->courses['id']}}/{{$classe_section->sections['id']}}/{{$subject['id']}}/{{$testname['id']}}/test_amrks_upload">{{ $testname['name'] }}
                                        </a>
                                      @endforeach
                                    </td>
                                </tr>
                                @endforeach
                                @endforeach
                            </tbody>

here i want merge $timetables and $subjectnames

0 likes
2 replies
vipin93's avatar
Level 13

I did like

$combine =  $timetables->merge($subjectnames);

and in view

                    <tbody class="text-center">
                                 <?php $i = 0 ?>
                                @foreach($combine as $classe_section)
                                <?php $i++ ?>
                                <tr>
                                    <td>{{ $i }}</td>
                                    <td>{{ $classe_section->courses['name'] }}</td>
                                    <td>{{ $classe_section->sections['name'] }}</td>

                              @endforeach 

getting error

Undefined index: name  on line 
<td>{{ $classe_section->courses['name'] }}</td>
and when i try this  <td>{{ $classe_section->courses->name }}</td>

Property [name] does not exist on this collection instance. 

here is my $combine

[
{
"id": 2,
"user_id": 1,
"name": "English",
"remarks": null,
"created_at": "2017-07-14 23:26:09",
"updated_at": "2017-07-14 23:26:09"
},
{
"id": 3,
"section_id": 1,
"course_id": 2,
"asession_id": 2,
"user_id": 1,
"start": "2017-07-16 07:40:00",
"end": "2017-07-16 08:20:00",
"created_at": "2017-07-16 00:38:54",
"updated_at": "2017-07-16 00:38:54",
"courses": {
"id": 2,
"user_id": 1,
"name": "I",
"remarks": null,
"created_at": "2017-07-14 23:05:01",
"updated_at": "2017-07-14 23:05:01"
},
"sections": {
"id": 1,
"user_id": 1,
"name": "A",
"remarks": null,
"created_at": "2017-07-14 23:25:51",
"updated_at": "2017-07-14 23:25:51"
}
},
{
"id": 1,
"user_id": 1,
"name": "Hindi",
"remarks": null,
"created_at": "2017-07-14 23:26:04",
"updated_at": "2017-07-14 23:26:04"
}
]

Please or to participate in this conversation.