Update Resource

UpdateSingleResourceRequest is intended for when an existing resource must be updated.

Example Request

use Aedart\Http\Api\Requests\Resources\UpdateSingleResourceRequest;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;

class UpdateUser extends UpdateSingleResourceRequest
{
    public function findRecordOrFail(): Model
    {
        return User::findOrFail($this->route('id'));
    }

    public function mustEvaluatePreconditions(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'name' => 'required|string|min:2|max:100'
        ];
    }
}

Example Action

Route::patch('/users/{id}', function (UpdateUser $request) {
    $user = $request->record;
    $user->name = $request->validated('name');

    $user->save();

    return UserResource::make($user);
})->name('users.update');

Authorisation

Authorisation checks is performed by the authorizeFoundRecord() method (see source code for details). The request will check against a update ability. From the above shown examples, a users.update ability is checked.

Request Preconditions

See Show Request for additional information.