1

I generated my StorePostRequest using artisan make command.
I defined rules on the rules method doing this:

public function rules()
{
    return [
        'title' => 'required|min:3|max:255',
        'slug' => ['required', Rule::unique('posts', 'slug')],
        'thumbnail' =>'required|image',
        'excerpt' => 'required|min:3',
        'body' => 'required|min:3',
        'category_id' => 'required|exists:categories,id'
    ];
}

However, in my PostController, I'm not able to get validated inputs except thumbnail using the safe()->except('thumbnail') like explained here

I'm getting the error

BadMethodCallException
Method App\Http\Requests\StorePostRequest::safe does not exist.
Nel
  • 454
  • 5
  • 14
  • `$validated = $request->safe()->except(['thumbnail'])` – JEJ Aug 25 '21 at 11:13
  • Thanks for your reply. However, ``$request->safe()->except(['thumbnail'])`` doesn't work either – Nel Aug 25 '21 at 11:16
  • 1
    You can use `only()` to get some specified inputs `$validated = $request->only(['// all keys you need']); – JEJ Aug 25 '21 at 11:17
  • thanks for your help, ``$request->except('thumbnail')`` worked. – Nel Aug 25 '21 at 14:10
  • 1
    @Nel Word of warning if you use the $request->except() you are getting all the requests fields even those that did not pass the validation! – Juha Vehnia Jan 31 '22 at 04:33

2 Answers2

4

Check your laravel/framework version by running

php artisan --version

The safe method found on the FormRequest class was only added in version 8.55.0.

Just good to keep in mind that just because you're on a version 8 of laravel framework, that doesn't mean you'll have all methods and properties found in the laravel 8.x docs. That is unless you're on the current latest version 8 of course.

Mike Mellor
  • 1,316
  • 17
  • 22
3

Using the except() method directly on $request worked. Thanks to @JEJ for his help.

$request->except('thumbnail');
Nel
  • 454
  • 5
  • 14
  • This is not safe though. The resulting collection may include key/values that are not present in the rules list as they will pass unfiltered. – Egemenk Aug 08 '22 at 20:52