Laravel-未定义的偏移量0-collection.php
我正在编写一个网络应用程序。后端是基于Laravel 4的RESTFul应用程序 我和某个控制器有问题 BedsController.phpLaravel-未定义的偏移量0-collection.php,php,laravel-4,Php,Laravel 4,我正在编写一个网络应用程序。后端是基于Laravel 4的RESTFul应用程序 我和某个控制器有问题 BedsController.php class BedsController extends \BaseController { /** * Display a listing of the resource. * GET /beds * * @return Response */ public function index
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;
}
一直在谷歌搜索,但找不到任何解决方案。
有什么想法吗
提前谢谢 您假设该指数存在,但情况可能并非总是如此。你的逻辑应该更准确:
$st = isset($stay[0]) ? $stay[0] : false;
if ($st){
//now you can use it safely.
}
为了添加另一个结果,我对集合进行了“双重”分组:
$price_change->grouped_lines = $price_change->lines->groupBy('PriceListID')->transform(function($item, $k) {
return $item->groupBy('StockID');
});
以上基本上通过2个键对集合进行分组,因此:
$price_change->grouped_lines[PriceListID][StockID]->
我收到了“未定义的偏移量0-collection.php”错误
要修复它,我必须首先对第一个键执行isset(),然后对第二个键执行isset():
if(isset($price_change->grouped_lines[1]) && isset($price_change->grouped_lines[1][23]))
它可以工作,但对我来说,问题是它在本地工作,在服务器上发布后就不能工作了。你能帮我吗?我也遇到了同样的问题,事实上,在某处放置一个if(isset($var))解决了我的问题。错误日志中的最后一个“堆栈跟踪”也指出了确切的位置。
if(isset($price_change->grouped_lines[1]) && isset($price_change->grouped_lines[1][23]))