Laravel - 未定义的偏移量0 - collection.php

12

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

我遇到了一个特定控制器的问题。

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时,应该返回一个复合数组,其中包含床的数据,以及如果有预订或该床当前被占用,则包括住宿信息和客人信息。

但是我得到的只是一条编译错误消息,如下所示:

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"

一直在Google上搜索,但找不到解决方案。 有什么想法吗?

提前感谢!

2个回答

16

您假设该索引存在,但并非总是如此。应更改逻辑:

您假设该索引存在,但并非总是如此。应更改逻辑:

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

它能工作,但对我来说问题是它在本地工作,在服务器上发布后就无法工作了。你能帮我吗? - Maulik Patel
我曾经遇到过同样的问题,确实在某个地方加上了 if (isset($var)) 就解决了我的问题。错误日志中的最后一个“堆栈跟踪”也清楚地指出了问题所在。 - Terje Nesthus

1
为了添加另一个结果,我对集合进行了“双重”分组:
$price_change->grouped_lines = $price_change->lines->groupBy('PriceListID')->transform(function($item, $k) {
      return $item->groupBy('StockID');
});

上述代码基本上通过2个键对集合进行分组,因此:
$price_change->grouped_lines[PriceListID][StockID]->

我收到了“Undefined offset 0 - collection.php”错误。 为了修复它,我首先必须对第一个键进行isset(),然后再对第二个键进行操作。
if(isset($price_change->grouped_lines[1]) && isset($price_change->grouped_lines[1][23]))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接