Well hello there,
When cleaning up my controller class I was wondering where to put extra fields when creating a model. In the StoreAdvertisementRequest
class there are some validation rules applied.
Next I also want to make sure the new advertisement is coupled to a user_id
and a certain status_id
field.
But how does that work in combination with a make:request
request and the method $request->validated()
.
Is there a way to sneak these in?
public function store(StoreAdvertisementRequest $request): RedirectResponse
{
// pass these in...
$validated['user_id'] = Auth::id();
$validated['status_id'] = AdvertisementStatus::WAITING_FOR_PROPOSAL;
$advertisement = Advertisement::create($request->validated());
4
Answers
just merge the arrays
try this
You first validate the request using the
$request->validated()
method, then add extra fields to the validated request data:Just add this function to your ‘StoreAdvertisementRequest’ class,
it will automatically appended to
$request->validated()
.