Skip to content

Instantly share code, notes, and snippets.

@emotality
Last active August 12, 2020 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emotality/155bfaf0222e0f977158f3d265fb360c to your computer and use it in GitHub Desktop.
Save emotality/155bfaf0222e0f977158f3d265fb360c to your computer and use it in GitHub Desktop.
Laravel 7 Request stub for store & update

Laravel 7 Request

Overview

This request stub is to demonstrate how it is possible to share a single form request for the same model, to store and to update the model.

Install

  1. If you are using Laravel 7, you can now override default stubs:
php artisan stub:publish
  1. Replace the below request.stub content with yours at stubs/request.stub

  2. Create a request like you normally would, for example:

php artisan make:request Admin/UserRequest

Usage

In this form request you can manipulate your data, before and after validation takes place, before returning it to your controller to avoid clutter in your controllers.

Store:

/**
 * Store a newly created resource in storage.
 *
 * @param \App\Http\Requests\Admin\UserRequest $request
 * @return \Illuminate\Http\RedirectResponse
 */
public function store(UserRequest $request)
{
    $user = User::create($request->validated());
    
    // user stored

    return redirect()->route('admin.users.show', $user);
}

Update:

/**
 * Update the specified resource in storage.
 *
 * @param \App\Http\Requests\Admin\UserRequest $request
 * @param \App\User $user
 * @return \Illuminate\Http\RedirectResponse
 */
public function update(UserRequest $request, User $user)
{
    $user->update($request->validated());
    
    // user updated

    return redirect()->route('admin.users.show', $user);
}

For this to work, you would need to spoof your form method to PUT when editing your model:

edit.blade.php:

<form method="POST" action="{{ route('admin.users.update', $user) }}">
    @csrf
    @method('PUT')

    <input type="hidden" name="model_id" value="{{ $user->id }}">
    ...
</form>

Contribute

Please comment if you have suggestions or better solutions, then I'll update my post. Thanks! 😎

<?php
namespace {{ namespace }};
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class {{ class }} extends FormRequest
{
/**
* Check if it's a PUT request.
*
* @var bool $is_updating
*/
protected $is_updating = false;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() : bool
{
return true;
}
/**
* Get data to be validated from the request.
*
* @return array
*/
public function validationData() : array
{
$this->is_updating = $this->isMethod('put');
return $this->all();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() : array
{
return $this->is_updating ? $this->updateRules() : $this->createRules();
}
/**
* Get the create validation rules.
*
* @return array
*/
public function createRules() : array
{
return [
// Example
'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')],
];
}
/**
* Get the update validation rules.
*
* @return array
*/
public function updateRules() : array
{
return [
// Example
'model_id' => ['bail', 'required', 'integer', 'exists:models,id'],
'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')->ignore($this->model_id)],
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages() : array
{
return [];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes() : array
{
return [];
}
/**
* Get the validated data from the request.
*
* @return array
*/
public function validated() : array
{
$validated = $this->validator->validated();
// Validated data for POST and PUT requests
return $this->is_updating ? $this->updateData($validated) : $this->createData($validated);
}
/**
* Get the validated data for storing.
*
* @param array $validated
* @return array
*/
private function createData(array $validated) : array
{
// Validated data for POST request (store)
return $validated;
}
/**
* Get the validated data for updating.
*
* @param array $validated
* @return array
*/
private function updateData(array $validated) : array
{
// Validated data for PUT request (update)
return $validated;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment