Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

FormRequest problem ? and another question ...

Hello,

Here is a part of my controller code.

namespace App\Http\Controllers;

use App\Models\Category; use App\Http\Requests\CategoryFormRequest;

public function store(CategoryFormRequest $request) { $category = new Category; $this->hydrate($request, $category); $category->save(); return view('categories.show', compact('category')); }

private function hydrate(CategoryFormRequest $request, Category $category) { $category->title = $request->title; $category->description = $request->description; $category->user_id = Auth::user(); }

First questions : CategoryFormRequest extends FormRequest extends Request, so my code should work. But it doesn't work and the code in the store method isn't executed at all. And I have no error message ! But if I replace the CategoryFormRequest parameter by a Request parameter, it works fine. Perhaps you have an idea ?

Second question : where should I have my hydrate() method ? in the model or in the controller ?

Thanks for your help.

Vincent

0 likes
18 replies
MichalOravec's avatar

If you have set $fillable on the model you can use create

https://laravel.com/docs/7.x/eloquent#mass-assignment

Also set your relationships

https://laravel.com/docs/7.x/eloquent-relationships#one-to-many

namespace App\Http\Controllers;

use App\Models\Category; 
use App\Http\Requests\CategoryFormRequest;
use Illuminate\Support\Facades\Auth;

public function store(CategoryFormRequest $request) 
{ 
    $category Auth::user()->categories()->create($request->all());

    return redirect()->route('categories.show', $category); 
}

public function show(Category $category) 
{ 
    return view('categories.show', compact('category')); 
}

And after create it will be better if you redirect to the show method.

tykus's avatar

Nothing is returned from hydrate and the Category is not passed by reference, so you are saving an empty Category, not the hydrated one.

What does it benefit you to have the hydrate method at all?

vincent15000's avatar

Thank you for your help. You propose me another solution, but I'd like to find why it doesn't work. I explain my problem once again.

  1. Why the code inside the store() method is not executed and no error ? It should work, but it doesn't. Is there any reason ?
  2. To hydrate my model, the hydrate method should better be in the model or in the controller ?
tykus's avatar

If you want to pass by reference, then the hydrate method signature should be:

private function hydrate(&$category)
{
	// ... 
}
vincent15000's avatar

@tykus my problem is that the code inside the store method is not read, not executed at all. It is exactly the same as I write no code at all inside the method. What is blocking is the CategoryFormRequest as a parameter of the store() method.

store(Request $request) => it's working fine store(CategoryFormRequest $request) => it's not working

vincent15000's avatar

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CategoryFormRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; }

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'title' => 'required|min:5|max:100',
        'description' => 'nullable',
        'user_id' => 'required'
    ];
}

/**
 * Get custom messages for validator errors.
 *
 * @return array
 */
public function messages()
{
    return [
        'title.required' => 'Le titre est obligatoire.',
        'title.min' => 'Le titre doit avoir au moins 5 caractères.',
        'title.max' => 'Le titre doit avoir moins de 100 caractères.',
    ];
}

}

tykus's avatar

Validation fails?

'user_id' => 'required'

Is the user_id in the Request, or is it coming from the session:

$category->user_id = Auth::user()
vincent15000's avatar

Sorry how are you doing to write your code with a black background ?

vincent15000's avatar

@tykus the user_id is coming from the session, it is not in the request. That's why I add it manually in the hydrate method.

vincent15000's avatar

Ok thank you ... I have no error message, but I am displaying the validation errors. Strange isn't it ?

MichalOravec's avatar

Yeah just remove user_id because you store there an authenticated user. So you don't need it to validate.

tykus's avatar

@vincent15000 please mark the Best Reply above if your issue has been resolved.

As to your second question

where should I have my hydrate() method ? in the model or in the controller ?

The model already has hydrate methods, including fill, create, update, updateOrCreate etc. etc. - the only difference is these methods take an array of key/value pairs, so the fields must be fillable on the model. I would never see a compelling benefit to your approach above.

vincent15000's avatar

@tykus Ok I understand what you mean ... well ... I did'nt know the native fill() method ;) ... I will have a look ;). Thanks again.

Please or to participate in this conversation.