A
A
Arseniy Togulev2019-02-05 17:17:59
Laravel
Arseniy Togulev, 2019-02-05 17:17:59

Query with relationships in Eloquent: Relationships how to compose?

There are 3 models Category, Product, Product Attributes

class Category extends Model
{
/**
     * Category has many Products.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
public function products()
    {
        return $this->belongsToMany(Product::class)->withTimestamps();
    }
}

class Product extends Model
{ /**
     * Product has many Categories.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function categories()
    {
        return $this->belongsToMany(Category::class)->withTimestamps();
    }
}

class Attribute extends Model
{
/**
   * Attribute has many Products.
   *
   * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
   */
  public function products()
    {
        return $this->belongsToMany(Product::class)->withTimestamps();
    }
}

The task is as follows:
In the category controller, you need to display products and their associated attributes
. The controller now looks like this:
class CatalogController extends Controller
{


    /**
     * Display the specified resource.
     *
     * @param  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $products=Category::with('products', 'attributes')->where('id', $id)->orWhere('slug', $id)->first();
        //dd($products);
        return view('catalog.index')->with('products', $products);
    }


}

The error is that attributes are not related in any way to Category::class
Category::class is related to Product::class through pivot table category_product
and, accordingly, Product::class is related to Attribute::class through attribute_product,
respectively, the question is:
how to correctly make a request in the controller ?
Or do I need to add something to the model so that the Category::class - Attribute::class relationship appears?
if needed what?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arseniy Togulev, 2019-02-06
@tetra

Alexey pointed to the documentation but did not take into account that I have connections through a pivot table and the solution is not suitable for this case
. There is a package that basically solves this problem, but I did it in a simple way:

/**
     * Display the specified resource.
     *
     * @param  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data=Category::with('products')->where('id', $id)->orWhere('slug', $id)->first();
        $data->products->map(function ($product) {
        $p = Product::find($product->id);
        $sizes=[];
        foreach ($p->attributes as $a) {
          $sizes[]=$a;
        }
        $product->sizes = $sizes;
        return $product;
    });

      return view('catalog.index')->with('products', $data);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question