Skip to content

Commit

Permalink
Update model repository (#288)
Browse files Browse the repository at this point in the history
* feat: Update BrandRepository, setup new docs and add tests

* feat: Update CollectionRepository, setup new docs and add tests

* chore: fix code formatting

* fix: Price class helper and refactor factories

* refactor: Update money attribute column

---------

Co-authored-by: mckenziearts <[email protected]>
  • Loading branch information
mckenziearts and mckenziearts authored Aug 27, 2024
1 parent 11d9d1d commit 87f2941
Show file tree
Hide file tree
Showing 105 changed files with 72,403 additions and 22,358 deletions.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"composer-runtime-api": "^2.1"
},
"require-dev": {
"akaunting/laravel-money": "^5.2",
"bacon/bacon-qr-code": "^2.0",
"blade-ui-kit/blade-heroicons": "^2.0",
"danharrin/livewire-rate-limiting": "^0.3|^1.0",
Expand Down Expand Up @@ -66,8 +65,7 @@
},
"autoload-dev": {
"psr-4": {
"Shopper\\Tests\\": "tests/src",
"Shopper\\Tests\\Database\\Factories\\": "tests/database/factories"
"Shopper\\Tests\\": "tests/src"
}
},
"extra": {
Expand All @@ -82,7 +80,7 @@
"scripts": {
"cs": [
"pint",
"npm run prettier"
"npx run prettier"
],
"pint": "./vendor/bin/pint",
"test:pest": "pest --parallel",
Expand Down
3 changes: 1 addition & 2 deletions packages/admin/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shopper/framework",
"description": "A TALL Laravel e-commerce admin panel to create and manage online shop.",
"description": "A TALL Laravel Headless e-commerce admin panel to create and manage online shop.",
"keywords": [
"laravel",
"livewire",
Expand All @@ -18,7 +18,6 @@
},
"require": {
"php": "^8.2",
"akaunting/laravel-money": "^5.2",
"bacon/bacon-qr-code": "^2.0",
"danharrin/livewire-rate-limiting": "^0.3|^1.0",
"filament/forms": "^3.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/admin/config/components/brand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
|--------------------------------------------------------------------------
| Livewire Pages
|--------------------------------------------------------------------------
|
*/

'pages' => [
Expand All @@ -20,6 +21,7 @@
|--------------------------------------------------------------------------
| Livewire Components
|--------------------------------------------------------------------------
|
*/

'components' => [
Expand Down
188 changes: 86 additions & 102 deletions packages/admin/docs/content/brands.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Brands
Most e-commerce sites sell products from several manufacturers. And each supplier can be represented by a brand.

Unless you make your own products, you should always register the brands of your products in Shopper.
Unless you make your own products, you'll be registering your product's brands in Shopper.

If you sell your own products, you must at least create your company as a brand: this helps your customer find what they are looking for, and this can bring some valuable search engine points.

## Overview
The management of brands is exactly the same as the one done in most of the e-commerce website creation tools: only the name can change. It is mainly used to facilitate the navigation of customers in your catalog, as it is increasingly common to search for a specific brand.
The management of brands is exactly the same as the one done in most of the e-commerce website creation tools: only the name can change.
It is mainly used to facilitate the navigation of customers in your catalog, as it is increasingly common to search for a specific brand.

<div class="screenshot">
<img src="/img/screenshots/{{version}}/brands.png" alt="Brands">
Expand All @@ -18,142 +19,103 @@ New brands are automatically activated and available for your online store, even
### Fields
The model used is `Shopper\Framework\Models\Shop\Product\Brand`.

| Name | Type | Required | Notes |
|-------------|-----------|------------|------------|
| `id` | autoinc | | auto |
| `name` | string | yes | |
| `slug` | string | yes | Unique, default value is auto generated using brand name |
| `website` | string | no | Nullable |
| `description` | longText | no | Nullable |
| `position` | string | no | Default `0` |
| `is_enabled` | boolean | no | Default `false` |
| `seo_title` | string | no | Nullable, for seo title max length is 60 |
| `seo_description` | string | no | Nullable, for seo description max length is 160 |
| Name | Type | Required | Notes |
|-------------------|----------|----------|----------------------------------------------------------|
| `id` | autoinc | | auto |
| `name` | string | yes | |
| `slug` | string | no | Unique, default value is auto generated using brand name |
| `website` | string | no | Nullable |
| `description` | longText | no | Nullable |
| `position` | string | no | Default `0` |
| `is_enabled` | boolean | no | Default `false` |
| `seo_title` | string | no | Nullable, for seo title max length is 60 |
| `seo_description` | string | no | Nullable, for seo description max length is 160 |

:::tip
Models are customizable, and we recommend changing the **Brand** model when you configure your site.
To change the model you need to look at the configuration file `config/shopper/system.php` at the key `models`.
To change the model you need to look at the configuration file `config/shopper/models.php`.
:::

```php
use Shopper\Core\Models;

return [
'models' => [
/*
* Eloquent model should be used to retrieve your brands. Of course,
* it is often just the "Brand" model but you may use whatever you like.
*
* The model you want to use as a Brand model needs to extends the
* `\Shopper\Framework\Models\Shop\Product\Brand` model.
*/
'brand' => \Shopper\Framework\Models\Shop\Product\Brand::class, // [tl! focus]

/*
* Eloquent model should be used to retrieve your categories. Of course,
* it is often just the "Category" model but you may use whatever you like.
*
* The model you want to use as a Category model needs to extends the
* `\Shopper\Framework\Models\Shop\Product\Category` model.
*/
'category' => \Shopper\Framework\Models\Shop\Product\Category::class,
]
// ...
'brand' => Models\Brand::class,
];
```

1. Create your own model that you have to use
1. Create your own Model
```bash
php artisan make:model Brand
```
Once the `app/Models/Brand.php` model is created in our app folder, we will make it extend from the `Shopper\Framework\Models\Shop\Product\Brand` model available in Shopper.
Once the `app/Models/Brand.php` model is created in your app folder, you need to extend from the `Shopper\Core\Models\Brand` Model.

2. Extend our Brand model from the Brand Shopper Model
```php
namespace App\Models;
use Shopper\Framework\Models\Shop\Product;
use Shopper\Core\Models\Brand as Model;
class Brand extends Product\Brand
class Brand extends Model
{
}
```

3. Update `brand` key for the model on the `system.php` config file to use our new model
3. Update `brand` key for the model on the `models.php` config file to use our new model
```php
return [
'models' => [
/*
* Eloquent model should be used to retrieve your brands. Of course,
* it is often just the "Brand" model but you may use whatever you like.
*
* The model you want to use as a Brand model needs to extends the
* `\Shopper\Framework\Models\Shop\Product\Brand` model.
*/
'brand' => \App\Models\Brand::class, // [tl! focus]
/*
* Eloquent model should be used to retrieve your categories. Of course,
* it is often just the "Category" model but you may use whatever you like.
*
* The model you want to use as a Category model needs to extends the
* `\Shopper\Framework\Models\Shop\Product\Category` model.
*/
'category' => \Shopper\Framework\Models\Shop\Product\Category::class,
]
];
'brand' => Models\Brand::class, // [tl! --]
'brand' => \App\Models\Brand::class, // [tl! ++]
```

### Components
Livewire components for managing brands are available in the component configuration file `config/shopper/components.php`.
By default, brands Livewire components are not published. To customize components, you must publish them.

```bash
php artisan shopper:component:publish brand
```

This command will publish all Livewire components used for brand management (from pages to form components).
Once you've published the component, you can find it in the `brand.php` locate in the `config/shopper/components` folder.
```php
use Shopper\Framework\Http\Livewire;
use Shopper\Livewire;
return [
'livewire' => [
'brands.browse' => Livewire\Brands\Browse::class,
'brands.create' => Livewire\Brands\Create::class,
'brands.edit' => Livewire\Brands\Edit::class,
'tables.brands-table' => Livewire\Tables\BrandsTable::class,
];
'pages' => [
'brand-index' => Livewire\Pages\Brand\Index::class,
],
'components' => [
'slide-overs.brand-form' => Livewire\SlideOvers\BrandForm::class,
],
];
```
For handling tables in Shopper, we use [Laravel Livewire Tables](https://github.com/rappasoft/laravel-livewire-tables) package by Anthony Rappa.

## Manage Brands
The brands are accessible via the Brands Menu on the left sidebar. The display page is rendered by the Livewire component `Shopper\Framework\Http\Livewire\Brands\Browse` and for the display of the brands table is the component `Shopper\Framework\Http\Livewire\Tables\BrandsTable`.
The brands are accessible via the Brands Menu on the left sidebar.
You can update the livewire page component in the configuration file to use your own.
To create a Livewire page for Shopper you need to run the following command
```bash
php artisan make:shopper-page Brand
```
You can modify them in the component configuration file to use your own.
This page will extend shopper's default layout, and you can render the view you want.

### Create brand
Click on the "Create" button on the brands page, and a creation form appears.
Click on the "Create" button on the brands page, which will display and slideover.

<div class="screenshot">
<img src="/img/screenshots/{{version}}/create-brand.png" alt="Create brand">
<img src="/img/screenshots/{{version}}/brand-form.png" alt="Create brand">
<div class="caption">Create brand</div>
</div>

Save your changes in order to be taken back to the brand's list. Required fields are marked with an **asterisk (*)**
The SEO section allows you to define how your brand information should be displayed in search engines. To modify the content you click on the button "Edit SEO preview"
<div class="screenshot">
<img src="/img/screenshots/{{version}}/brand-seo.png" alt="brand seo form">
<div class="caption">Brand SEO</div>
</div>
By fill the data you will have a direct preview of the content.
### Delete brand
To delete, deactivate or activate brands, you need to select the brand you want to delete and then click on the "Bulk Actions" button to choose the action you want to perform.
<div class="screenshot">
<img src="/img/screenshots/{{version}}/delete-brand.png" alt="delete brand">
<div class="caption">Delete brand</div>
</div>
## Retrieve Data
Once you have your brands and you want to display them in your store, you can retrieve them this way in your controller
Once you have your brands you want to display them in your store, you can retrieve them this way in your controller
```php
namespace App\Http\Controllers;
Expand All @@ -164,7 +126,7 @@ use Carbon\Carbon;
class HomeController extends Controller
{
public function home()
public function __invoke()
{
$products = Product::with('categories', 'attributes')
->publish()
Expand All @@ -187,7 +149,7 @@ class HomeController extends Controller
```
:::tip
Knowing that your brands can be displayed on several pages and places in your store, you can create a **View Composer** ([read more about View Composer](https://laravel.com/docs/9.x/views#view-composers)).
Knowing that your brands can be displayed on several pages and places in your store website, you can create a **View Composer** ([read more about View Composer](https://laravel.com/docs/9.x/views#view-composers)).
:::
- Create your brand composer undo a custom folder `app/View/Composers`
Expand All @@ -200,16 +162,17 @@ use Illuminate\View\View;
class BrandsComposer
{
public function compose(View $view)
{
$view->with('brands', Brand::enabled()->get()->take(12));
}
public function compose(View $view): void
{
$view->with('brands', Brand::enabled()->get()->take(12));
}
}
```
- Then you have to add it in your **AppServiceProvider**
```php
namespace App\Providers;
use App\View\Composers\BrandsComposer;
Expand All @@ -218,10 +181,10 @@ use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
View::composer('partials.brands', BrandsComposer::class); // [tl! focus]
}
public function boot(): void
{
View::composer('partials.brands', BrandsComposer::class); // [tl! focus]
}
}
```
Expand All @@ -231,3 +194,24 @@ And in your front-end you can browse your brands to have a display like this
<img src="/img/screenshots/{{version}}/brand-lists.png" alt="Brands preview list">
<div class="caption">Brands example list</div>
</div>
## Disabled Brand
Sometimes in your store, you won't have a brand name for your products (it's rare but possible), especially if you make them yourself.
In this case, you can hide brands on the sidebar and disabled all brand-related functionalities in your store.
To disable brand-related functionalities, open the `features.php` configuration file in the `config/shopper` folder and set the brand key to disable.
```php
use Shopper\Enum\FeatureState;
return [
'attribute' => FeatureState::Enabled,
'brand' => FeatureState::Enabled, // [tl! --]
'brand' => FeatureState::Disabled, // [tl! ++]
'category' => FeatureState::Enabled,
'collection' => FeatureState::Enabled,
'discount' => FeatureState::Enabled,
'review' => FeatureState::Enabled,
];
```
2 changes: 1 addition & 1 deletion packages/admin/docs/content/categories.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ use Shopper\Core\Models;

return [
// ...
'brand' => \App\Models\Brand::class,
'brand' => \App\Models\Brand::class,

// ...
'category' => Models\Category::class, // [tl! focus]
Expand Down
Loading

0 comments on commit 87f2941

Please sign in to comment.