简体   繁体   English

Laravel 资源返回空数组

[英]Laravel resource return empty array

I have resource where i get product data trough third table but having hard time make relationships work on models so it return empty array.我有资源,我通过第三个表获取产品数据,但很难使关系在模型上起作用,因此它返回空数组。

Logic逻辑

  1. Product has many barcodes产品有很多条码
  2. Barcodes can have (belongsTo) damage条码可能有(属于)损坏
  3. In damage we get product trough barcode table (we store barcode_id )在损坏中,我们通过条形码表获得产品槽(我们存储barcode_id

I also included fillable part of each column so you can see columns in database.我还包括了每列的可填充部分,以便您可以查看数据库中的列。

Code代码

Product model

class Product extends Model
{

    protected $fillable = [
        'name', 'slug', 'stock', 'cover', 'description', 'sku', 'price', 'discount',
    ];

    public function barcodes()
    {
        return $this->hasMany(Barcode::class);
    }
}

Barcode model

class Barcode extends Model
{
    protected $fillable = [
        'product_id', 'sku', 'serial_number', 'price', 'discount',
    ];

    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    public function damages()
    {
        return $this->hasMany(DamageProduct::class);
    }
}

DamageProduct model

class DamageProduct extends Model
{
    protected $fillable = [
        'outlet_id', 'user_id', 'barcode_id', 'description',
    ];

    public function barcode()
    {
        return $this->belongsTo(Barcode::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}

DamageProductsResource resource

class DamageProductsResource extends JsonResource
{
    public function toArray($request)
    {
        $arrayData = [
            'id' => $this->id,
            'outlet' => new OutletsResource($this->whenLoaded('outlet')),
            'user' => new usersResource($this->whenLoaded('user')),
            'barcode' => new BarcodeResource($this->whenLoaded('barcode')),
            'description' => $this->description,
        ];

        return $arrayData;
    }
}

Result

一

Any idea?任何想法?

Update更新

In case you need to see how BarcodeResource resource looks like here it is:如果您需要查看BarcodeResource资源的样子,请参见此处:

public function toArray($request)
{
        $arrayNull = [
            'id' => $this->id,
            'product' => new ProductsResource($this->whenLoaded('product')),
            'sku' => $this->sku,
            'serial_number' =>  $this->serial_number ? (Int) $this->serial_number : null,
            'price' => (Int) $this->price,
            'discount' => $this->discount ? (Int) $this->discount : null,
        ];
}

I would say you simply forgot the return statement in your BarcodeResource我会说您只是忘记了BarcodeResource中的return语句

public function toArray($request)
{
    $arrayNull = [
        'id' => $this->id,
        'product' => new ProductsResource($this->whenLoaded('product')),
        'sku' => $this->sku,
        'serial_number' =>  $this->serial_number ? (Int) $this->serial_number : null,
        'price' => (Int) $this->price,
        'discount' => $this->discount ? (Int) $this->discount : null,
    ];
    return $arrayNull; // this is missing
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM