Skip to content

Commit

Permalink
Merge pull request #194 from Oneami/master
Browse files Browse the repository at this point in the history
160. Add product attribute: country of origin
  • Loading branch information
dmitrakovich authored Nov 24, 2023
2 parents 20b2e5d + 2e725be commit 37a62bc
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Admin\Controllers\ProductAttributes;

use App\Models\ProductAttributes\CountryOfOrigin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CountryOfOriginController extends AdminController
{
/**
* Title for current resource.
*
* @var string
*/
protected $title = 'Страна производитель';

/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
$grid = new Grid(new CountryOfOrigin());

$grid->column('id', 'Id');
$grid->column('name', 'Название');
$grid->column('slug', 'Slug');
$grid->column('seo', 'Seo');
$grid->column('created_at', 'Дата создания')->display(fn () => date('d.m.Y H:i:s', strtotime($this->created_at)));
$grid->column('updated_at', 'Дата обновления')->display(fn () => date('d.m.Y H:i:s', strtotime($this->updated_at)));

$grid->disableFilter();
$grid->disableColumnSelector();
$grid->disableExport();
$grid->disableRowSelector();

return $grid;
}

/**
* Make a show builder.
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
$show = new Show(CountryOfOrigin::findOrFail($id));

$show->field('id', 'Id');
$show->field('name', 'Название');
$show->field('slug', 'Slug');
$show->field('seo', 'Seo');
$show->field('created_at', 'Дата создания');
$show->field('updated_at', 'Дата обновления');

return $show;
}

/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
$form = new Form(new CountryOfOrigin());

$form->text('name', 'Название');
$form->text('slug', 'Slug');
$form->textarea('seo', 'Seo');

$form->disableCreatingCheck();
$form->disableEditingCheck();
$form->disableViewCheck();

return $form;
}
}
2 changes: 2 additions & 0 deletions src/app/Admin/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Models\Color;
use App\Models\Fabric;
use App\Models\Heel;
use App\Models\ProductAttributes\CountryOfOrigin;
use App\Models\ProductAttributes\Manufacturer;
use App\Models\Season;
use App\Models\Size;
Expand Down Expand Up @@ -223,6 +224,7 @@ protected function form()
$form->select('brand_id', 'Бренд')->options(Brand::orderBy('name')->pluck('name', 'id'))->required()->default($productFromStock->brand_id);
$form->select('collection_id', 'Коллекция')->options(Collection::pluck('name', 'id'))->required();
$form->select('manufacturer_id', 'Производитель')->options(Manufacturer::pluck('name', 'id'));
$form->select('country_of_origin_id', 'Страна производитель')->options(CountryOfOrigin::pluck('name', 'id'));
$form->text('color_txt', 'Цвет');
$form->text('fabric_top_txt', 'Материал верха');
$form->text('fabric_inner_txt', 'Материал внутри');
Expand Down
2 changes: 2 additions & 0 deletions src/app/Admin/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
$router->resource('brands', BrandController::class);
$router->resource('manufacturers', ManufacturerController::class);
$router->resource('collections', CollectionController::class);
$router->resource('country-of-origin', CountryOfOriginController::class);
});

$router->group(['prefix' => 'config', 'namespace' => 'Config', 'as' => 'config.'], function (Router $router) {
Expand All @@ -64,6 +65,7 @@
$router->get('instagram-token', InstagramTokenForm::class);
$router->get('newsletter_for_registered', NewsletterForm::class);
$router->get('sending-tracks', SendingTracksForm::class);
$router->get('auto-order-statuses', AutoOrderStatusesForm::class);
});

$router->group(['prefix' => 'bnrs'], function (Router $router) {
Expand Down
1 change: 1 addition & 0 deletions src/app/Http/Controllers/Shop/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function show(int $id): View
$product = Product::with([
'tags',
'category',
'countryOfOrigin',
'availableSizes' => fn ($q) => $q->with(['stock' => fn ($q) => $q->with('city')]),
])->withTrashed()->findOrFail($id);
$this->productService->addToRecent($product->id);
Expand Down
8 changes: 8 additions & 0 deletions src/app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ public function manufacturer(): Relations\BelongsTo
return $this->belongsTo(ProductAttributes\Manufacturer::class);
}

/**
* Country of origin
*/
public function countryOfOrigin(): Relations\BelongsTo
{
return $this->belongsTo(ProductAttributes\CountryOfOrigin::class);
}

/**
* Slug для фильтра
*/
Expand Down
29 changes: 29 additions & 0 deletions src/app/Models/ProductAttributes/CountryOfOrigin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models\ProductAttributes;

use App\Traits\AttributeFilterTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

/**
* @property int $id
* @property string $name
* @property string $slug
* @property string $seo
*/
class CountryOfOrigin extends Model
{
use AttributeFilterTrait, HasFactory;

protected $guarded = ['id'];

protected static function boot(): void
{
parent::boot();
static::creating(function ($model) {
$model->slug = $model->slug ? $model->slug : Str::slug($model->name);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('country_of_origins', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->text('seo')->nullable();
$table->timestamps();
});

Schema::table('products', function (Blueprint $table) {
$table->foreignId('country_of_origin_id')->nullable()->constrained('country_of_origins')->nullOnDelete();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('products', function (Blueprint $table) {
$table->dropForeign(['country_of_origin_id']);
$table->dropColumn(['country_of_origin_id']);
});
Schema::dropIfExists('country_of_origins');
}
};
4 changes: 4 additions & 0 deletions src/resources/views/shop/product.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ class="bg-dark d-inline-flex alight-items-center m-1 px-2 py-0 text-white"
Высота каблука - {{ $product->heel_txt }} <br>
@endif

@if (!empty($product->countryOfOrigin->name))
Страна производитель - {{ $product->countryOfOrigin->name }} <br>
@endif

</div>
</div>

Expand Down

0 comments on commit 37a62bc

Please sign in to comment.