A
A
Analka2020-12-04 09:18:57
Laravel
Analka, 2020-12-04 09:18:57

Correctly compose relations in Laravel?

There is a model

Product
    {
      return $this->hasOne(ProductDescription::class,'product_id','product_id')->where('language_id','=', 1);
    }

Model
ProductDescription

Model
Order

public function products()
    {
      return $this->hasMany(OrderProduct::class,'order_id');
    }


Model
OrderProduct

public function description()
    {
      return $this->hasOne(ProductDescription::class,'product_id','product_id')->where('language_id','=', 1);
    }

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


How to arrange links correctly in order to display user orders in personal account and products for each order?

now I did like this, but some kind of garbage came out

$orders = Order::select(
              'orders.*',
              )
              ->with(['products' => function($query){
                $query->with(['description','product']);
              }])
              ->where('user_id','=', Auth::user()->id)
              ->paginate(30);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sanes, 2020-12-04
@Sanes

Model name must be singular

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question