Home > Software design >  send entity for controller not working, php laravel 7
send entity for controller not working, php laravel 7

Time:10-05

in blade template

<form action="{{ route('leads.courses.restore', $lead->id) }}" method="POST">
  @csrf
  <button type="submit">
    Restore 
  </button>
</form>

in routes

Route::post('leads/courses/{lead}/restore', 'LeadsCoursesController@restore')->name('leads.courses.restore');

in controller

public function restore(Lead $lead)
  {
    dd("ok");
  }

but i receive HTTP code 404, if i remove the parameter in controller receive "ok".

CodePudding user response:

This problem happens because you try to find a soft deleted record so to fix it add to your route withTrashed() to be like this

Route::post('leads/courses/{lead}/restore', 'LeadsCoursesController@restore')
   ->withTrashed()
   ->name('leads.courses.restore');

for more info check this part in docs

CodePudding user response:

Instead of:

{{ route('leads.courses.restore', $lead->id) }}

Should be:

{{ route('leads.courses.restore', ['lead' => $lead->id]) }}

Or in Laravel 8

 {{ route('leads.courses.restore', ['lead' => $lead]) }}

CodePudding user response:

I found out what was happening, as the name of the function suggests, I intended to restore a deleted lead and with that I was passing the id of a deleted lead as a parameter resulting in null when instantiating. To solve, I sent the id and found the record using the Laravel function below.

public function restore(string $leadId)
    {
        $lead = Lead::withTrashed()->find($leadId);
        dd($lead);
    }
  • Related