From f46280eb2b755a08fbf0a69cd350a6ec1e1815e0 Mon Sep 17 00:00:00 2001 From: bad Date: Wed, 8 Dec 2021 23:39:53 +0100 Subject: [PATCH] Add backend categories --- .php-cs-fixer.cache | 2 +- app/Faker/CategoryNameProvider.php | 32 +++++++++++++++ app/Models/Category.php | 22 ++++++++++ app/Models/Product.php | 7 +++- app/Providers/FakerServiceProvider.php | 2 + database/factories/CategoryFactory.php | 29 +++++++++++++ database/factories/ProductFactory.php | 1 - .../2021_12_08_183713_morph_images.php | 35 ---------------- ...1_12_08_203110_create_categories_table.php | 41 +++++++++++++++++++ database/seeders/DatabaseSeeder.php | 5 ++- resources/views/product/view.blade.php | 1 + 11 files changed, 137 insertions(+), 40 deletions(-) create mode 100644 app/Faker/CategoryNameProvider.php create mode 100644 app/Models/Category.php create mode 100644 database/factories/CategoryFactory.php delete mode 100644 database/migrations/2021_12_08_183713_morph_images.php create mode 100644 database/migrations/2021_12_08_203110_create_categories_table.php diff --git a/.php-cs-fixer.cache b/.php-cs-fixer.cache index b81f1e0..a3394f2 100644 --- a/.php-cs-fixer.cache +++ b/.php-cs-fixer.cache @@ -1 +1 @@ -{"php":"8.0.13","version":"3.0.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":{"elements":["const","method","property"]},"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/run\/user\/1000\/neoformat\/product.blade.php":1493321553,"\/tmp\/neoformat\/user.blade.php":2134171036,"\/tmp\/neoformat\/edit.blade.php":2692964585,"\/tmp\/neoformat\/ProductController.php":4228240463}} \ No newline at end of file +{"php":"8.0.13","version":"3.0.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":{"elements":["const","method","property"]},"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/run\/user\/1000\/neoformat\/product.blade.php":1493321553,"\/tmp\/neoformat\/user.blade.php":2134171036,"\/tmp\/neoformat\/edit.blade.php":2692964585,"\/tmp\/neoformat\/ProductController.php":4228240463,"\/tmp\/neoformat\/DatabaseSeeder.php":3199248297}} \ No newline at end of file diff --git a/app/Faker/CategoryNameProvider.php b/app/Faker/CategoryNameProvider.php new file mode 100644 index 0000000..5869a08 --- /dev/null +++ b/app/Faker/CategoryNameProvider.php @@ -0,0 +1,32 @@ +belongsToMany(Category::class,'categories_products','category_uuid'); + } + + public function image() { + return $this->morphOne(Image::class, 'imageable'); + } +} diff --git a/app/Models/Product.php b/app/Models/Product.php index a701c61..13a7b41 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -11,11 +11,14 @@ class Product extends Model use HasUUID; public $primaryKey = "uuid"; - public function images() - { + public function images() { return $this->morphMany(Image::class, 'imageable'); } + public function categories() { + return $this->belongsToMany(Category::class,'categories_products','product_uuid'); + } + protected $fillable = [ 'name', 'description', diff --git a/app/Providers/FakerServiceProvider.php b/app/Providers/FakerServiceProvider.php index af3c99a..13967c7 100644 --- a/app/Providers/FakerServiceProvider.php +++ b/app/Providers/FakerServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Faker\CategoryNameProvider; use App\Faker\ProductNameProvider; use Illuminate\Support\ServiceProvider; @@ -20,6 +21,7 @@ class FakerServiceProvider extends ServiceProvider $this->app->singleton(Generator::class, function () { $faker = Factory::create(); $faker->addProvider(new ProductNameProvider($faker)); + $faker->addProvider(new CategoryNameProvider($faker)); return $faker; }); } diff --git a/database/factories/CategoryFactory.php b/database/factories/CategoryFactory.php new file mode 100644 index 0000000..635e9d3 --- /dev/null +++ b/database/factories/CategoryFactory.php @@ -0,0 +1,29 @@ + $this->faker->categoryName(), + ]; + } +} diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php index 916b33d..da7dfaf 100644 --- a/database/factories/ProductFactory.php +++ b/database/factories/ProductFactory.php @@ -4,7 +4,6 @@ namespace Database\Factories; use App\Models\Product; use Illuminate\Database\Eloquent\Factories\Factory; -use illuminate\support\str; class ProductFactory extends Factory { diff --git a/database/migrations/2021_12_08_183713_morph_images.php b/database/migrations/2021_12_08_183713_morph_images.php deleted file mode 100644 index b517403..0000000 --- a/database/migrations/2021_12_08_183713_morph_images.php +++ /dev/null @@ -1,35 +0,0 @@ -uuidMorphs("imageable"); - }); - Schema::table("images", function(Blueprint $table) { - DB::table("images")->update(['imageable_id'=>DB::raw('`product_uuid`')], ['imageable_type' => "App\Models\Product"]); - $table->dropConstrainedForeignId("product_uuid"); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - // Ehh don't wanna bother - } -} diff --git a/database/migrations/2021_12_08_203110_create_categories_table.php b/database/migrations/2021_12_08_203110_create_categories_table.php new file mode 100644 index 0000000..dd89a96 --- /dev/null +++ b/database/migrations/2021_12_08_203110_create_categories_table.php @@ -0,0 +1,41 @@ +uuid("uuid")->primary(); + $table->timestamps(); + + $table->string("name")->index()->unique(); + }); + + Schema::create('categories_products', function (Blueprint $table) { + $table->id(); + $table->timestamps(); + + $table->foreignUuid("product_uuid")->references("uuid")->on("products"); + $table->foreignUuid("category_uuid")->references("uuid")->on("categories"); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('categories'); + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 0b5d3f6..2c76878 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -2,6 +2,7 @@ namespace Database\Seeders; +use App\Models\Category; use App\Models\Image; use App\Models\User; use Illuminate\Database\Seeder; @@ -17,6 +18,7 @@ class DatabaseSeeder extends Seeder public function run() { User::factory(10)->create(); + $creds = [ 'email' => config('admin.email'), 'name' => config('admin.username'), @@ -27,6 +29,7 @@ class DatabaseSeeder extends Seeder $user->admin = true; $user->save(); - \App\Models\Product::factory(10)->has(Image::factory()->count(3))->create(); + $categories = Category::factory()->count(4)->create(); + \App\Models\Product::factory(10)->has(Image::factory()->count(3))->hasAttached($categories)->create(); } } diff --git a/resources/views/product/view.blade.php b/resources/views/product/view.blade.php index 6647e35..42f6dea 100644 --- a/resources/views/product/view.blade.php +++ b/resources/views/product/view.blade.php @@ -14,6 +14,7 @@
+ {{ dd($product->categories[0]->name) }}

{{ $product->name }}

{{ ($product->price) }}