Laravel - Undefined offset 0 - collection.php
<p>I'm programming a web app. The backend is a RESTFul app based in Laravel 4.</p>

<p>I'm having problems with a particular controller.</p>

<p>BedsController.php</p>

<pre><code>class BedsController extends \BaseController {

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

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

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

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

                //dd( $b-&gt;stay );

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

            foreach( $beds as $b ){
                $be = $b['attributes'];
                $st = array();
                $stay = Stay::where('bed', '=', $b-&gt;id )
                            -&gt;where('indate', '&lt;=', $hoy )
                            -&gt;where('outdate', '&gt;=', $hoy )
                            -&gt;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'] &gt; 0 ){
                        $be['huesped'] = Guest::find( $b-&gt;stay-&gt;guest );
                    }else{}

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

        }
        return $retorno;

    }
</code></pre>

<p>So, when I make a call to mysiteapp.local/beds, I should get back a composite array with the bed's data and, if there's a reservation, or if that bed's occupied at the moment, the stay info and guest's info.</p>

<p>But all I get is a compiling error message sayin':</p>

<pre><code>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"
</code></pre>

<p>Have been googling around, but couldn't find any solution.
Any ideas?</p>

<p>Thanks in advance!</p>