0
public function doFollow($id)
{
    $user = $this->user->getById($id);        

    if (Auth::user()->isFollowing($id)) {
        Auth::user()->unfollow($id);
    } else {
        Auth::user()->follow($id);

        $user->notify(new FollowedUser(Auth::user()));
    }

    return redirect()->back();
}
3
  • Do you have $this->user in your __construct() method? Commented Jun 14, 2020 at 20:37
  • This is where the issue is $user = $this->user->getById($id); ... try this instead $user = User::find($id); Commented Jun 14, 2020 at 20:41
  • 2
    Where is $this->user defined? Commented Jun 14, 2020 at 20:47

1 Answer 1

1

Before you can check your UserController class does have $user property if is not you must initialize in class constructor.

use App\User;

class UserController extends Controller{

    private $user;

    public function __construct(User $user){
        // İnitialize user property.
        $this->user = $user;
    }

}

If problem was not solved. Check your getById method as below.

Your getById method returns null object and you try access property in null object so it is undefined property.

Look getById method for return existing User Object. Check your query and change find($id) to findOrFail($id). It throws Error when query not found user by given $id.

Or you can check and do job if user exists:

public function doFollow($id)
{
    $user = $this->user->getById($id);

    if($user){
        if (Auth::user()->isFollowing($id)) {
            Auth::user()->unfollow($id);
        } else {
            Auth::user()->follow($id);

            $user->notify(new FollowedUser(Auth::user()));
        }
    }
    return redirect()->back();
}
Sign up to request clarification or add additional context in comments.

Comments

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.