Skip to content
Snippets Groups Projects
Commit b8175f7c authored by Shizuco's avatar Shizuco
Browse files

feat: ability to change one row from anothers data

parent 2e636aee
No related branches found
No related tags found
No related merge requests found
<?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\Fields\Text;
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)
{
if (count($models) > 1)
{
return Action::danger('Choose too much rows!');
}
if(!$fields->changeModel || !$fields->changeField)
{
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(),
];
}
}
......@@ -126,6 +126,8 @@ public function lenses(NovaRequest $request)
public function actions(NovaRequest $request)
{
return [];
return [
new Actions\CompareActionChangeValues($this->sku, $this->id),
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment