Newer
Older
<?php
declare (strict_types = 1);
namespace App\Nova\Actions;
use App\Models\Product;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Http\Requests\NovaRequest;
class CompareActionChangeValues extends Action
{
use InteractsWithQueue, Queueable;
public $onlyOnIndex = true;
protected ?string $sku;
protected ?int $id;
public function __construct(?string $sku, ?int $id)
{
$this->sku = $sku;
$this->id = $id;
}
public function handle(ActionFields $fields, Collection $models)
{
return Action::danger('Choose too much rows!');
}
return Action::danger('All fields is required!');
}
$productFromChange = Product::where('id', $models[0]->id)->firstOrFail();
$changeField = $fields->changeField;
Product::where('id', $fields->changeModel)->update([$fields->changeField => $productFromChange[strtolower($changeField)]]);
}
public function fields(NovaRequest $request)
{
$products = 0;
if ($this->sku) {
$products = Product::where('sku', $this->sku)->where('id', '!=', $this->id)->pluck('name', 'id')->all();
}
return [
Select::make('Model to change', 'changeModel')
->options(
$products
)
->required(),
Select::make('Field to change', 'changeField')
->options([
'price' => 'price',
'description' => 'description',
'name' => 'name',
])
->required(),
];
}
}