Reputation: 1160
I'm writing a booking system for my webapp.
I get the output I want in my Controller with print_r.
Unfortunately I have not been able to get the right output in my View.
$userId = Sentry::getUser()->id;
$my_garden = DB::select('select * from gardens where userId = ?',array($userId));
foreach($my_garden as $garden)
{
$gardenId = $garden->id;
$address = $garden->address;
$bookings_in_my_garden = Garden::find($gardenId)->booking;
print_r($address.'<br>');
foreach($bookings_in_my_garden as $bookings)
{
print_r($bookings->dateFrom." - ".$bookings->dateTo.'<br>');
}
}
The output I get is
Predikerinnenstraat 10
2013-11-05 - 2013-11-12
Prinsenstraat 2
2013-11-30 - 2013-12-07
2013-11-20 - 2013-11-27
and is correct
But when I pass it to my view like so:
return View::make('admin.booking.index')
->with('my_garden',$my_garden)
->with('bookings_in_my_garden',$bookings_in_my_garden);
and call
@foreach($my_garden as $garden)
<tr>
<td>{{ $garden->id }}</td>
@foreach($bookings_in_my_garden as $bookings)
<td>{{ $bookings->dateFrom }}</td>
<td></td>
<td></td>
@endforeach
<td>{{ $garden->address }}</td>
<td></td>
</tr>
@endforeach
I end up with
2 2013-11-30 2013-11-20 Predikerinnenstraat 10
8 2013-11-30 2013-11-20 Prinsenstraat 2
Someone who can help me out?
Upvotes: 0
Views: 1372
Reputation: 87719
You are passing to your view only the last booking.
You have some options, here are two of them:
1) Create a Eloquent Relationship between them, so you can in your view:
@foreach($my_garden as $garden)
<tr>
<td>{{ $garden->id }}</td>
@foreach($garden->bookings as $booking)
<td>{{ $booking->dateFrom }}</td>
<td></td>
<td></td>
@endforeach
<td>{{ $garden->address }}</td>
<td></td>
</tr>
@endforeach
2) Store them all in an array in the controller and pass to your view.
Upvotes: 1