Support the ongoing development of Laravel.io →
posted 1 year ago
Last updated by @agencep 1 year ago.
0
Solution

I found the solution with our artisans on discord

I post it for those who seek !!!

You can create a BesProduct pivot class that has relationships for the country and label models. To do this, you will need to create a new BesProduct model that extends the base pivot model provided by Laravel.

Here's an example of how your BesProduct model might look:

<?php

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;

class BesProduct extends Pivot
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'bes_product';

    /**
     * The relationships that should be touched on save.
     *
     * @var array
     */
    protected $touches = ['bes', 'product'];

    /**
     * Get the bes for the pivot model.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function bes()
    {
        return $this->belongsTo(Bes::class);
    }

    /**
     * Get the product for the pivot model.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    /**
     * Get the country for the pivot model.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function country()
    {
        return $this->belongsTo(Country::class);
    }

    /**
     * Get the label for the pivot model.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function label()
    {
        return $this->belongsTo(Label::class);
    }
}

This model extends the base Pivot model provided by Laravel, and defines two relationships: country and label. These relationships are defined using the belongsTo method, which tells Laravel that the bes_product pivot table has foreign key columns for both the country and label models.

Once you have created this BesProduct model, you can use it in your other models to define the pivot relationship. For example, in your Product model, you can update the products relationship to use the BesProduct model like this:

public function products(){
    return $this->belongsToMany(Product::class, 'bes_product', 'bes_id', 'product_id')
        ->using(BesProduct::class)
        ->withPivot(['country_id', 'label_id', 'total']);
}

This tells Laravel to use the BesProduct model as the pivot model for the products relationship. You will also need to update the Bes model in a similar way.

Once you have done this, you will be able to access the country and label relationships on instances of the BesProduct model. For example, if you have a $product instance, you can access the country and label for the product like this:

$country = $product->pivot->country;
$label = $product->pivot->label;

I hope this helps! Let me know if you have any other questions.

tvbeek, geekasso liked this reply

2
Solution selected by @agencep

Sign in to participate in this thread!

Eventy

Your banner here too?

Sabri Sliti agencep Joined 2 Dec 2022

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.