For some reason I am unable to validate data

2020-05-27 php laravel controller

I am trying to validate my data but for some reason I am getting this error " Trying to get property 'title' of non-object"

Here's My Controller:-

public function store(Request $request)

{
  $data = request()->validate([
        'title' => 'required',
        'body' => 'required',
      ]);


     Post::create([
            'title'=>$data->title,
            'body'=>$data->body,
            'created_by'=>$request->created_by,
            'user_id'=>Auth::user()->id,
            'filled_by'=>Auth::user()->uuid,

          ]);


  return redirect('/home');


}

Answers

request()->validate([]); will return Array with validated data. You are using $data->title but $data is NOT an Object but Array.

Instead use

'title' => $data['title'],

Related