Cleaning up a controller

Posted 4 months ago by timgavin

I'm using hash IDs to display a Post ID publicly so I have to decode that ID every time I want to find a Post, which is getting messy. Is there a way to automatically decode the hash ID for each one of my controller methods?

Here's an example of what I'm currently using.

public function destroy($id)
{
    // Is there a way to decode the hash ID globally in the controller so I can get rid of the following line?
    $id = Post::decodeHashId($id);

    $delete = Post::findOrFail($id)->delete();
    
    if($delete) {
        return response()->json(['success' => 'success'], 204);
    }

    return response()->json(['success' => 'error'], 500);
}

Please sign in or create an account to participate in this conversation.