Yeni bir ürün eklendiğinde bu ürün için gerekli olabilecek ilgili ürünleri kullanıcıya önerme işlemi için aşağıdaki gibi bir yol izlenebilir.
- Ürün eklenir.
- Gerekli ürünler listesi oluşturulur ve sonrasında ürünler bu listeye eklenir.
- Oluşturulan yada var olan ilişkili ürün listesi ana ürüne tanımlanır.
Products:
| id | name |
|---|---|
| 1 | Ev Tipi Matkap |
| 2 | 5mm Matkap Ucu |
| 3 | 10mm Matkap Ucu |
| 4 | İş Gözlüğü |
| 5 | İş İş Eldiveni |
| 6 | Masa Avizesi |
| 7 | Kırmızı Avize Ampülü |
| 8 | Sanayi Tipi Matkap |
Product Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function requirementList()
{
return $this->belongsToMany(Requirement::class, 'product_requirement_relation', 'requirement_id', 'product_id')
->withTimestamps();
}
}Category:
| id | name |
|---|---|
| 1 | Yapı Malzemeleri |
Category Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
}Requirements
| id | name | category_id |
|---|---|---|
| 1 | Matkap için ilgili ürünler | 1 |
Requirement Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Requirement extends Model
{
protected $fillable = ['name', 'category_id'];
public function products()
{
return $this->belongsToMany(Product::class, 'require_product', 'require_id', 'product_id')
->withTimestamps();
}
}Requirement List (pivot)
| id | require_id | product_id |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 4 |
| 4 | 1 | 5 |
Product Requirement Relation (pivot)
| id | requirement_id | product_id |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1 | 8 |
Aşağıdaki sorgu ile id'i 1 olan bir ürünün tüm requirement listesini ürünleriyl birlikte alalım.
<?php
App\Product::with('requirementList.products')->find(1)Sorgu sonucu:
Product {#156 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:4 [▶]
#original: array:4 [▶]
#relations: array:1 [▼
"requirementList" => Collection {#160 ▼
#items: array:1 [▼
0 => Requirement {#163 ▼
#fillable: array:2 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:5 [▶]
#original: array:9 [▶]
#relations: array:2 [▼
"pivot" => Pivot {#162 ▶}
"products" => Collection {#178 ▼
#items: array:4 [▼
0 => Product {#174 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:4 [▶]
#original: array:8 [▼
"id" => 2
"name" => "Sibyl Orn"
"created_at" => "2016-07-17 17:43:06"
"updated_at" => "2016-07-17 17:43:06"
"pivot_require_id" => 1
"pivot_product_id" => 2
"pivot_created_at" => "2016-07-17 18:07:13"
"pivot_updated_at" => "2016-07-17 18:07:13"
]
#relations: array:1 [▼
"pivot" => Pivot {#173 ▶}
]
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => Product {#175 ▶}
2 => Product {#176 ▶}
3 => Product {#177 ▶}
]
}
]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
]
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}