Q:

Show form errors on error page with validation in Laravel

If the $request->SN is either invalid or empty, I get an error like the following.

Trying to get property of non-object

Ticket::create([
    'user_id' => Laptop::where('SN', $request->SN)->first()->user_id,
    'laptop_id' => Laptop::where('SN', $request->SN)->first()->id,
    'title' => $request->title,
    'body' => $request->body,
    'laptop_ww' => $request->laptop_ww
]);

How can I display a custom error instead of this exception? By default, it goes to an error page with status 500.

A:

public function save(Request $request)
{
    try{
    Ticket::create([
        'user_id' => Laptop::where('SN', $request->SN)->first()->user_id,
        'laptop_id' => Laptop::where('SN', $request->SN)->first()->id,
        'title' => $request->title,
        'body' => $request->body,
        'laptop_ww' => $request->laptop_ww
    return view(your_address, "successfully saved");
    ]);
    }catch(Exception ex){
    return view(your_address, "Custom error here");
    }
  }

