Why do you have a many-to-many relationship - can a Product be supplied by more than one supplier? And why do you have a Pivot Model?
Apr 20, 2022
11
Level 1
Show data of Table and Pivot Table ( belongsToMany )
I have a table with Suppliers that have products, one Supplier can have many products.
I have 3 tables / models:
- Supplier //data table.
- Products //data table.
- ProductSupplierRelation //pivot table.
In the Supplier Model I have this method :
public function products () {
return $this->belongsToMany(
Product::class ,
ProductSupplierRelation::class ,
'r_supplier_id' ,
'r_product_id'
)
}
In the controller supplier, method show, I want to show one supplier with thier products, is this the correct way ?
class SupplierController extends Controller
{
public function show ( Supplier $supplier ) {
return view( 'supplier.show' , [
'supplier' => $supplier ,
'supplier_products' => $supplier->products
] );
}
}
Please or to participate in this conversation.