PHP code example of akbarali / action-data

1. Go to this page and download the library: Download akbarali/action-data library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

akbarali / action-data example snippets


StoreIncomeActionData::createFromRequest($request);

StoreIncomeActionData::createFromArray($array);

StoreIncomeActionData::createFromJson($json);

$actionData->addValidationRule('id', 'eException();

$userId = Auth::id();
$actionData->set('data', '*.user_id', $userId);
$actionData->addValidationRule('data.*.user_id', '

$actionData->toArray();
// or  
$actionData->all();
// or
$actionData->toSnakeArray();

$actionData->only(['id', 'name']);

'providers' => [
...
\Akbarali\ActionData\Providers\ActionDataServiceProvider::class,
]

public function store(StoreIncomeActionData $actionData)
{
//This 

if($actionData->isUpdate()){
// Update
}else{
// Create
}

class StorePotentialPartnersActionData extends \Akbarali\ActionData\ActionDataBase
{
    public ?int    $id;
    public ?int    $agent_id;
    public ?string $phone;

    public ?string $full_name;
    public ?string $address;
    public ?string $description;
    /** @var array<UploadedFile>|null */
    public ?array $images    = [];
    public ?array $oldImages = [];

    protected function prepare(): void
    {
        $this->rules = [
            'id'          => 'nullable|numeric|exists:potential_partner,id',
            'full_name'   => '

/**
 * @param int     $id
 * @param Request $request
 * @return RedirectResponse
 * @throws ValidationException
 */
public function update(int $id, Request $request): RedirectResponse
{
   try {
        $request->request->set('id', $id);
        $this->storePotentialPartner(StorePotentialPartnersActionData::fromRequest($request)->updated());
        return to_route('page.index')->with('message', trans('all.updated'));
    } catch (OperationException|ActionDataException|QueryException $e) {
        return back()->withInput()->withErrors($e->getMessage());
   }
}

public function isUpdate(): bool
{
    return $this->updated ?? (isset($this->id) && $this->id > 0);
}

class StorePartnersActionData extends \Akbarali\ActionData\ActionDataBase
{
    public readonly int $id;

    protected function prepare(): void
    {
        $this->rules = [
            'id'          => 'nullable|numeric|exists:potential_partner,id',
        ];
    }
}

class PartnerController extends Controller{

    public function update(int $id, Request $request): RedirectResponse
    {
       try {
            $request->request->set('id', $id);
            $this->storePartner(StorePartnersActionData::fromRequest($request)->updated());
            return to_route('page.index')->with('message', trans('all.updated'));
        } catch (OperationException|ActionDataException|QueryException $e) {
            return back()->withInput()->withErrors($e->getMessage());
       }
    }
    
    public function storePartner(StorePotentialPartnersActionData $actionData): RedirectResponse
    {
        $actionData->id = 1; // Exception: Error
    }
}


$actionData->addValidationRule('pinfl', 'unique:users,pinfl');
$actionData->addValidationRules([
	'pinfl' => 'unique:users,pinfl',
	'phone' => 'unique:users,phone',
	'passport' => 'unique:users,passport',
]);
$actionData->addValidationValue('