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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community