2016-12-01 5 views
0

Ich habe die Hostel und Block- Modelle wie folgt:foreach wo Schleife Abfrage-Generator führt Controller 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'); 
    } 
} 

I haben die Variablen $gender und $capacity, die aus der Sicht durch die übergeben werden, URL In meinem HostelContoller.php Ich versuche, alle der Hostels mit Spalte gender = $gender zu erhalten, und diese blocks sollte Spalte capacity = $capacity haben. Das ist, was ich in 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]); 
    } 

In booking.php dies habe ich getan haben:

@foreach($blocks as $block) 
      <tr> 

      <td> {{ $block->hostel->name }}</td> 
      <td> {{ $block ->name}}</td> 

Dies wird nur angezeigt wird 1 Datensatz von 6 Aufzeichnungen und ich weiß nicht, wo ich bin es fehlt. Bitte helfen !!!

+0

Was ist in $ hostel-> id'? – claudios

+0

Es ist die 'ids' der Hostels aus dem Hostels Tabelle –

Antwort

0

Sie überschreiben jedes Mal die $ blocks-Variable. Sie sollten Blöcke zu $ ​​Blöcken wie folgt hinzufügen:

public function hostels($gender, $capacity) 
      { 
       $hostels = Hostel::where('gender', $gender)->get(); 
       $blocks = array(); 
       foreach($hostels as $hostel) { 
        $blocks[] = Block::where('capacity', $capacity) 
            ->where('hostel_id', $hostel->id) 
            ->get(); 
       } 

       # Return the view 
       return view('student/booking', ['blocks' => $blocks]); 
      } 
+0

Vielen Dank. –

Verwandte Themen