1

I am trying to simple error handling whereby if a query doesn't return anything I want to redirect the user to a 404 page, but for now I just echoing a message to see if it triggers, here is my code

public function show($user_id, $username) {

    $user = User::where('id', $user_id)->where('name', $username)->get();

    if (count($user = 0)) {
        echo "REEEEEEE";
    }
    else 
    {
        $user->load('comments.users', 'posts.users');

        return $user;
    }
    //return view ('users.show', compact('user'));

}

This only echos the message and doesn't load the relationships or return the user if the query returns something, am I doing something wrong?

1 Answer 1

1

Change your code like this:

$userCount = User::where('id', $user_id)->where('name', $username)->count();

if ($userCount == 0)

In your code you have a wrong if statement:

if (count($user = 0)) becomes if (count(0))

Sign up to request clarification or add additional context in comments.

2 Comments

It works nice one boss, any idea why my code doesn't work? Incorrect syntax?
@user6073700 you mistyped, count($user = 0) is not a correct statement. You assign user to 0 and then if (count(0)). If it helped mark my answer as right, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.