Skip to content
Snippets Groups Projects
Product.php 4.31 KiB
Newer Older
Shizuco's avatar
Shizuco committed
<?php

Shizuco's avatar
Shizuco committed
declare (strict_types = 1);

Shizuco's avatar
Shizuco committed
namespace App\Nova;

use App\Models\Category;
use App\Models\Retailer;
use App\Rules\isSKUAvailable;
use Illuminate\Database\Eloquent\Model;
Shizuco's avatar
Shizuco committed
use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsToMany;
Shizuco's avatar
Shizuco committed
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Select;
Shizuco's avatar
Shizuco committed
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
Shizuco's avatar
Shizuco committed
use Laravel\Nova\Http\Requests\NovaRequest;
use Outl1ne\MultiselectField\Multiselect;
Shizuco's avatar
Shizuco committed
use Vyuldashev\NovaMoneyField\Money;
Shizuco's avatar
Shizuco committed

class Product extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\Product::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
Shizuco's avatar
Shizuco committed
        'name', 'sku',
    public static $categories = [];

    public static function attach(Model $model)
    {
        $categories = self::$categories;
        foreach ($categories as $category) {
            $model->categories()->attach($category);
        }
    }

    public static function afterCreate(NovaRequest $request, Model $model)
    {
        self::attach($model);
    }

    public function getAllRetailers()
    {
        $list = array();
        foreach (Retailer::get() as $retailer) {
            $list[$retailer['id']] = $retailer['name'];
        }
        return $list;
    }

    public function getAllCategories()
    {
        $list = array();
        foreach (Category::get() as $retailer) {
            $list[$retailer['id']] = $retailer['name'];
        }
        return $list;
    }

Shizuco's avatar
Shizuco committed
    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function fields(NovaRequest $request)
    {
        return [
            ID::make()->sortable(),
Shizuco's avatar
Shizuco committed

            Text::make('Name')
                ->sortable()
                ->rules('required', 'max:255', 'min:2'),

            Select::make('Retailer', 'retailer_id')
                ->options(
                    $this->getAllRetailers()
                )
Shizuco's avatar
Shizuco committed
                ->rules('required')
                ->placeholder('Choose retailer')
Shizuco's avatar
Shizuco committed
                ->displayUsingLabels(),
            BelongsToMany::make('Categories')
                ->rules('required'),

            Multiselect::make('Categories')
                ->options(
                    $this->getAllCategories())
                ->rules('required')
                ->fillUsing(
                    function ($request, $model) {
                        self::$categories = $request->categories;
                        return null;
                    })
                ->hideFromIndex()
                ->hideFromDetail()
                ->hideWhenUpdating(),

            Text::make('SKU')
                ->sortable()
                ->creationRules('required', new isSKUAvailable(intval($request->retailer_id), 0)),
Shizuco's avatar
Shizuco committed
            Money::make('Price')
                ->sortable()
                ->rules('required'),

            Textarea::make('Description')
                ->sortable()
                ->rules('required'),
Shizuco's avatar
Shizuco committed
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function cards(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function filters(NovaRequest $request)
    {
        return [
            new Filters\ProductPrice(),
            new Filters\ProductCategory()
Shizuco's avatar
Shizuco committed
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function lenses(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function actions(NovaRequest $request)
    {
        return [];
    }
}