when i delete something from database ... lets say a blog , i might want to redirect the link for that blog to another link instead of 404 page
i have a table called setting_redirects :
setting_redirects : id , deleted_link , redirect_link
i'll store the deleted links and new link in this table
this is how i show a blog
function show( $blog_id ){
$blog = Blog::findOrFail($blog_id);
}
the problem is if it's not found findOrFail will automatically redirect user to 404 page ... i want to be able to checksetting_redirects to see if a redirect is available for that blog .... if so redirect to that new link otherwise go to 404
something like this
function show( $blog_id ){
$blog = Blog::find($blog_id);
if(!$blog)
{
$redirect_available = SettingRedirect::where('deleted_link ' , Request::url() ) ->first();
if($redirect_available )
return redirect( $redirect_available-> redirect_link );
else
abort(404);
}
}
but i want this for all my tables not only blogs and i dont want to write this code in all of my controllers
is there anyway to do this without changing all my controllers ? maybe a middleware before going to 404 page ?
source https://stackoverflow.com/questions/68534659/calling-a-function-before-redirecting-to-404-page-on-findorfail
Comments
Post a Comment