0
我有旅館和塊模型如下:的foreach其中控制器laravel使用循環查詢構建器的結果5.3
class Hostel extends Model
{
//use Sortable;
public function block()
{
return $this->hasMany('App\Block');
}
}
class Block extends Model
{
// use Sortable;
public function hostel()
{
return $this->belongsTo('App\Hostel');
}
}
我具有被從該視圖通過傳遞的變量$gender
和$capacity
URL。在我的HostelContoller.php
我試圖讓所有blocks
的旅館列gender = $gender
和這些blocks
應該有列capacity = $capacity
。這是我在HostelContoller.php
public function hostels($gender, $capacity)
{
$hostels = Hostel::where('gender', $gender)->get();
foreach($hostels as $hostel) {
$blocks = Block::where('capacity', $capacity)
->where('hostel_id', $hostel->id)
->get();
}
# Return the view
return view('student/booking', ['blocks' => $blocks]);
}
都做在booking.php
我有這樣的:
@foreach($blocks as $block)
<tr>
<td> {{ $block->hostel->name }}</td>
<td> {{ $block ->name}}</td>
這僅顯示1個記錄出6條,我不知道我在哪裏丟失了。請幫忙!!!
'$ hostel-> id'裏面有什麼? – claudios
這是來自旅館表 –