diff --git a/app/Http/Controllers/MainPageController.php b/app/Http/Controllers/MainPageController.php new file mode 100644 index 0000000..d8d2a71 --- /dev/null +++ b/app/Http/Controllers/MainPageController.php @@ -0,0 +1,14 @@ +latest()->limit(100)->get(); + return view("index", ["products" => $products]); + } +} diff --git a/app/Models/Image.php b/app/Models/Image.php index e922f4a..d03a9e6 100644 --- a/app/Models/Image.php +++ b/app/Models/Image.php @@ -16,6 +16,6 @@ class Image extends Model public function URL() { - Storage::url($this->path); + return Storage::url($this->path); } } diff --git a/app/Models/Product.php b/app/Models/Product.php index fb9ac22..baccc1b 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -11,6 +11,9 @@ class Product extends Model use HasUUID; public $primaryKey = "uuid"; + public function images() { + return $this->hasMany(Image::class, "product_uuid", "uuid"); + } protected $fillable = [ 'name', diff --git a/database/factories/ImageFactory.php b/database/factories/ImageFactory.php new file mode 100644 index 0000000..19c212d --- /dev/null +++ b/database/factories/ImageFactory.php @@ -0,0 +1,33 @@ +faker->image()); + return [ + 'uuid' => (string) Str::uuid(), + 'path' => Storage::putFile("uploads", $imageFile), + ]; + } +} diff --git a/database/migrations/2021_10_28_093227_create_images_table.php b/database/migrations/2021_10_28_093227_create_images_table.php index 2c6c771..bdc5888 100644 --- a/database/migrations/2021_10_28_093227_create_images_table.php +++ b/database/migrations/2021_10_28_093227_create_images_table.php @@ -15,6 +15,7 @@ class CreateImagesTable extends Migration { Schema::create('images', function (Blueprint $table) { $table->uuid("uuid")->unique()->primary; + $table->foreignUuid("product_uuid")->references("uuid")->on("products"); $table->string('path')->unique(); $table->timestamps(); }); diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 9a22734..76da146 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use App\Models\Image; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder @@ -14,6 +15,6 @@ class DatabaseSeeder extends Seeder public function run() { \App\Models\User::factory(10)->create(); - \App\Models\Product::factory(10)->create(); + \App\Models\Product::factory(10)->has(Image::factory()->count(3))->create(); } } diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php new file mode 100644 index 0000000..1df57a6 --- /dev/null +++ b/resources/views/index.blade.php @@ -0,0 +1,16 @@ + + +
+ + +{{ $product->name }}
+