php - Collection.php 第 1477 行中的 ErrorException:未定义的偏移量:0
问题描述
我面临这个错误:
ErrorException 未定义的偏移量:0
在我的 Laravel 包中的集合文件脚本中。我正在使用 PHP 7.1+。
请用基本的方式向我解释一下。
{
/**
* Zip the collection together with one or more arrays.
*
* e.g. new Collection([1, 2, 3])->zip([4, 5, 6]);
* => [[1, 4], [2, 5], [3, 6]]
*
* @param mixed ...$items
* @return static
*/
public function zip($items)
{
$arrayableItems = array_map(function ($items) {
return $this->getArrayableItems($items);
}, func_get_args());
$params = array_merge([function () {
return new static(func_get_args());
}, $this->items], $arrayableItems);
return new static(call_user_func_array('array_map', $params));
}
/**
* Get the collection of items as a plain array.
*
* @return array
*/
public function toArray()
{
return array_map(function ($value) {
return $value instanceof Arrayable ? $value->toArray() : $value;
}, $this->items);
}
/**
* Convert the object into something JSON serializable.
*
* @return array
*/
public function jsonSerialize()
{
return array_map(function ($value) {
if ($value instanceof JsonSerializable) {
return $value->jsonSerialize();
} elseif ($value instanceof Jsonable) {
return json_decode($value->toJson(), true);
} elseif ($value instanceof Arrayable) {
return $value->toArray();
} else {
return $value;
}
}, $this->items);
}
/**
* Get the collection of items as JSON.
*
* @param int $options
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}
/**
* Get an iterator for the items.
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->items);
}
/**
* Get a CachingIterator instance.
*
* @param int $flags
* @return \CachingIterator
*/
public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
{
return new CachingIterator($this->getIterator(), $flags);
}
/**
* Count the number of items in the collection.
*
* @return int
*/
public function count()
{
return count($this->items);
}
/**
* Get a base Support collection instance from this collection.
*
* @return \Illuminate\Support\Collection
*/
public function toBase()
{
return new self($this);
}
/**
* Determine if an item exists at an offset.
*
* @param mixed $key
* @return bool
*/
public function offsetExists($key)
{
return array_key_exists($key, $this->items);
}
/**
* Get an item at a given offset.
*
* @param mixed $key
* @return mixed
*/
public function offsetGet($key)
{
return $this->items[$key];
}
/**
* Set the item at a given offset.
*
* @param mixed $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, $value)
{
if (is_null($key)) {
$this->items[] = $value;
} else {
$this->items[$key] = $value;
}
}
/**
* Unset the item at a given offset.
*
* @param string $key
* @return void
*/
public function offsetUnset($key)
{
unset($this->items[$key]);
}
/**
* Convert the collection to its string representation.
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
/**
* Results array of items from Collection or Arrayable.
*
* @param mixed $items
* @return array
*/
protected function getArrayableItems($items)
{
if (is_array($items)) {
return $items;
} elseif ($items instanceof self) {
return $items->all();
} elseif ($items instanceof Arrayable) {
return $items->toArray();
} elseif ($items instanceof Jsonable) {
return json_decode($items->toJson(), true);
} elseif ($items instanceof JsonSerializable) {
return $items->jsonSerialize();
} elseif ($items instanceof Traversable) {
return iterator_to_array($items);
}
return (array) $items;
}
/**
* Add a method to the list of proxied methods.
*
* @param string $method
* @return void
*/
public static function proxy($method)
{
static::$proxies[] = $method;
}
/**
* Dynamically access collection proxies.
*
* @param string $key
* @return mixed
*
* @throws \Exception
*/
public function __get($key)
{
if (! in_array($key, static::$proxies)) {
throw new Exception("Property [{$key}] does not exist on this collection instance.");
}
return new HigherOrderCollectionProxy($this, $key);
}
}
这是第 1475 和 1477 行:
public function offsetGet($key)
{
return $this->items[$key];
我的控制器视图代码也是
<?php
$sno=0;
foreach ($booking as $viewbook) {
$sno++;
$booking_time=$viewbook->booking_time;
if($booking_time>12)
{
$final_time=$booking_time-12;
$final_time=$final_time."PM";
}else{
$final_time=$booking_time."AM";
}
$ser_id=$viewbook->services_id;
$sel=explode("," , $ser_id);
$lev=count($sel);
$ser_name="";
$sum="";
$price="";
for($i=0;$i<$lev;$i++)
{
$id=$sel[$i];
$fet1 = DB::table('subservices')->where('subid', '=', $id)->get();
$ser_name.=$fet1[0]->subname.'<br>';
$ser_name.=",";
$ser_name=trim($ser_name,",");
}
$bookid= $viewbook->book_id;
$newbook = DB::table('booking')->where('book_id', '=', $bookid)->get();
?>
<tr>
<td><?php echo $sno; ?></td>
<td><?php echo $viewbook->shop_name;?></td>
<td><?php echo $ser_name;?></td>
<td><?php echo $viewbook->booking_date;?></td>
<td><?php echo $final_time;?></td>
<td><?php echo $viewbook->phone;?></td>
<td><?php echo $viewbook->name;?></td>
<td><?php echo $viewbook->user_email;?></td>
<td><?php echo $viewbook->booking_address;?></td>
<td><?php echo $viewbook->booking_city;?></td>
<td><?php echo $viewbook->booking_pincode;?></td>
<td><?php echo $viewbook->total_amt.' '.$setting[0]->site_currency;?></td>
<td><?php echo $viewbook->payment_mode;?></td>
<?php if($newbook[0]->status=="pending"){ $color="#FB6704"; } else if($newbook[0]->status=="paid") { $color="#0DE50D"; }?>
<td style="color:<?php echo $color;?>;"><?php echo $newbook[0]->status;?></td>
<td>
<?php if(config('global.demosite')=="yes"){?>
<a href="#" class="btn btn-danger btndisable">Delete</a> <span class="disabletxt">( <?php echo config('global.demotxt');?> )</span>
<?php } else { ?>
<a href="<?php echo $url;?>/admin/booking/{{ $viewbook->book_id }}" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this?')">Delete</a>
<?php } ?>
解决方案
做一个 dd($yourcollection->toArray());
检查您是否有在构造函数或视图中尝试访问的密钥。
所以最好发布控制器/视图代码而不是 collection.php :)
附上您的所有 $variable[0] 示例 $fet1[0]
与@if(isset($fet1[0])) $fet1[0]->name; @万一
将 if($newbook[0]->status=="pending") 更改为 if(isset($newbook[0]) && $newbook[0]->status=="pending")
我认为您应该将数据库查询移至控制器。
推荐阅读
- java - 处理来自 getMessage() 的消息异常
- sql - 找出最长的无雨时间(每小时,超过几天)
- python - 明亮物体周围的轮廓拟合
- javascript - 在leaflet-geoman中绘制圆时,有什么方法可以限制圆的大小?
- wpf - 低 DPI 图像未在 WPF 图像中完全显示
- ios - iOS WidgetKit - 更新占位符视图时未更新小部件预览屏幕
- php - 为 Laravel 7 在表单中创建默认值
- javascript - 如何停止加载特定的 HTML 元素?(需要根据浏览设备加载视频)
- ios - Swift - 如何知道文件是否成功保存在 .cachesDirectory
- linux - 通过 shell 脚本将“原始”命令行添加到 bashrc(不评估命令)