2323 * @OA\Property(property="product_image_id", type="string", example=1),
2424 * @OA\Property(property="is_location_offer", type="boolean", example=1),
2525 * @OA\Property(property="is_rental", type="boolean", example=0),
26+ * @OA\Property(property="co2_rating", type="string", enum={"A", "B", "C", "D", "E"}, example="B"),
2627 * }
2728 * )
2829 *
3839 * @OA\Property(property="is_location_offer", type="boolean", example=1),
3940 * @OA\Property(property="is_rental", type="boolean", example=0),
4041 * @OA\Property(property="in_stock", type="boolean", example=0),
42+ * @OA\Property(property="co2_rating", type="string", enum={"A", "B", "C", "D", "E"}, example="B"),
43+ * @OA\Property(property="is_eco_friendly", type="boolean", example=true),
4144 * @OA\Property(property="brand", ref="#/components/schemas/BrandResponse"),
4245 * @OA\Property(property="category", ref="#/components/schemas/CategoryResponse"),
4346 * @OA\Property(property="product_image", ref="#/components/schemas/ImageResponse")
@@ -49,9 +52,9 @@ class Product extends BaseModel
4952 use HasFactory, FilterQueryString, HasUlids;
5053
5154 protected $ table = 'products ' ;
52- protected $ fillable = ['name ' , 'description ' , 'category_id ' , 'brand_id ' , 'price ' , 'product_image_id ' , 'is_location_offer ' , 'is_rental ' , 'stock ' ];
55+ protected $ fillable = ['name ' , 'description ' , 'category_id ' , 'brand_id ' , 'price ' , 'product_image_id ' , 'is_location_offer ' , 'is_rental ' , 'stock ' , ' co2_rating ' ];
5356 protected $ hidden = ['brand_id ' , 'category_id ' , 'product_image_id ' , 'stock ' , 'created_at ' , 'updated_at ' ];
54- protected $ appends = ['in_stock ' ];
57+ protected $ appends = ['in_stock ' , ' is_eco_friendly ' ];
5558 protected $ filters = ['between ' , 'sort ' ];
5659
5760 protected $ casts = array (
@@ -87,4 +90,24 @@ public function getInStockAttribute()
8790 return $ this ->stock > 0 ;
8891 }
8992
93+ public function getIsEcoFriendlyAttribute ()
94+ {
95+ return in_array (strtoupper ($ this ->co2_rating ?? '' ), ['A ' , 'B ' ]);
96+ }
97+
98+ public function scopeEcoFriendly ($ query )
99+ {
100+ return $ query ->whereIn ('co2_rating ' , ['A ' , 'B ' , 'a ' , 'b ' ]);
101+ }
102+
103+ public function scopeWithFilters ($ query , array $ filters )
104+ {
105+ return $ query ->when ($ filters ['eco_friendly ' ] ?? null , function ($ q , $ ecoFriendly ) {
106+ if ($ ecoFriendly == '1 ' || $ ecoFriendly === true || $ ecoFriendly === 'true ' ) {
107+ return $ q ->ecoFriendly ();
108+ }
109+ return $ q ;
110+ });
111+ }
112+
90113}
0 commit comments