2016-05-24 13 views
5

Programuję aplikację internetową. Backend jest spokojny aplikacja oparta na laravel 4.Laravel - Undefined offset 0 - collection.php

Mam problemy ze szczególnym kontrolerem.

BedsController.php

class BedsController extends \BaseController { 

    /** 
    * Display a listing of the resource. 
    * GET /beds 
    * 
    * @return Response 
    */ 
    public function index() 
    { 
     // 
     $user = JWTAuth::parseToken()->authenticate(); 
     $data = Input::get('room'); 
     $retorno = array(); 
     $hoy = date('Y-m-d'); 
     if($data){ 
      $d = intval($data); 
      $beds = Bed::where('room', '=', $d)->get(array('size', 'room', 'id', 'name')); 

      foreach($beds as $b){ 
       $b->stay = Stay::where('bed', '=', $b->id) 
          ->where('indate', '<=', $hoy) 
          ->where('outdate', '>=', $hoy) 
          ->get(array('id', 'room', 'bed', 'guest', 'booking', 'indate', 'outdate')); 

          dd($b->stay); 
       if(isset($b->stay->guest)){ 
        $b->stay->huesped = Guest::find($b->stay->guest); 
       }else{} 

       if(isset($b->stay->booking)){ 
        $b->stay->huesped = Booking::find($b->stay->booking); 
       } 

       //dd($b->stay); 

       array_push($retorno, $b); 
      } 
      //$room = Room::find($d); 
      //return $room->camas()->get('size', 'room', 'id'); 
      //$beds = Bed::where('room', $data)->get(); 
     }else{ 
      $beds = Bed::where('hotel', '=', $user->hostel)->get(array('size', 'room', 'id', 'name')); 

      foreach($beds as $b){ 
       $be = $b['attributes']; 
       $st = array(); 
       $stay = Stay::where('bed', '=', $b->id) 
          ->where('indate', '<=', $hoy) 
          ->where('outdate', '>=', $hoy) 
          ->get(array('id', 'room', 'bed', 'guest', 'booking', 'indate', 'outdate')); 
          //return $stay[0]; 

       $st = $stay[0]; 
          //dd($stay[0]); 
       if(isset($stay[0])){ 
        if($stay[0]['attributes']['guest'] > 0){ 
         $be['huesped'] = Guest::find($b->stay->guest); 
        }else{} 

        if($stay[0]['attributes']['booking']){ 
         $be['reserva'] = Booking::find($b->stay->booking); 
        } 
        $be['stay'] = $st; 
       } 
       array_push($retorno, $be); 
       $be = array(); 
      } 

     } 
     return $retorno; 

    } 

Więc, kiedy zadzwonić do mysiteapp.local/łóżek, powinienem wrócić kompozytowego tablicę z danymi łóżku i, jeśli to zastrzeżenie, lub jeśli na łóżko zajęte w tej chwili, informacje o pobycie i informacje o gościu.

Ale mam jest komunikat o błędzie kompilacji mówię:

error:{type: "ErrorException", message: "Undefined offset: 0",…} 
file:"/home/pablo/htdocs/pbertran/angularh/api/vendor/laravel/framework/src/Illuminate/Support/Collection.php" 
line:788 
message:"Undefined offset: 0" 
type:"ErrorException" 

Zostały googling wokół, ale nie mógł znaleźć żadnego rozwiązania. Jakieś pomysły?

Z góry dziękuję!

Odpowiedz

6

Przyjmujesz założenia, że ​​indeks jest obecny, co nie zawsze musi mieć miejsce. Twoja logika powinna być bardziej gęsta:

$st = isset($stay[0]) ? $stay[0] : false; 
if ($st){ 
    //now you can use it safely. 
} 
+1

Dziękuję człowieku! Zadziałało – Pablo

Powiązane problemy