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.
main
KKlochko 2 years ago
parent 4a4d5e7335
commit ea120c8c9c

@ -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();
}
}

@ -18,7 +18,7 @@ class User extends Authenticatable
* @var array<int, string>
*/
protected $fillable = [
'name',
'username',
'matrix_username',
'phone',
];

@ -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();

@ -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

Loading…
Cancel
Save