0

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)

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

one

Any idea?

Update

In case you need to see how BarcodeResource resource looks like here it is:

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,
        ];
}
1
  • I do not want to poke an open wound, but this is why it is so important not to do $variable = xxx; return $variable; and just do return xxxx; every time. There is no sense in following a code style like that, it is just prone to this type of issue, and you are adding 2 lines of code (new line + return) just because. Again, just leaving this for anyone reading this in the future. I am so against the code style you are using (just the return thing) because I have seen this issue millions of times, and this is perfect proof of why not follow that style. Commented Sep 18, 2023 at 8:46

1 Answer 1

2

I would say you simply forgot the return statement in your 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,
    ];
    return $arrayNull; // this is missing
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's absolutely right but somehow product data isn't include barcode resource! screenshot ibb.co/sPyL2bk
the issue was from controller i needed to add barcode relation in with() as well. Thank you. ->with(['outlet', 'barcode', 'barcode.product', 'user'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.