Php Laravel管理不同的API响应 解释

Php Laravel管理不同的API响应 解释,php,laravel,eloquent,eager-loading,Php,Laravel,Eloquent,Eager Loading,用户使用条形码模型(可以是物品、包装或库存货架)扫描条形码和系统响应 条形码资源基于条形码可编类别解析条形码可编资源。每个条形码模型返回不同的JSON资源 // BarcodeResource.php $modelResource = app()->makeWith(__NAMESPACE__ . '\\' . class_basename($this->barcodable) . 'Resource', [ 'resource' => $this->barco

用户使用条形码模型(可以是物品、包装或库存货架)扫描条形码和系统响应

条形码资源基于条形码可编类别解析条形码可编资源。每个条形码模型返回不同的JSON资源

// BarcodeResource.php

$modelResource = app()->makeWith(__NAMESPACE__ . '\\' . class_basename($this->barcodable) . 'Resource', [
    'resource' => $this->barcodable
]);

return [
    'code' => $this->code,
    'model_type' => class_basename($this->barcodable),
    'model_data' => $modelResource
];
万一

  • 。。。文章,我想打印包含此类文章的软件包
  • 。。。包装,我想打印位置(库存货架),包括物品和子包装
  • 。。。库存货架,我想打印所有的包裹
问题 我想用递归资源防止无限循环。

Article
  >> Package
      >> Article (infinity loop begins because package resource 
                  returns articles in spesific package)

Package
  >> Article
      >> Package (loop...)
  >> Inventory Shelf
      >> Package (loop...)
  >> Child package


Inventory Shelf
  >> Package
      >> Article
      >> Inventory Shelf (loop...)
      >> Child package
急切的加载和不稳定的关系应该是一种解决方案,但我如何才能在正确的阶段不稳定这些关系呢?对于一个资源,还是应该创建多个资源(递归/正常)


尝试 包含关系的额外属性 我尝试了这个解决方案,但神奇的是,
$this->relations
属性在两次递归后变为整数1

class PackageResource extends JsonResource
{
    private $relations;

    public function __construct($resource, array $relations = [])
    {
        parent::__construct($resource);
        $this->relations = $relations;
    }

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'articles' => $this->when(in_array('articles', $this->relations), ArticleResource::collection($this->articles, $this->relations)),
            'children' => PackageResource::collection($this->children, $this->relations),
        ];
    }

对于类似情况,我的解决方案如下: 在资源文件中,我总是根据请求属性
返回关系。本申请随附如下: 我需要具有
订单
配置文件
用户
,但我还需要订单的
区域
,而请求是这样的:

http://example.com/api/v1/user/234?with=user.orders,user.profile,orders.area
在资源文件中,还有类似的内容:

公共功能待命($request)
{
$return=[
'id'=>this->id,
“名称”=>$this->name,
'email'=>$this->email,
“位置”=>$this->location,
'active'=>this->isActive(),
“级别”=>$this->level,
];
如果($request->has('with')){
$relationships=[
“订单”=>[OrderCollection::class,“订单”],
'area'=>[area::class,'area','area.admin'],
'profile'=>[UserProfile::class'profile'],
];
$with=分解(“,”,$request->with);
foreach($key=>$relationship的关系){
if(在_数组中(“用户”。$key,$with)){
$return[$key]=新的$relationship[0]($this->{$relationship[1]});
}
}
}
$return['created']=$this->created_at->toDateTimeString();
return$return;
}
另一种解决方案是向资源类添加额外属性:

protected$with=”“;
公共函数u_构造(混合$resource,$,with=“”)
{
父项::_构造($resource);
}
然后,当您调用该资源时,可以使用前面的方法对其进行过滤。我刚测试过,它对我有效


希望有帮助。

谢谢你的回答。我不想在URL中提供任何额外的参数,因为无论返回的条形码模型是什么,请求都应该完全相同。我试图从控制器向资源注入额外属性,但尚未使其工作。您无法插入额外属性?是的,我插入了,但当我将其转发到另一个资源时,它将更改为值整数1(在包含关系的字符串之前)。因此,原始属性值不知何故消失了。由于某种原因,当我尝试向前传递param时,它不起作用:
$return[$key]=new$relationship[0]($this->{$relationship[1]},$with)因为属性更改为整数值1。我不明白为什么会这样……您应该使用命令
php-artisan-make:resource-UserCollection
创建资源控制器,并将它们与
新资源控制器(模型)
样式一起使用。您失去了关系,因为您将其传递给集合静态方法,而不是ResourceController的构造函数。与尝试相关:丢失关系,因为该关系应通过构造函数传递,但通过集合方法传递。您还应覆盖集合方法。
class PackageResource extends JsonResource
{
    private $relations;

    public function __construct($resource, array $relations = [])
    {
        parent::__construct($resource);
        $this->relations = $relations;
    }

    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'articles' => $this->when(in_array('articles', $this->relations), ArticleResource::collection($this->articles, $this->relations)),
            'children' => PackageResource::collection($this->children, $this->relations),
        ];
    }