Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion database/factories/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lunar\FieldTypes\Text;
use Lunar\Models\Brand;
use Lunar\Enums\ProductStatus;
use Lunar\Models\Product;
use Lunar\Models\ProductType;

Expand All @@ -15,7 +16,7 @@ public function definition(): array
{
return [
'product_type_id' => ProductType::factory(),
'status' => 'published',
'status' => ProductStatus::Published,
'brand_id' => Brand::factory()->create()->id,
'attribute_data' => collect([
'name' => new Text($this->faker->name),
Expand Down
9 changes: 9 additions & 0 deletions src/Enums/ProductStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Lunar\Enums;

enum ProductStatus: string
{
case Published = 'published';
case Draft = 'draft';
}
10 changes: 7 additions & 3 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Lunar\Base\Traits\LogsActivity;
use Lunar\Base\Traits\Searchable;
use Lunar\Database\Factories\ProductFactory;
use Lunar\Enums\ProductStatus;
use Lunar\Jobs\Products\Associations\Associate;
use Lunar\Jobs\Products\Associations\Dissociate;
use Spatie\MediaLibrary\HasMedia as SpatieHasMedia;
Expand All @@ -35,7 +36,7 @@
* @property int $id
* @property ?int $brand_id
* @property int $product_type_id
* @property string $status
* @property \Lunar\Enums\ProductStatus $status
* @property ?\Illuminate\Support\Collection $attribute_data
* @property ?\Illuminate\Support\Carbon $created_at
* @property ?\Illuminate\Support\Carbon $updated_at
Expand Down Expand Up @@ -83,6 +84,7 @@ protected static function newFactory()
*/
protected $casts = [
'attribute_data' => AsAttributeData::class,
'status' => ProductStatus::class,
];

/**
Expand Down Expand Up @@ -189,9 +191,11 @@ public function brand(): BelongsTo
return $this->belongsTo(Brand::modelClass());
}

public function scopeStatus(Builder $query, string $status): Builder
public function scopeStatus(Builder $query, ProductStatus|string $status): Builder
{
return $query->whereStatus($status);
$value = $status instanceof ProductStatus ? $status->value : $status;

return $query->where('status', $value);
}

public function prices(): HasManyThrough
Expand Down
Loading