brand product relation and address

This commit is contained in:
merdan 2022-01-27 20:07:05 +05:00
parent 8ffcd7568e
commit d4281d67cf
16 changed files with 109 additions and 37 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace Sarga\API\Http\Resources\Core;
use Illuminate\Http\Resources\Json\JsonResource;
class State extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'code' => $this->code,
'name' => $this->default_name
];
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,8 @@
<?php namespace Sarga\Brand\Providers;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
class BrandServiceProvider extends ServiceProvider
{
/**
@ -14,6 +16,8 @@ class BrandServiceProvider extends ServiceProvider
$this->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);
}

View File

@ -0,0 +1,23 @@
<?php
namespace Sarga\Brand\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot(): void
{
Event::listen('bagisto.admin.catalog.product.edit_form_accordian.additional_views.before', function($viewRenderEventManager){
$viewRenderEventManager->addTemplate('brand::admin.catalog.products.fields.brand');
});
}
}

View File

@ -73,4 +73,8 @@ class BrandRepository extends Repository
$brand->save();
}
}
public function actives(){
return $this->findByField('status',1);
}
}

View File

@ -0,0 +1,15 @@
@php $brands = app(Sarga\Brand\Repositories\BrandRepository::class)->actives(); @endphp
<div class="control-group" :class="[errors.has('brand_id') ? 'has-error' : '']">
<label for="brand_id" class="required">{{ __('brand::app.brand') }}</label>
<select class="control" v-validate="'required'" id="brand_id" name="brand_id" data-vv-as="&quot;{{ __('brand::app.brand') }}&quot;">
<option value="">Select</option>
@foreach($brands as $brand)
<option value="{{ $brand->id }}" {{ $product->brand_id == $brand->id ? 'selected' : '' }}>
{{ $brand->name }}
</option>
@endforeach
</select>
<span class="control-error" v-if="errors.has('brand_id')">@{{ errors.first('brand_id') }}</span>
</div>

View File

@ -11,11 +11,11 @@ Route::group(['middleware' => ['web', 'admin', 'admin_locale'], 'prefix' => conf
* Categories routes.
*/
Route::get('/brands', [BrandController::class, 'index'])->defaults('_config', [
'view' => 'brand::admin.index',
'view' => 'brand::admin.catalog.brand.index',
])->name('admin.catalog.brand.index');
Route::get('/brands/create', [BrandController::class, 'create'])->defaults('_config', [
'view' => 'brand::admin.create',
'view' => 'brand::admin.catalog.brand.create',
])->name('admin.catalog.brand.create');
Route::post('/brands/create', [BrandController::class, 'store'])->defaults('_config', [
@ -23,7 +23,7 @@ Route::group(['middleware' => ['web', 'admin', 'admin_locale'], 'prefix' => conf
])->name('admin.catalog.brand.store');
Route::get('/brands/edit/{id}', [BrandController::class, 'edit'])->defaults('_config', [
'view' => 'brand::admin.edit',
'view' => 'brand::admin.catalog.brand.edit',
])->name('admin.catalog.brand.edit');
Route::post('/brands/edit/{id}', [BrandController::class, 'update'])->defaults('_config', [

View File

@ -80,6 +80,8 @@ class AddressController extends Controller
request()->merge([
'address1' => implode(PHP_EOL, array_filter(request()->input('address1'))),
'customer_id' => $customer->id,
'country' => 'Turkmenistan',
'postcode' => '0000'
]);
}
@ -87,10 +89,10 @@ class AddressController extends Controller
'address1' => 'string|required',
'company' => 'string|nullable',
'vat_id' => 'string|nullable',
'country' => 'string|required',
// 'country' => 'string|required',
'state' => 'string|nullable',
'city' => 'string|required',
'postcode' => 'required',
// 'postcode' => 'required',
'phone' => 'required',
]);
@ -105,7 +107,7 @@ class AddressController extends Controller
/**
* Update the specified resource in storage.
* @param int $id
*
*
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
*/
@ -117,16 +119,19 @@ class AddressController extends Controller
]);
}
request()->merge(['address1' => implode(PHP_EOL, array_filter(request()->input('address1')))]);
request()->merge(['address1' => implode(PHP_EOL, array_filter(request()->input('address1'))),
'country' => 'Turkmenistan',
'postcode' => '0000'
]);
$this->validate(request(), [
'address1' => 'string|required',
'company' => 'string|nullable',
'vat_id' => 'string|nullable',
'country' => 'string|required',
// 'country' => 'string|required',
'state' => 'string|nullable',
'city' => 'string|required',
'postcode' => 'required',
// 'postcode' => 'required',
'phone' => 'required',
]);

View File

@ -33,6 +33,7 @@ class Product extends Model implements ProductContract
'attribute_family_id',
'sku',
'parent_id',
'brand_id'
];
/**