From ea120c8c9c47596c3c514d5cc5f774d8426fc378 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 16 Jun 2023 16:50:12 +0300 Subject: [PATCH] Update User, Cart models. Added isEmpty (no items), setCity, dropItems to Cart. The cart field, company, was made nullable. Updated the user field from name to username and made it unique. --- app/Models/Cart.php | 22 ++++++++++++++++++- app/Models/User.php | 2 +- .../2014_10_12_000000_create_users_table.php | 2 +- .../2023_06_15_134235_create_carts_table.php | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/Models/Cart.php b/app/Models/Cart.php index ca3458d..283b9b0 100644 --- a/app/Models/Cart.php +++ b/app/Models/Cart.php @@ -12,7 +12,7 @@ class Cart extends Model use HasFactory; protected $fillable = [ - 'status', + 'status', // CART | DONE ]; public function city(): BelongsTo @@ -29,5 +29,25 @@ class Cart extends Model { return $this->belongsToMany(Item::class, 'carts_items', 'cart_id', 'item_id'); } + + public function isEmpty() { + return $this->items()->isEmpty(); + } + + public function setCity($city) { + if($this->city == $city) + return; + + $this->city = $city; + + if(!$this->isEmpty()) + $this->dropItems(); + + $this->save(); + } + + public function dropItems() { + $this->items()->detach(); + } } diff --git a/app/Models/User.php b/app/Models/User.php index ceeaade..a3e099b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -18,7 +18,7 @@ class User extends Authenticatable * @var array */ protected $fillable = [ - 'name', + 'username', 'matrix_username', 'phone', ]; diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 9f7aeb5..2d91c19 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -13,7 +13,7 @@ return new class extends Migration { Schema::create('users', function (Blueprint $table) { $table->id(); - $table->string('name'); + $table->string('username')->unique(); $table->string('matrix_username')->unique(); $table->string('phone')->unique(); $table->timestamps(); diff --git a/database/migrations/2023_06_15_134235_create_carts_table.php b/database/migrations/2023_06_15_134235_create_carts_table.php index 197c88c..dbd4290 100644 --- a/database/migrations/2023_06_15_134235_create_carts_table.php +++ b/database/migrations/2023_06_15_134235_create_carts_table.php @@ -14,7 +14,7 @@ return new class extends Migration Schema::create('carts', function (Blueprint $table) { $table->id(); $table->unsignedBiginteger('city_id')->unsigned(); - $table->unsignedBiginteger('company_id')->unsigned(); + $table->unsignedBiginteger('company_id')->unsigned()->nullable(); $table->unsignedBiginteger('user_id')->unsigned(); $table->string('status'); // CART | DONE