Код: Выделить всё
products
Код: Выделить всё
date
каждый товар может иметь множество пользовательских деталей, поэтому связь между ними — один ко многим.
теперь я хочу разрешить клиенту выполнять расширенный поиск на основе сведений о продукте, доступных в моей базе данных, а также выполнять некоторую сортировку на основе пользовательских данных
Код: Выделить всё
products table
-------------------------------
|id | name | description |
| 1 | product1 | |
| 2 | product2 | |
| 3 | product3 | |
| 4 | product4 | |
-------------------------------
Код: Выделить всё
custom_details table
-------------------------------------------------------------------------------------
| id | detailable_id | detailable_type | data_type | name | value |
| 1 | 1 | App\Models\Product | number | length | 10 |
| 2 | 1 | App\Models\Product | number | height | 19 |
| 3 | 1 | App\Models\Product | text | factory | usa |
| 4 | 1 | App\Models\Product | date | release_date | 2024-01-01 |
| 5 | 2 | App\Models\Product | number | length | 20 |
| 6 | 2 | App\Models\Product | number | height | 50 |
| 7 | 2 | App\Models\Product | text | factory | india |
| 8 | 2 | App\Models\Product | date | release_date | 2024-02-01 |
| 9 | 3 | App\Models\Product | number | length | 30 |
| 10 | 3 | App\Models\Product | number | height | 33 |
| 11 | 3 | App\Models\Product | text | factory | philippines |
| 12 | 3 | App\Models\Product | date | release_date | 2024-03-01 |
| 13 | 4 | App\Models\Product | number | length | 22 |
| 14 | 4 | App\Models\Product | number | height | 68 |
| 15 | 4 | App\Models\Product | text | factory | hawai |
| 16 | 4 | App\Models\Product | date | release_date | 2024-04-01 |
-------------------------------------------------------------------------------------
каждый тип данных моего custom_detail имеет собственную реализацию фильтра . ниже приведен пример фильтра, который я получаю от клиентов
Код: Выделить всё
'filters' => [
[
'name' => 'length',
'type' => 'number',
'min' => 15,
'max' => 30,
'order' => 'asc',
],
[
'name' => 'factory',
'type' => 'text',
'values' => ['usa', 'hawai'],
],
[
'name' => 'release_date',
'type' => 'date',
'min' => '2024-02-01',
'max' => '2024-05-06',
],
],
Код: Выделить всё
$query
->select("products.*")
->with(['customDetails', 'category'])
->join('custom_details', function ($query) use ($filters) {
$query->on('products.id', '=', 'custom_details.detailable_id')
->where('custom_details.detailable_type', '=', 'App\Models\Product');
if (count($filters) > 0) {
$query->where(function ($query) use ($filters) {
foreach ($filters as $key => $filter) {
$query->orWhere(function ($query) use ($filter) {
$query->where('custom_details.name', $filter['name'])
->where('custom_details.type', $filter['type']);
$query->where(function ($query) use ($filter) {
switch ($filter['type']) {
case 'text':
if (isset($filter['values']) && $filter['values']) {
$query->whereIn('custom_details.value', $filter['values']);
}
break;
case 'date':
if (isset($filter['min']) && $filter['min']) {
$query->whereDate('custom_details.value', '>=' ,$filter['min']);
}
if (isset($filter['max']) && $filter['max']) {
$query->whereDate('custom_details.value', '=' ,$filter['min']);
}
if (isset($filter['max']) && $filter['max']) {
$query->where('custom_details.value', '
Подробнее здесь: [url]https://stackoverflow.com/questions/78302392/laravel-advanced-search-in-parent-table-using-child-table-values[/url]