diff --git a/packages/Sarga/API/Http/Resources/Core/State.php b/packages/Sarga/API/Http/Resources/Core/State.php new file mode 100644 index 000000000..a5f0da490 --- /dev/null +++ b/packages/Sarga/API/Http/Resources/Core/State.php @@ -0,0 +1,23 @@ + $this->id, + 'code' => $this->code, + 'name' => $this->default_name + ]; + } +} \ No newline at end of file diff --git a/packages/Sarga/API/Http/routes.php b/packages/Sarga/API/Http/routes.php index 100b2c342..aab750e58 100644 --- a/packages/Sarga/API/Http/routes.php +++ b/packages/Sarga/API/Http/routes.php @@ -12,6 +12,7 @@ use Webkul\API\Http\Controllers\Shop\ResourceController; use Webkul\Attribute\Repositories\AttributeOptionRepository; use Sarga\API\Http\Resources\Catalog\AttributeOption; use Sarga\API\Http\Resources\Catalog\Category; +use Webkul\Core\Repositories\CountryStateRepository; Route::group(['prefix' => 'api'], function ($router) { Route::group(['middleware' => ['locale', 'currency']], function ($router) { @@ -42,6 +43,11 @@ Route::group(['prefix' => 'api'], function ($router) { Route::get('products/{id}', [Products::class, 'get']); Route::get('products/{id}/variants', [Products::class, 'variants']); + + Route::get('states', [ResourceController::class, 'index'])->defaults('_config', [ + 'repository' => CountryStateRepository::class, + 'resource' => Category::class, + ]); }); Route::group(['prefix' => 'scrap','middleware' =>['scrap']], function ($router){ diff --git a/packages/Sarga/API/Repositories/ProductRepository.php b/packages/Sarga/API/Repositories/ProductRepository.php index 9d897ea69..72a7cd64d 100644 --- a/packages/Sarga/API/Repositories/ProductRepository.php +++ b/packages/Sarga/API/Repositories/ProductRepository.php @@ -72,6 +72,9 @@ class ProductRepository extends WProductRepository } else $product['attribute_family_id'] = $product['type'] == 'configurable' ? 2 : 1; + if(!empty($data['brand']) && $brand = $this->brandRepository->findOneByField('name' , $data['brand'])){ + $product['brand_id'] = $brand->id; + } //create product $parentProduct = $this->getModel()->create($product); $this->assignAttributes($parentProduct, [ @@ -97,25 +100,6 @@ class ProductRepository extends WProductRepository $this->createSellerProduct($product, $seller->id); } - if(!empty($data['brand'])){ - $brand = $this->brandRepository->firstOrCreate(['code' =>Str::slug($data['brand'])],[ - 'name' =>$data['brand'], - 'status' =>1, - ]); - - if($brand){ - $brand->products()->attach($parentProduct->id); - - if(!empty($data['categories'])){ - $brand->categories()->attach($data['categories']); - } - - if($seller){ - $brand->sellers()->attach($seller->id); - } - } - } - if ($product['type'] == 'configurable') { $variant = null; //create variants color diff --git a/packages/Sarga/Admin/src/Providers/AdminServiceProvider.php b/packages/Sarga/Admin/src/Providers/AdminServiceProvider.php index b7607daaa..8a7549107 100644 --- a/packages/Sarga/Admin/src/Providers/AdminServiceProvider.php +++ b/packages/Sarga/Admin/src/Providers/AdminServiceProvider.php @@ -21,6 +21,7 @@ class AdminServiceProvider extends ServiceProvider $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'sarga'); $this->app->register(EventServiceProvider::class); + } public function register() diff --git a/packages/Sarga/Brand/src/Database/Migrations/2022_01_24_130148_create_brands_table.php b/packages/Sarga/Brand/src/Database/Migrations/2022_01_24_130148_create_brands_table.php index e136ef46c..01304908d 100644 --- a/packages/Sarga/Brand/src/Database/Migrations/2022_01_24_130148_create_brands_table.php +++ b/packages/Sarga/Brand/src/Database/Migrations/2022_01_24_130148_create_brands_table.php @@ -30,11 +30,16 @@ class CreateBrandsTable extends Migration $table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade'); }); - Schema::create('product_brands',function (Blueprint $table) { - $table->integer('product_id')->unsigned(); - $table->integer('brand_id')->unsigned(); - $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); - $table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade'); +// Schema::create('product_brands',function (Blueprint $table) { +// $table->integer('product_id')->unsigned(); +// $table->integer('brand_id')->unsigned(); +// $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); +// $table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade'); +// }); + + Schema::table('products', function (Blueprint $table) { + $table->integer('brand_id')->unsigned()->nullable(); + $table->foreign('brand_id')->references('id')->on('brands')->onDelete('set null'); }); Schema::create('seller_brands',function (Blueprint $table) { @@ -52,9 +57,9 @@ class CreateBrandsTable extends Migration */ public function down() { - Schema::dropIfExists('brands'); Schema::dropIfExists('category_brands'); - Schema::dropIfExists('product_brands'); +// Schema::dropIfExists('product_brands'); Schema::dropIfExists('seller_brands'); + Schema::dropIfExists('brands'); } } diff --git a/packages/Sarga/Brand/src/Models/Brand.php b/packages/Sarga/Brand/src/Models/Brand.php index f42b76eba..d8581c229 100644 --- a/packages/Sarga/Brand/src/Models/Brand.php +++ b/packages/Sarga/Brand/src/Models/Brand.php @@ -2,6 +2,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Facades\Storage; use Sarga\Brand\Contracts\Brand as BrandContract; use Webkul\Category\Models\CategoryProxy; @@ -40,9 +41,9 @@ class Brand extends Model implements BrandContract /** * The products that belong to the category. */ - public function products(): BelongsToMany + public function products(): HasMany { - return $this->belongsToMany(ProductProxy::modelClass(), 'product_brands'); + return $this->hasMany(ProductProxy::modelClass(), 'brand_id'); } public function sellers():BelongsToMany diff --git a/packages/Sarga/Brand/src/Providers/BrandServiceProvider.php b/packages/Sarga/Brand/src/Providers/BrandServiceProvider.php index bdebdb9d9..f4c20a5a2 100644 --- a/packages/Sarga/Brand/src/Providers/BrandServiceProvider.php +++ b/packages/Sarga/Brand/src/Providers/BrandServiceProvider.php @@ -1,6 +1,8 @@ loadRoutesFrom(__DIR__ . '/../Routes/brand-routes.php'); $this->loadViewsFrom(__DIR__ . '/../Resources/views', 'brand'); $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'brand'); +// Log::info('brandd service provider'); + $this->app->register(EventServiceProvider::class); // CategoryProxy::observe(CategoryObserver::class); } diff --git a/packages/Sarga/Brand/src/Providers/EventServiceProvider.php b/packages/Sarga/Brand/src/Providers/EventServiceProvider.php new file mode 100644 index 000000000..258cbda1d --- /dev/null +++ b/packages/Sarga/Brand/src/Providers/EventServiceProvider.php @@ -0,0 +1,23 @@ +addTemplate('brand::admin.catalog.products.fields.brand'); + + }); + } +} \ No newline at end of file diff --git a/packages/Sarga/Brand/src/Repositories/BrandRepository.php b/packages/Sarga/Brand/src/Repositories/BrandRepository.php index e16c780c5..a9880f1c4 100644 --- a/packages/Sarga/Brand/src/Repositories/BrandRepository.php +++ b/packages/Sarga/Brand/src/Repositories/BrandRepository.php @@ -73,4 +73,8 @@ class BrandRepository extends Repository $brand->save(); } } + + public function actives(){ + return $this->findByField('status',1); + } } \ No newline at end of file diff --git a/packages/Sarga/Brand/src/Resources/views/admin/create.blade.php b/packages/Sarga/Brand/src/Resources/views/admin/catalog/brand/create.blade.php similarity index 100% rename from packages/Sarga/Brand/src/Resources/views/admin/create.blade.php rename to packages/Sarga/Brand/src/Resources/views/admin/catalog/brand/create.blade.php diff --git a/packages/Sarga/Brand/src/Resources/views/admin/edit.blade.php b/packages/Sarga/Brand/src/Resources/views/admin/catalog/brand/edit.blade.php similarity index 100% rename from packages/Sarga/Brand/src/Resources/views/admin/edit.blade.php rename to packages/Sarga/Brand/src/Resources/views/admin/catalog/brand/edit.blade.php diff --git a/packages/Sarga/Brand/src/Resources/views/admin/index.blade.php b/packages/Sarga/Brand/src/Resources/views/admin/catalog/brand/index.blade.php similarity index 100% rename from packages/Sarga/Brand/src/Resources/views/admin/index.blade.php rename to packages/Sarga/Brand/src/Resources/views/admin/catalog/brand/index.blade.php diff --git a/packages/Sarga/Brand/src/Resources/views/admin/catalog/products/fields/brand.blade.php b/packages/Sarga/Brand/src/Resources/views/admin/catalog/products/fields/brand.blade.php new file mode 100644 index 000000000..295508a7c --- /dev/null +++ b/packages/Sarga/Brand/src/Resources/views/admin/catalog/products/fields/brand.blade.php @@ -0,0 +1,15 @@ +@php $brands = app(Sarga\Brand\Repositories\BrandRepository::class)->actives(); @endphp +