Level 122
You need a Many to Many relationship since Sale has many Category and Category has many Sale
categories
id |name |
----------------
1 |Category A|
2 |Category B|
3 |Category C|
4 |Category D|
5 |Category E|
sales
id |customer |region |product
-------------------------------------------------
1 James Region A Fruits
2 Billy Region B Vegetable
3 Benny Region C Computer
category_sale pivot table
category_id | sale_id
------------------------
1 | 1
2 | 1
5 | 1
3 | 2
2 | 2
4 | 2
2 | 3
3 | 3
1 | 3
https://laravel.com/docs/7.x/eloquent-relationships#many-to-many
Models
class Category extends Model
{
public function sales ()
{
return $this->belongsToMany('App\Model\Sale');
}
}
class Sale extends Model
{
public function categories ()
{
return $this->belongsToMany('App\Model\Category');
}
}