How to view request data inside a Form class
1 September 2024 (Updated 1 September 2024)
Suppose you have a StorePersonRequest
class like this:
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StorePersonRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true; // Handled by the auth middleware
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
return [
'name' => 'required|max:255',
'position' => 'required|max:255',
'about' => 'nullable',
'company_id' => [
'required',
'int',
Rule::exists('companies', 'id')->where(function (Builder $query) {
return $query->where('user_id', auth()->user()->id);
}),
],
];
}
}
And a PersonController#store
method that uses that request:
/**
* Store a newly created resource in storage.
*/
public function store(StorePersonRequest $request)
{
$params = $request->validated() + [
'user_id' => currentUser()->id,
];
Person::create($params);
return redirect()
->route('people.index')
->with('success', 'Person created successfully');
}
How can you inspect the incoming data inside the request class?
You can either add a breakpoint in the request class’s authorize()
method and check $this->input()
or you can do a dump($this->input()
or dd($this->input())
:
Tagged:
Laravel