Php 数组\u键\u存在()错误/编辑供应商文件
我在heroku部署的laravel API中遇到了一个小问题,这一问题不知从何处开始发生在我身上,没有更新任何内容或进行任何相关更改,当我尝试使用任何有说服力的资源时,它就会发生在我身上,例如:Php 数组\u键\u存在()错误/编辑供应商文件,php,laravel,eloquent,vendor,Php,Laravel,Eloquent,Vendor,我在heroku部署的laravel API中遇到了一个小问题,这一问题不知从何处开始发生在我身上,没有更新任何内容或进行任何相关更改,当我尝试使用任何有说服力的资源时,它就会发生在我身上,例如: $brands = Brand::paginate(15); return BrandResource::collection($brands); 我得到这个错误: array_key_exists():不推荐在对象上使用array_key_exists()。改为使用isset()或属性_exist
$brands = Brand::paginate(15);
return BrandResource::collection($brands);
我得到这个错误:
array_key_exists():不推荐在对象上使用array_key_exists()。改为使用isset()或属性_exists()
在DelegatesToResource.php第49行中
稍微研究一下,进入供应商
中的文件:DelegatesToResource.php
,实际上它使用:
public function offsetExists($offset)
{
return array_key_exists($offset, $this->resource);
}
为了进行测试,我创建了一个新的Laravel项目,事实上它附带了已经更正的行,如下所示:
public function offsetExists($offset)
{
return isset($this->resource[$offset]);
}
class BrandResource extends JsonResource
{
/**
* Determine if the given attribute exists.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->resource[$offset]);
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
class CustomResource extends JsonResource
{
/**
* Determine if the given attribute exists.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->resource[$offset]);
}
}
如果在我的项目中有任何方法可以解决这个问题,我知道我不应该也不能更改供应商
中的文件,所以我的问题是在这种情况下该怎么办
我正在使用Laravel Framework 5.6.39和PHP7.2.18(cli)解决方案1 将更新的代码添加到您的
品牌资源中
,使其看起来像这样:
public function offsetExists($offset)
{
return isset($this->resource[$offset]);
}
class BrandResource extends JsonResource
{
/**
* Determine if the given attribute exists.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->resource[$offset]);
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
class CustomResource extends JsonResource
{
/**
* Determine if the given attribute exists.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->resource[$offset]);
}
}
解决方案2
如果要在多个资源中分页数据,那么最好扩展包含此更新函数的自定义类,而不是直接扩展JsonResource
。
所以看起来是这样的:
public function offsetExists($offset)
{
return isset($this->resource[$offset]);
}
class BrandResource extends JsonResource
{
/**
* Determine if the given attribute exists.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->resource[$offset]);
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
class CustomResource extends JsonResource
{
/**
* Determine if the given attribute exists.
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->resource[$offset]);
}
}
并利用您的资源,如:
class BrandResource extends CustomResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
你知道如何改变它,你也知道。如果您不能或不想更改它,请在任何地方调整错误报告
E_ALL&~E_DEPRECATED
尝试升级您的laravel。您的版本已经1-2年了…另一种方法是在本地下载软件包,并对其进行更改。在composer.json中,您必须在本地引用包。我检查了laravel源代码的github repo中的一些最后提交,它们已经解决了这个问题:公共函数offsetExists($offset)
“{`-return array\u key\u exists($offset,$this->resource);`+return isset($this->resource[$offset]);`}`