Collection.php第1477行中的ErrorException:未定义的偏移量:0

时间:2018-11-01 04:24:21

标签: php laravel

我遇到此错误:

  

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 } ?>
						  
					

1 个答案:

答案 0 :(得分:0)

执行dd($ yourcollection-> toArray());

在构造函数或视图中检查是否具有要访问的密钥。

因此最好向您发布控制器/视图代码,而不是collection.php:)

包含所有$ variable [0]示例$ fet1 [0]

带有@if(isset($ fet1 [0]))$ fet1 [0]->名称; @endif

将if($ newbook [0]-> status ==“ pending”)更改为if(isset($ newbook [0])&& $ newbook [0]-> status ==“ pending”)

我认为您应该将数据库查询移至控制器。