2016-05-24 16 views
5

मैं एक वेब ऐप प्रोग्रामिंग कर रहा हूं। बैकएंड एक आरईएसटीफ़ुल ऐप है जो लैरावेल 4 पर आधारित है।लार्वेल - अपरिभाषित ऑफ़सेट 0 - collection.php

मुझे किसी विशेष नियंत्रक के साथ समस्याएं आ रही हैं।

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; 

    } 

तो, जब मैं mysiteapp.local के लिए एक कॉल/बिस्तर, मैं बिस्तर के डेटा के साथ एक समग्र सरणी वापस मिलना चाहिए और, आरक्षण नहीं है, या यदि अगर वह बिस्तर के इस समय, रहने की जानकारी और अतिथि की जानकारी पर कब्जा कर लिया।

लेकिन सभी मैं एक संकलन त्रुटि कह संदेश है:

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" 

आसपास googling किया गया है, लेकिन किसी भी समाधान नहीं मिला। कोई विचार?

अग्रिम धन्यवाद!

उत्तर

6

आप उस सूचकांक के बारे में धारणाएं बनाते हैं, जो हमेशा मामला नहीं हो सकता है। आपका तर्क अधिक जोरदार होना चाहिए:

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

धन्यवाद आदमी! इसने काम कर दिया – Pablo

संबंधित मुद्दे