Newer
Older
use App\Models\Retailer;
use App\Rules\isSKUAvailable;
use Illuminate\Database\Eloquent\Model;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Textarea;
use Outl1ne\MultiselectField\Multiselect;
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 = [
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;
}
/**
* 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(),
->rules('required', 'max:255', 'min:2'),
Select::make('Retailer', 'retailer_id')
->options(
$this->getAllRetailers()
)
->placeholder('Choose retailer')
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)),
->sortable()
->rules('required'),
Textarea::make('Description')
->sortable()
->rules('required'),
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
];
}
/**
* 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 [];
}
/**
* 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 [];
}
}