skip to Main Content

Scenario: A post has a delete button in all posts list view and the postShow view. So if the post is deleted from the list it will return back()->with(‘success’, ‘Some message’) but if we delete it from the postShow view then redirect to the home.

(in short if the the post is deleted via show page then it redirects to home else redirect back)

{
    $post = Post::findOrFail($id);
    $post->delete();

    if(route('post.show', $post->slug)){
        return redirect()->route('home')->with(["message" => "Your Post has been deleted successfully");
    } else {
        return back()->with(["message" => "Your Post has been deleted successfully");
    }
}

This is the code but it is always redirecting to home.

3

Answers


  1. In my opinion the simplest way to approach this would be to include a query parameter when you are sending the request from the all posts list view.

    From the all posts list view you could add the parameter redirectTo with the value home.

    Then in the controller based on the redirectTo parameter you can implement your own redirection logic.

    {
        $post = Post::findOrFail($id);
        $post->delete();
        $redirectTo = $request->get('redirectTo') ?? "";
    
        if($redirectTo == "home"){
            return redirect()->route('home')->with(["message" => "Your Post has been deleted successfully");
        } else {
            return back()->with(["message" => "Your Post has been deleted successfully");
        }
    }
    
    Login or Signup to reply.
  2. route('post.show', $post->slug) will always evaluate as true, isn’t it? Hence it is always redirecting to the home page.


    Note that the below method is not hack proof, but the task at hand is too trivial to make it hack proof anyway.

    In your routes, you can have the route for delete as below with additional parameter of fromList

    Route::delete('/post/delete/{id}/{fromList}', ...)->name('post.delete');
    

    In your view files, the delete route link would be,

    List pageroute('post.delete',['id' => $yourID, 'fromList' => 1])

    Show pageroute('post.delete',['id' => $yourID, 'fromList' => 0])

    Then in your controller’s delete method, you can check where to redirect based on the fromList flag.

    <?php
    
    public function delete($id, $fromList){
        $post = Post::findOrFail($id);
        $post->delete();
    
        if($fromList == '0'){
            return redirect()->route('home')->with(["message" => "Your Post has been deleted successfully"]);
        } 
    
        return back()->with(["message" => "Your Post has been deleted successfully"]);
    }
    
    Login or Signup to reply.
  3. The code you provided is not working as expected because the condition if(route('post.show', $post->slug)) will always evaluate to true. This condition is checking if the route "post.show" with the post’s slug exists, and it will exist as long as you have defined the "post.show" route in your routes file.

    To achieve the desired behavior of redirecting to the home page if the post is deleted from the "postShow" view and redirecting back otherwise, you can modify your code like this:

    $post = Post::findOrFail($id);
    $isDeletedFromShowPage = false; // Initialize a variable to track if deleted from show page
    
    if (/* Add your condition to check if deleted from show page */) {
        // Delete logic here
        $isDeletedFromShowPage = true;
        $post->delete();
    }
    
    if ($isDeletedFromShowPage) {
        return redirect()->route('home')->with(["message" => "Your Post has been deleted successfully"]);
    } else {
        return back()->with(["message" => "Your Post has been deleted successfully"]);
    }
    

    In the condition where you check if the post is deleted from the "postShow" view, you should add your logic to determine whether it was deleted from that specific view. If it was deleted from the show page, set the $isDeletedFromShowPage variable to true, and then use that variable to determine the redirect behavior.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search