Skip to content

Commit

Permalink
REFACTOR: 🔨 Use Softdeletes for ProductType and Product and add g…
Browse files Browse the repository at this point in the history
…etPriceAttribute() (#10)

* Dep: Fix the php version

* feat: Add Migration for product

* feat: Add Factory and model for product

* feat: Add relation

* feat: Add Controller Requests and Policy

* feat: Add ProductType model

* fix: Fix relation with Product Type model

* feat: Use Soft delete for Product Model

* fix: Remove duplicated seed of Product and ProductType

* fix: ProductType model

- Add products relationship
- Use `guarded` attributes instead of `fillable`
- Add `getPriceAttribute` method
- Add soft deletes

---------

Co-authored-by: Darián LĂłpez Utra <[email protected]>
Co-authored-by: Nico Deblauwe <[email protected]>
  • Loading branch information
3 people authored Jan 26, 2024
1 parent ae3a845 commit e30e14d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
19 changes: 10 additions & 9 deletions app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
use HasFactory;
use HasFactory, SoftDeletes;

protected $fillable = [
'product_type_id',
'name',
'price_in_cents',
'description',
];
protected $guarded = ['id'];

public function product_type()
public function product_types()
{
return $this->belongsTo(product_type::class);
return $this->belongsTo(ProductType::class);
}

public function getPriceAttribute(): float
{
return $this->price_in_cents / 100;
}
}
15 changes: 11 additions & 4 deletions app/Models/ProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ class ProductType extends Model
{
use HasFactory;

protected $fillable = [
'name',
'time_unit',
'price_in_cents',
protected $guarded = [
'id',
];

public function products()
{
return $this->hasMany(Product::class);
}

public function getPriceAttribute(): float
{
return $this->price_in_cents / 100;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public function up(): void
{
Schema::create('product_types', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name', 100)->unique();
$table->enum('time_unit', ['y', 'm', 'h'])->nullable();
$table->bigInteger('price_in_cents')->nullable();

$table->timestamps();
$table->softDeletes();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function up(): void
$table->string('name');
$table->bigInteger('price_in_cents')->nullable();
$table->string('description');

$table->timestamps();
$table->softDeletes();

$table->foreign('product_types_id')->references('id')->on('product_types');
});
Expand Down
3 changes: 0 additions & 3 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,5 @@ public function run(): void
'invoice_id' => $invoice->id,
]);
});



}
}

0 comments on commit e30e14d

Please sign in to comment.