Laravel-未定义的偏移量0-collection.php [英] Laravel - Undefined offset 0 - collection.php

查看:67
本文介绍了Laravel-未定义的偏移量0-collection.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编程一个Web应用程序.后端是一个基于Laravel 4的RESTFul应用.

I'm programming a web app. The backend is a RESTFul app based in Laravel 4.

我在使用特定控制器时遇到问题.

I'm having problems with a particular controller.

BedsController.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/beds时,我应该返回一个包含床数据的复合数组,如果有预订,或者如果该床目前被占用,则住宿信息和客人的信息

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.

但是我得到的只是一个编译错误消息,说:'

But all I get is a compiling error message sayin':

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"

一直在搜索,但是找不到任何解决方案.有什么想法吗?

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

提前谢谢!

推荐答案

您假设存在该索引,但情况并非总是如此.您的逻辑应该更简洁:

You make assumptions about that index being present, which may not always be the case. Your logic should be more thussly:

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

这篇关于Laravel-未定义的偏移量0-collection.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆