diff --git a/app/Livewire/Product/Create.php b/app/Livewire/Product/Create.php index 1638439..3eb6962 100644 --- a/app/Livewire/Product/Create.php +++ b/app/Livewire/Product/Create.php @@ -34,7 +34,7 @@ public function create() $product = Product::create([ 'name' => $this->name, 'description' => $this->description, - 'price' => $this->price, + 'price' => $this->price * 100, 'image' => $image, ]); diff --git a/app/Livewire/Product/Show.php b/app/Livewire/Product/Show.php index 637def8..b29b4eb 100644 --- a/app/Livewire/Product/Show.php +++ b/app/Livewire/Product/Show.php @@ -3,12 +3,65 @@ namespace App\Livewire\Product; use App\Models\Product; +use Illuminate\Support\Number; use Illuminate\View\View; +use Livewire\Attributes\Validate; use Livewire\Component; +use Livewire\WithFileUploads; class Show extends Component { public Product $product; + use WithFileUploads; + + #[Validate('required|string|max:255')] + public string $name = ''; + + #[Validate('required')] + public string $description = ''; + + #[Validate('required')] + public string $price = ''; + + #[Validate('nullable|image|max:1024')] + public $image = ''; + + public function mount() + { + $this->name = $this->product->name; + $this->description = $this->product->description; + $this->price = $this->product->price / 100; + } + + public function update() + { + $this->validate(); + + try { + $image = $this->image ? $this->image->store('products', 'public') : $this->product->image; + + $this->product->update([ + 'name' => $this->name, + 'description' => $this->description, + 'price' => $this->price * 100, + 'image' => $image, + ]); + + session()->flash('flash.type', 'success'); + session()->flash('flash.title', $this->name.' Updated'); + session()->flash('flash.message', 'Product has been successfully updated.'); + + return redirect()->route('products.show', $this->product); + } catch (\Exception $e) { + $this->addError('name', $e->getMessage()); + + session()->flash('flash.type', 'error'); + session()->flash('flash.title', 'Product Update Failed'); + session()->flash('flash.message', 'There was an error updating the product.'); + + return redirect()->back(); + } + } public function render(): View { diff --git a/app/Models/Product.php b/app/Models/Product.php index 53ad6fe..af29eb8 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -2,6 +2,7 @@ namespace App\Models; +use Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -24,7 +25,7 @@ class Product extends Model ]; protected $casts = [ - 'price' => 'float', + 'price' => 'integer', ]; public function user(): BelongsTo @@ -45,8 +46,8 @@ protected static function boot() }); } - public function getPriceAttribute(): string + public function getFormattedPriceAttribute(): string { - return Number::currency($this->attributes['price']); + return number_format($this->price / 100, 2); } } diff --git a/database/migrations/2024_04_24_013252_create_products_table.php b/database/migrations/2024_04_24_013252_create_products_table.php index fda5149..8c8358a 100644 --- a/database/migrations/2024_04_24_013252_create_products_table.php +++ b/database/migrations/2024_04_24_013252_create_products_table.php @@ -16,7 +16,7 @@ public function up(): void $table->string('name'); $table->string('slug'); $table->string('description'); - $table->string('price'); + $table->integer('price'); $table->string('image')->nullable(); $table->softDeletes(); $table->timestamps(); diff --git a/resources/views/livewire/product/create.blade.php b/resources/views/livewire/product/create.blade.php index 6da26e5..8ac8383 100644 --- a/resources/views/livewire/product/create.blade.php +++ b/resources/views/livewire/product/create.blade.php @@ -18,7 +18,12 @@