John Davidson

php - Why do my routes fail after calling a destroy function?

0 comments
Message:


There have been a few iterations of this code but I will only show what has worked best so far (no errors).


Destroy Function:


public function destroy($locale, $id)
{
Component::where('id', $id)->delete();

$locale = App::getLocale();

return route('components.index', compact('locale'));
}

Index Function (referenced above):


public function index($locale)
{
parent::lang($locale);

$components = Component::paginate(10);

return view('production/index-component', compact('components'));
}

Relevant Route:


Route::group(['prefix' => '{locale}'], function() {
Route::resource('/components', ComponentController::class);
});

Resulting URL


The destroy function works otherwise, records are deleted (upon going to the correct URL). Ideally this should simply redirect the user back to {locale}/components.


Thank you in advance!



My Answer:
The issue with your code is that you are returning a route in the destroy function instead of redirecting to that route.

Instead of returning the route in the destroy function, you should use the redirect() function to redirect the user to the index route after deleting the record.

Here is how you can modify your destroy function:

php
public function destroy($locale, $id)
{
Component::where('id', $id)->delete();

return redirect()->route('components.index', $locale);
}


By using the redirect() function, you are instructing the browser to redirect to the specified route after the record has been deleted. This should solve the issue with your routes failing after calling the destroy function.

Rate this post

3 of 5 based on 7854 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us