Display product on the main page

This commit is contained in:
bad 2021-11-14 20:44:10 +01:00
parent 0e61588895
commit f8e7060ef2
8 changed files with 72 additions and 5 deletions

View file

@ -0,0 +1,14 @@
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class MainPageController extends Controller
{
public function index() {
$products = Product::query()->latest()->limit(100)->get();
return view("index", ["products" => $products]);
}
}

View file

@ -16,6 +16,6 @@ class Image extends Model
public function URL() public function URL()
{ {
Storage::url($this->path); return Storage::url($this->path);
} }
} }

View file

@ -11,6 +11,9 @@ class Product extends Model
use HasUUID; use HasUUID;
public $primaryKey = "uuid"; public $primaryKey = "uuid";
public function images() {
return $this->hasMany(Image::class, "product_uuid", "uuid");
}
protected $fillable = [ protected $fillable = [
'name', 'name',

View file

@ -0,0 +1,33 @@
<?php
namespace Database\Factories;
use App\Models\Image;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use illuminate\support\str;
class ImageFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Image::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$imageFile = new File($this->faker->image());
return [
'uuid' => (string) Str::uuid(),
'path' => Storage::putFile("uploads", $imageFile),
];
}
}

View file

@ -15,6 +15,7 @@ class CreateImagesTable extends Migration
{ {
Schema::create('images', function (Blueprint $table) { Schema::create('images', function (Blueprint $table) {
$table->uuid("uuid")->unique()->primary; $table->uuid("uuid")->unique()->primary;
$table->foreignUuid("product_uuid")->references("uuid")->on("products");
$table->string('path')->unique(); $table->string('path')->unique();
$table->timestamps(); $table->timestamps();
}); });

View file

@ -2,6 +2,7 @@
namespace Database\Seeders; namespace Database\Seeders;
use App\Models\Image;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
@ -14,6 +15,6 @@ class DatabaseSeeder extends Seeder
public function run() public function run()
{ {
\App\Models\User::factory(10)->create(); \App\Models\User::factory(10)->create();
\App\Models\Product::factory(10)->create(); \App\Models\Product::factory(10)->has(Image::factory()->count(3))->create();
} }
} }

View file

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sklep</title>
</head>
<body>
@foreach ($products as $product)
<div class="product-container">
<img class="product-image" src="@if(isset($product->images[0])) {{ $product->images[0]->URL() }} @else {{ asset("test.txt") }} @endif">
<p class="product-name"> {{ $product->name }} </p>
</div>
@endforeach
</body>
</html>

View file

@ -2,6 +2,7 @@
use App\Http\Controllers\ImageController; use App\Http\Controllers\ImageController;
use App\Http\Controllers\LoginController; use App\Http\Controllers\LoginController;
use App\Http\Controllers\MainPageController;
use App\Http\Controllers\OrderController; use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProductController; use App\Http\Controllers\ProductController;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
@ -18,9 +19,7 @@ use Illuminate\Support\Facades\Route;
| |
*/ */
Route::get('/', function () { Route::get('/', [MainPageController::class, "index"])->name("main");
return view('welcome');
});
Route::get('/user', function () { Route::get('/user', function () {
$user = Auth::user(); $user = Auth::user();