From b935bda881b0e1b71e6307447247a3690b0a0e9a Mon Sep 17 00:00:00 2001 From: "Kazarmshchikov.A" Date: Wed, 21 May 2025 03:41:21 +0700 Subject: [PATCH 1/2] HW4 is completed --- .gitignore | 2 + app/Models/BaseModel.php | 17 +++ app/Models/Category.php | 42 ++++++ app/Models/City.php | 63 ++++++++ app/Models/Country.php | 54 +++++++ app/Models/Currency.php | 41 ++++++ app/Models/Image.php | 54 +++++++ app/Models/Item.php | 137 ++++++++++++++++++ app/Models/Region.php | 54 +++++++ app/Models/Role.php | 42 ++++++ app/Models/RoleUser.php | 33 +++++ app/Models/User.php | 56 ++++++- database/factories/ImageFactory.php | 26 ++++ database/factories/ItemFactory.php | 44 ++++++ database/factories/RoleUserFactory.php | 24 +++ database/factories/UserFactory.php | 6 +- ...25_05_17_175629_create_countries_table.php | 2 +- .../2025_05_18_091213_create_images_table.php | 3 - .../2025_05_20_191236_create_roles_table.php | 56 +++++++ ...25_05_20_191341_create_role_user_table.php | 32 ++++ database/seeders/DatabaseSeeder.php | 13 +- database/seeders/ImageSeeder.php | 16 ++ database/seeders/ItemsSeeder.php | 30 +--- database/seeders/UserSeeder.php | 17 +++ 24 files changed, 826 insertions(+), 38 deletions(-) create mode 100644 app/Models/BaseModel.php create mode 100644 app/Models/Category.php create mode 100644 app/Models/City.php create mode 100644 app/Models/Country.php create mode 100644 app/Models/Currency.php create mode 100644 app/Models/Image.php create mode 100644 app/Models/Item.php create mode 100644 app/Models/Region.php create mode 100644 app/Models/Role.php create mode 100644 app/Models/RoleUser.php create mode 100644 database/factories/ImageFactory.php create mode 100644 database/factories/ItemFactory.php create mode 100644 database/factories/RoleUserFactory.php create mode 100644 database/migrations/2025_05_20_191236_create_roles_table.php create mode 100644 database/migrations/2025_05_20_191341_create_role_user_table.php create mode 100644 database/seeders/ImageSeeder.php create mode 100644 database/seeders/UserSeeder.php diff --git a/.gitignore b/.gitignore index 08e51ca68..dac24af22 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,8 @@ yarn-error.log /.nova /.vscode /.zed +/.vite _ide_helper.php +_ide_helper_models.php .phpstorm.meta.php .php-cs-fixer.cache diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php new file mode 100644 index 000000000..366d90ef8 --- /dev/null +++ b/app/Models/BaseModel.php @@ -0,0 +1,17 @@ +|BaseModel newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|BaseModel newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|BaseModel query() + * @mixin \Eloquent + */ +class BaseModel extends Model {} diff --git a/app/Models/Category.php b/app/Models/Category.php new file mode 100644 index 000000000..1ead75c9d --- /dev/null +++ b/app/Models/Category.php @@ -0,0 +1,42 @@ +|Category newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Category newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Category query() + * @method static \Illuminate\Database\Eloquent\Builder|Category whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Category whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Category whereName($value) + * @method static \Illuminate\Database\Eloquent\Builder|Category whereUpdatedAt($value) + * @property-read Image $image + * @mixin Eloquent + */ +class Category extends BaseModel +{ + protected $fillable = [ + 'name', + ]; + protected $hidden = [ + 'created_at', + 'updated_at', + ]; + + public function image(): MorphOne + { + return $this->morphOne(Image::class, 'image'); + } +} diff --git a/app/Models/City.php b/app/Models/City.php new file mode 100644 index 000000000..dfbccf226 --- /dev/null +++ b/app/Models/City.php @@ -0,0 +1,63 @@ +|City newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|City newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|City query() + * @method static \Illuminate\Database\Eloquent\Builder|City whereCountryId($value) + * @method static \Illuminate\Database\Eloquent\Builder|City whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|City whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|City whereLatitude($value) + * @method static \Illuminate\Database\Eloquent\Builder|City whereLongitude($value) + * @method static \Illuminate\Database\Eloquent\Builder|City whereName($value) + * @method static \Illuminate\Database\Eloquent\Builder|City whereRegionId($value) + * @method static \Illuminate\Database\Eloquent\Builder|City whereTimezone($value) + * @method static \Illuminate\Database\Eloquent\Builder|City whereUpdatedAt($value) + * @property-read Country $country + * @property-read Region $region + * @mixin Eloquent + */ +class City extends BaseModel +{ + protected $fillable = [ + 'name', + 'latitude', + 'longitude', + 'timezone', + 'country_id', + 'region_id', + ]; + protected $hidden = [ + 'created_at', + 'updated_at', + ]; + + public function country(): BelongsTo + { + return $this->belongsTo(Country::class); + } + + public function region(): BelongsTo + { + return $this->belongsTo(Region::class); + } +} diff --git a/app/Models/Country.php b/app/Models/Country.php new file mode 100644 index 000000000..f6fbfcb51 --- /dev/null +++ b/app/Models/Country.php @@ -0,0 +1,54 @@ +|Country newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Country newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Country query() + * @method static \Illuminate\Database\Eloquent\Builder|Country whereCode($value) + * @method static \Illuminate\Database\Eloquent\Builder|Country whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Country whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Country whereName($value) + * @method static \Illuminate\Database\Eloquent\Builder|Country whereUpdatedAt($value) + * @property-read Collection $cities + * @property-read int|null $cities_count + * @property-read Collection $regions + * @property-read int|null $regions_count + * @mixin Eloquent + */ +class Country extends BaseModel +{ + protected $fillable = [ + 'name', + 'code', + ]; + protected $hidden = [ + 'created_at', + 'updated_at', + ]; + + public function regions(): HasMany + { + return $this->hasMany(Region::class); + } + + public function cities(): HasMany + { + return $this->hasMany(City::class); + } +} diff --git a/app/Models/Currency.php b/app/Models/Currency.php new file mode 100644 index 000000000..288aaa8e9 --- /dev/null +++ b/app/Models/Currency.php @@ -0,0 +1,41 @@ +|Currency newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Currency newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Currency query() + * @method static \Illuminate\Database\Eloquent\Builder|Currency whereChar($value) + * @method static \Illuminate\Database\Eloquent\Builder|Currency whereCode($value) + * @method static \Illuminate\Database\Eloquent\Builder|Currency whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Currency whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Currency whereName($value) + * @method static \Illuminate\Database\Eloquent\Builder|Currency whereUpdatedAt($value) + * @mixin Eloquent + */ +class Currency extends BaseModel +{ + protected $fillable = [ + 'name', + 'code', + 'char', + ]; + protected $hidden = [ + 'created_at', + 'updated_at', + ]; +} diff --git a/app/Models/Image.php b/app/Models/Image.php new file mode 100644 index 000000000..ef2762cd5 --- /dev/null +++ b/app/Models/Image.php @@ -0,0 +1,54 @@ +|Image newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Image newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Image query() + * @method static \Illuminate\Database\Eloquent\Builder|Image whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Image whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Image whereImageId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Image whereImageType($value) + * @method static \Illuminate\Database\Eloquent\Builder|Image whereMain($value) + * @method static \Illuminate\Database\Eloquent\Builder|Image wherePath($value) + * @method static \Illuminate\Database\Eloquent\Builder|Image whereUpdatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Image whereUserId($value) + * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $image + * @mixin Eloquent + */ +class Image extends BaseModel +{ + /** @use HasFactory */ + use hasFactory; + + protected $fillable = [ + 'path', + 'main', + 'image_type', + 'image_id', + ]; + + public function image(): MorphTo + { + return $this->morphTo(); + } +} diff --git a/app/Models/Item.php b/app/Models/Item.php new file mode 100644 index 000000000..5ebdf6e85 --- /dev/null +++ b/app/Models/Item.php @@ -0,0 +1,137 @@ +|Item newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Item newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Item query() + * @method static \Illuminate\Database\Eloquent\Builder|Item whereAddress($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereCategoryId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereCityId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereCountryId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereCurrencyId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereDeletedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereDescription($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereIsModerated($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereIsNew($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereIsPublished($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereName($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item wherePrice($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item wherePublishedUntil($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereRegionId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereUpdatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Item whereUserId($value) + * @property-read Category $category + * @property-read City $city + * @property-read Country $country + * @property-read Currency $currency + * @property-read string $full_address + * @property-read string $price_with_currency + * @property-read Region $region + * @property-read User $user + * @property-read Collection $images + * @property-read int|null $images_count + * @mixin Eloquent + */ +class Item extends BaseModel +{ + /** @use HasFactory */ + use HasFactory; + + protected $fillable = [ + 'name', + 'description', + 'price', + 'address', + 'user_id', + 'currency_id', + 'category_id', + 'country_id', + 'region_id', + 'city_id', + 'is_new', + 'is_moderated', + 'is_published', + 'published_until', + ]; + + protected $hidden = [ + 'created_at', + 'updated_at', + ]; + + protected $casts = [ + 'is_new' => 'boolean', + 'is_moderated' => 'boolean', + 'is_published' => 'boolean', + 'published_until' => 'datetime', + ]; + + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } + + public function currency(): BelongsTo + { + return $this->belongsTo(Currency::class); + } + + public function category(): BelongsTo + { + return $this->belongsTo(Category::class); + } + + public function country(): BelongsTo + { + return $this->belongsTo(Country::class); + } + + public function region(): BelongsTo + { + return $this->belongsTo(Region::class); + } + + public function city(): BelongsTo + { + return $this->belongsTo(City::class); + } + + public function images(): MorphMany + { + return $this->morphMany(Image::class, 'image'); + } +} diff --git a/app/Models/Region.php b/app/Models/Region.php new file mode 100644 index 000000000..65ec9610f --- /dev/null +++ b/app/Models/Region.php @@ -0,0 +1,54 @@ +|Region newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Region newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Region query() + * @method static \Illuminate\Database\Eloquent\Builder|Region whereCountryId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Region whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Region whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Region whereName($value) + * @method static \Illuminate\Database\Eloquent\Builder|Region whereUpdatedAt($value) + * @property-read Collection $cities + * @property-read int|null $cities_count + * @property-read Country $country + * @mixin Eloquent + */ +class Region extends BaseModel +{ + protected $fillable = [ + 'name', + 'country_id', + ]; + protected $hidden = [ + 'created_at', + 'updated_at', + ]; + + public function country(): BelongsTo + { + return $this->belongsTo(Country::class); + } + + public function cities(): HasMany + { + return $this->hasMany(City::class); + } +} diff --git a/app/Models/Role.php b/app/Models/Role.php new file mode 100644 index 000000000..3e4a35bfc --- /dev/null +++ b/app/Models/Role.php @@ -0,0 +1,42 @@ +|Role newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Role newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Role query() + * @method static \Illuminate\Database\Eloquent\Builder|Role whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|Role whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|Role whereName($value) + * @method static \Illuminate\Database\Eloquent\Builder|Role whereUpdatedAt($value) + * @property-read Collection $users + * @property-read int|null $users_count + * @method static \Illuminate\Database\Eloquent\Builder|Role whereKey($value) + * @mixin Eloquent + */ +class Role extends Model +{ + protected $fillable = [ + 'name', + 'key', + ]; + + public function users(): BelongsToMany + { + return $this->belongsToMany(User::class); + } +} diff --git a/app/Models/RoleUser.php b/app/Models/RoleUser.php new file mode 100644 index 000000000..331d7b85f --- /dev/null +++ b/app/Models/RoleUser.php @@ -0,0 +1,33 @@ +|RoleUser newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|RoleUser newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|RoleUser query() + * @mixin Eloquent + */ +class RoleUser extends Model +{ + /** @use HasFactory */ + use hasFactory; + use AsPivot; + + public $timestamps = false; + public $fillable = [ + 'user_id', + 'role_id', + ]; +} diff --git a/app/Models/User.php b/app/Models/User.php index cafe718aa..da5dbf822 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -1,16 +1,58 @@ $notifications + * @property-read int|null $notifications_count + * @method static UserFactory factory($count = null, $state = []) + * @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|User newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|User query() + * @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|User whereEmail($value) + * @method static \Illuminate\Database\Eloquent\Builder|User whereEmailVerifiedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|User whereId($value) + * @method static \Illuminate\Database\Eloquent\Builder|User whereName($value) + * @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value) + * @method static \Illuminate\Database\Eloquent\Builder|User wherePhone($value) + * @method static \Illuminate\Database\Eloquent\Builder|User whereRememberToken($value) + * @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value) + * @property-read Image $image + * @property-read \Illuminate\Database\Eloquent\Collection $roles + * @property-read int|null $roles_count + * @mixin Eloquent + */ class User extends Authenticatable { - /** @use HasFactory<\Database\Factories\UserFactory> */ - use HasFactory, Notifiable; + /** @use HasFactory */ + use HasFactory; + use Notifiable; /** * The attributes that are mass assignable. @@ -46,4 +88,14 @@ protected function casts(): array 'password' => 'hashed', ]; } + + public function image(): MorphOne + { + return $this->morphOne(Image::class, 'image'); + } + + public function roles(): BelongsToMany + { + return $this->belongsToMany(Role::class); + } } diff --git a/database/factories/ImageFactory.php b/database/factories/ImageFactory.php new file mode 100644 index 000000000..a881aaa5b --- /dev/null +++ b/database/factories/ImageFactory.php @@ -0,0 +1,26 @@ + + */ +class ImageFactory extends Factory +{ + #[\Override] + public function definition(): array + { + return [ + 'path' => fake()->imageUrl(), + 'main' => fake()->boolean(30), + 'image_id' => Item::factory(), + 'image_type' => Item::class, + ]; + } +} diff --git a/database/factories/ItemFactory.php b/database/factories/ItemFactory.php new file mode 100644 index 000000000..7a1f970eb --- /dev/null +++ b/database/factories/ItemFactory.php @@ -0,0 +1,44 @@ + + */ +class ItemFactory extends Factory +{ + #[\Override] + public function definition(): array + { + $users = DB::table('users')->get(); + $category = DB::table('categories')->select('id')->get(); + $country = DB::table('countries')->select('id')->where('code', 'ru')->first(); + $region = DB::table('regions')->select('id')->first(); + $city = DB::table('cities')->select('id')->first(); + $currency = DB::table('currencies')->select('id')->where('code', 'RUB')->first(); + + return [ + 'name' => fake()->sentence(3, 7), + 'description' => fake()->paragraph(), + 'price' => fake()->numberBetween(300, 10000), + 'address' => fake()->address(), + 'user_id' => $users->random()->id, + 'currency_id' => $currency->id, + 'country_id' => $country->id, + 'region_id' => $region->id, + 'city_id' => $city->id, + 'category_id' => $category->random()->id, + 'is_new' => fake()->boolean(30), + 'is_moderated' => fake()->boolean(70), + 'is_published' => fake()->boolean(70), + 'published_until' => fake()->dateTimeBetween('now', '+1 month'), + ]; + } +} diff --git a/database/factories/RoleUserFactory.php b/database/factories/RoleUserFactory.php new file mode 100644 index 000000000..acfcbb58d --- /dev/null +++ b/database/factories/RoleUserFactory.php @@ -0,0 +1,24 @@ + + */ +class RoleUserFactory extends Factory +{ + #[\Override] + public function definition(): array + { + $roles = DB::table('roles')->select('id')->get(); + return [ + 'user_id' => User::factory(), + 'role_id' => $roles->random()->id, + ]; + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 584104c9c..44a37a5d2 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -2,6 +2,9 @@ namespace Database\Factories; +use App\Models\Role; +use App\Models\RoleUser; +use DB; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Str; @@ -29,6 +32,7 @@ public function definition(): array 'email_verified_at' => now(), 'password' => static::$password ??= Hash::make('password'), 'remember_token' => Str::random(10), + 'phone' => fake()->unique()->numerify('7##########'), ]; } @@ -37,7 +41,7 @@ public function definition(): array */ public function unverified(): static { - return $this->state(fn (array $attributes) => [ + return $this->state(fn(array $attributes) => [ 'email_verified_at' => null, ]); } diff --git a/database/migrations/2025_05_17_175629_create_countries_table.php b/database/migrations/2025_05_17_175629_create_countries_table.php index 48c10267d..af8269586 100644 --- a/database/migrations/2025_05_17_175629_create_countries_table.php +++ b/database/migrations/2025_05_17_175629_create_countries_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create($this->tableName, static function (Blueprint $table) { $table->id(); $table->string('name', 255)->comment('Название страны'); - $table->string('code', 2)->unique(); + $table->string('code', 2)->comment('Символьный код')->unique(); $table->timestamps(); $table->comment('Страны'); diff --git a/database/migrations/2025_05_18_091213_create_images_table.php b/database/migrations/2025_05_18_091213_create_images_table.php index ed9f8bec9..bdc22e7a7 100644 --- a/database/migrations/2025_05_18_091213_create_images_table.php +++ b/database/migrations/2025_05_18_091213_create_images_table.php @@ -16,11 +16,8 @@ public function up(): void $table->string('path', 255)->comment('Путь к изображению'); $table->boolean('main')->default(false)->comment('Основное изображение'); $table->morphs('image'); - $table->foreignId('user_id')->comment('Id пользователя'); $table->timestamps(); $table->comment('Изображения'); - - $table->index(['user_id']); }); } diff --git a/database/migrations/2025_05_20_191236_create_roles_table.php b/database/migrations/2025_05_20_191236_create_roles_table.php new file mode 100644 index 000000000..e0e2d68e9 --- /dev/null +++ b/database/migrations/2025_05_20_191236_create_roles_table.php @@ -0,0 +1,56 @@ +tableName, static function (Blueprint $table) { + $table->id(); + $table->string('name', 100)->comment('Название роли'); + $table->string('key', 100)->comment('Строковый ключ'); + $table->timestamps(); + }); + + $roles = [ + [ + 'name' => 'Администратор', + 'key' => 'admin', + 'created_at' => now(), + 'updated_at' => now(), + ], + [ + 'name' => 'Модератор', + 'key' => 'moderator', + 'created_at' => now(), + 'updated_at' => now(), + ], + [ + 'name' => 'Пользователь', + 'key' => 'user', + 'created_at' => now(), + 'updated_at' => now(), + ], + [ + 'name' => 'Гость', + 'key' => 'guest', + 'created_at' => now(), + 'updated_at' => now(), + ], + ]; + + DB::table($this->tableName)->insert($roles); + + } + + public function down(): void + { + Schema::dropIfExists($this->tableName); + } +}; diff --git a/database/migrations/2025_05_20_191341_create_role_user_table.php b/database/migrations/2025_05_20_191341_create_role_user_table.php new file mode 100644 index 000000000..007aa44ae --- /dev/null +++ b/database/migrations/2025_05_20_191341_create_role_user_table.php @@ -0,0 +1,32 @@ +tableName, static function (Blueprint $table) { + $table->id(); + $table->foreignId('user_id') + ->comment('Идентификатор пользователя') + ->constrained('users') + ->cascadeOnDelete(); + $table->foreignId('role_id') + ->comment('Идентификатор роли') + ->constrained('roles') + ->cascadeOnDelete(); + $table->comment('Pivot таблица связи пользователей и их ролей'); + }); + } + + public function down(): void + { + Schema::dropIfExists($this->tableName); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index e09563090..5602361f3 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -4,6 +4,8 @@ namespace Database\Seeders; +use App\Models\Image; +use App\Models\RoleUser; use App\Models\User; // use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; @@ -15,12 +17,17 @@ class DatabaseSeeder extends Seeder */ public function run(): void { - // User::factory(10)->create(); - - User::factory()->create([ + $firstUser = User::factory()->create([ 'name' => 'Test User', 'email' => 'test@example.com', 'phone' => '79237000000', ]); + RoleUser::create([ + 'user_id' => $firstUser->id, + 'role_id' => 1, + ]); + + RoleUser::factory(9)->create(); + Image::factory(10)->create(); } } diff --git a/database/seeders/ImageSeeder.php b/database/seeders/ImageSeeder.php new file mode 100644 index 000000000..4a5848735 --- /dev/null +++ b/database/seeders/ImageSeeder.php @@ -0,0 +1,16 @@ +create(); + } +} diff --git a/database/seeders/ItemsSeeder.php b/database/seeders/ItemsSeeder.php index 5167ccc8f..bd8d92159 100644 --- a/database/seeders/ItemsSeeder.php +++ b/database/seeders/ItemsSeeder.php @@ -4,39 +4,13 @@ namespace Database\Seeders; +use App\Models\Item; use Illuminate\Database\Seeder; -use Illuminate\Support\Facades\DB; class ItemsSeeder extends Seeder { public function run(): void { - $user = DB::table('users')->select('id')->get(); - $category = DB::table('categories')->select('id')->get(); - $country = DB::table('countries')->select('id')->where('code', 'ru')->first(); - $region = DB::table('regions')->select('id')->first(); - $city = DB::table('cities')->select('id')->first(); - $currency = DB::table('currencies')->select('id')->where('code', 'RUB')->first(); - - $item = [ - 'name' => fake()->name(), - 'description' => fake()->paragraph(), - 'price' => fake()->numberBetween(300, 10000), - 'address' => fake()->address(), - 'user_id' => $user->random()->id, - 'currency_id' => $currency->id, - 'country_id' => $country->id, - 'region_id' => $region->id, - 'city_id' => $city->id, - 'category_id' => $category->random()->id, - 'is_new' => fake()->boolean(30), - 'is_moderated' => fake()->boolean(70), - 'is_published' => fake()->boolean(70), - 'published_until' => fake()->dateTimeBetween('now', '+1 month'), - 'created_at' => now(), - 'updated_at' => now(), - ]; - - DB::table('items')->insert($item); + Item::factory(1)->create(); } } diff --git a/database/seeders/UserSeeder.php b/database/seeders/UserSeeder.php new file mode 100644 index 000000000..100d50adf --- /dev/null +++ b/database/seeders/UserSeeder.php @@ -0,0 +1,17 @@ +create(); + } +} From 9f8460d5aed9d5acc141a260c9d9c5a26785082e Mon Sep 17 00:00:00 2001 From: "Kazarmshchikov.A" Date: Wed, 21 May 2025 22:08:03 +0700 Subject: [PATCH 2/2] HW4: review fix --- app/Providers/AppServiceProvider.php | 7 ++++++- database/factories/ImageFactory.php | 2 +- database/seeders/DatabaseSeeder.php | 6 +++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b65b..1fe82fa37 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -19,6 +20,10 @@ public function register(): void */ public function boot(): void { - // + Relation::enforceMorphMap([ + 'user' => 'App\Models\User', + 'item' => 'App\Models\Item', + 'category' => 'App\Models\Category', + ]); } } diff --git a/database/factories/ImageFactory.php b/database/factories/ImageFactory.php index a881aaa5b..bb5b5371c 100644 --- a/database/factories/ImageFactory.php +++ b/database/factories/ImageFactory.php @@ -20,7 +20,7 @@ public function definition(): array 'path' => fake()->imageUrl(), 'main' => fake()->boolean(30), 'image_id' => Item::factory(), - 'image_type' => Item::class, + 'image_type' => 'item', ]; } } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 5602361f3..7b92b73cb 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -26,8 +26,12 @@ public function run(): void 'user_id' => $firstUser->id, 'role_id' => 1, ]); + Image::factory()->create([ + 'image_id' => $firstUser->id, + 'image_type' => 'user', + ]); - RoleUser::factory(9)->create(); + RoleUser::factory(10)->create(); Image::factory(10)->create(); } }