Count Referer in laravel 6

z3r0 :

I build a referral system Using Laravel and VueJS. i want count referral each users.

I implement count it from User Model

public function getCountReferrerAttribute()
{
    $user = User::where('referred_by', auth()->user()->name)->count();

    return $user;
}

And i try to get it from Vuejs

enter image description here

Its show Referrer Count for Every User...

User Schema

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('referral_code')->unique()->nullable();
    $table->string('referred_by')->nullable();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

Thanks in advance...

Helioarch :

Assuming you are calling this function for every user on your table, your query is getting all user that is referred by the currently logged in user because of auth()->user()->name, instead your function should use the name of the user itself.

So it will look something like this

public function getCountReferrerAttribute()
{
    $user = User::where('referred_by', $this->name)->count();

    return $user;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=293750&siteId=1