Add backend categories

This commit is contained in:
bad 2021-12-08 23:39:53 +01:00
parent 9b9bd4ff7b
commit f46280eb2b
11 changed files with 137 additions and 40 deletions

View File

@ -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}}
{"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}}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Faker;
use Faker\Provider\Base;
class CategoryNameProvider extends Base
{
protected static $categories = [
"żywioły",
"wody",
"pierwiastki",
"racje",
"plastik",
"naturalne",
"rzeczywiste",
"A|B",
"moduły node",
"kontenery",
"filmy",
"bajki",
"maszyny",
"challenge",
"śmieci",
];
public function categoryName(): string
{
return static::randomElement(static::$categories);
}
}

22
app/Models/Category.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory, HasUUID;
public $primaryKey = "uuid";
protected $fillable = ['name'];
public function products() {
return $this->belongsToMany(Category::class,'categories_products','category_uuid');
}
public function image() {
return $this->morphOne(Image::class, 'imageable');
}
}

View File

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

View File

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

View File

@ -0,0 +1,29 @@
<?php
namespace Database\Factories;
use App\Models\Category;
use App\Models\Order;
use Illuminate\Database\Eloquent\Factories\Factory;
class CategoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Category::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->categoryName(),
];
}
}

View File

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

View File

@ -1,35 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class AddCategories extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table("images", function(Blueprint $table) {
$table->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
}
}

View File

@ -0,0 +1,41 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->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');
}
}

View File

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

View File

@ -14,6 +14,7 @@
<div class="product-container">
<div class="product-container-left">
<img class="product-image" src="@if(isset($product->images[0])) {{ $product->images[0]->URL() }} @else {{ asset("test.txt") }} @endif">
{{ dd($product->categories[0]->name) }}
<div class="name-price-container">
<h2 class="product-name"> {{ $product->name }} </h2>
<h3>{{ ($product->price) }}<span class="price-currency"></span></h3>