5
5
use Illuminate \Database \Query \Builder ;
6
6
use Illuminate \Support \Facades \DB ;
7
7
use Webkul \DataGrid \DataGrid ;
8
+ use Webkul \Tag \Repositories \TagRepository ;
8
9
9
10
class ProductDataGrid extends DataGrid
10
11
{
@@ -17,15 +18,18 @@ public function prepareQueryBuilder(): Builder
17
18
18
19
$ queryBuilder = DB ::table ('products ' )
19
20
->leftJoin ('product_inventories ' , 'products.id ' , '= ' , 'product_inventories.product_id ' )
21
+ ->leftJoin ('product_tags ' , 'products.id ' , '= ' , 'product_tags.product_id ' )
22
+ ->leftJoin ('tags ' , 'tags.id ' , '= ' , 'product_tags.tag_id ' )
20
23
->select (
21
24
'products.id ' ,
22
25
'products.sku ' ,
23
26
'products.name ' ,
24
27
'products.price ' ,
28
+ 'tags.name as tag_name ' ,
25
29
)
26
- ->addSelect (DB ::raw ('SUM( ' . $ tablePrefix . ' product_inventories.in_stock) as total_in_stock ' ))
27
- ->addSelect (DB ::raw ('SUM( ' . $ tablePrefix . ' product_inventories.allocated) as total_allocated ' ))
28
- ->addSelect (DB ::raw ('SUM( ' . $ tablePrefix . ' product_inventories.in_stock - ' . $ tablePrefix . ' product_inventories.allocated) as total_on_hand ' ))
30
+ ->addSelect (DB ::raw ('SUM(product_inventories.in_stock) as total_in_stock ' ))
31
+ ->addSelect (DB ::raw ('SUM(product_inventories.allocated) as total_allocated ' ))
32
+ ->addSelect (DB ::raw ('SUM(product_inventories.in_stock - product_inventories.allocated) as total_on_hand ' ))
29
33
->groupBy ('products.id ' );
30
34
31
35
if (request ()->route ('id ' )) {
@@ -36,6 +40,7 @@ public function prepareQueryBuilder(): Builder
36
40
$ this ->addFilter ('total_in_stock ' , DB ::raw ('SUM( ' .$ tablePrefix .'product_inventories.in_stock ' ));
37
41
$ this ->addFilter ('total_allocated ' , DB ::raw ('SUM( ' .$ tablePrefix .'product_inventories.allocated ' ));
38
42
$ this ->addFilter ('total_on_hand ' , DB ::raw ('SUM( ' .$ tablePrefix .'product_inventories.in_stock - ' .$ tablePrefix .'product_inventories.allocated ' ));
43
+ $ this ->addFilter ('tag_name ' , 'tags.name ' );
39
44
40
45
return $ queryBuilder ;
41
46
}
@@ -93,6 +98,24 @@ public function prepareColumns(): void
93
98
'type ' => 'string ' ,
94
99
'sortable ' => true ,
95
100
]);
101
+
102
+ $ this ->addColumn ([
103
+ 'index ' => 'tag_name ' ,
104
+ 'label ' => trans ('admin::app.products.index.datagrid.tag-name ' ),
105
+ 'type ' => 'string ' ,
106
+ 'searchable ' => false ,
107
+ 'sortable ' => true ,
108
+ 'filterable ' => true ,
109
+ 'filterable_type ' => 'searchable_dropdown ' ,
110
+ 'closure ' => fn ($ row ) => $ row ->tag_name ?? '-- ' ,
111
+ 'filterable_options ' => [
112
+ 'repository ' => TagRepository::class,
113
+ 'column ' => [
114
+ 'label ' => 'name ' ,
115
+ 'value ' => 'name ' ,
116
+ ],
117
+ ],
118
+ ]);
96
119
}
97
120
98
121
/**
0 commit comments