12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- class DistProduct extends Model
- {
- use HasFactory;
- protected $table = 'dist_product';
- protected $casts = [
- 'created_at' => 'datetime:Y-m-d H:i:s',
- 'updated_at' => 'datetime:Y-m-d H:i:s',
- 'parameters' => 'json',
- ];
-
- public function distProductCategory()
- {
- return $this->hasOne(DistProductCategory::class, 'id', 'category_id');
- }
-
- public function images()
- {
- return $this->hasMany(DistProductImage::class, 'product_id')->orderBy('order', 'asc')->orderBy('id', 'asc');;
- }
-
- public static function getProduct($productId)
- {
- return self::where('enabled', 1)->where('dist_id', getDistId())->with('images')->find($productId);
- }
-
- public static function getProducts($categoryId = null, $limit = null)
- {
- $query = self::query();
- $query->where('enabled', 1);
- $query->where('dist_id', getDistId());
-
- if ($categoryId) {
- $query->where('category_id', $categoryId);
- }
-
- $query->with(['images', 'distProductCategory']);
-
- $query->orderBy('order', 'desc')
- ->orderBy('id', 'desc');
-
- if ($limit) {
- return $query->limit($limit)->get();
- }
-
- return $query->get();
- }
- }
|