vendredi 25 octobre 2019

Displaying a list of users with information on the number of votes in Laravel

I am beginner in Laravel. I use in my project Laravel 5.8.

I have this code:

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->char('enable', 1)->default(0);
            $table->string('email', 120)->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
....
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';
        });
    }


Schema::create('comments', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('commentable_type');
        $table->bigInteger('commentable_id');
        $table->char('enable', 1)->default(0);
        $table->char('to_stats', 1)->default(0);
        $table->tinyInteger('rating')->default(0);
        $table->text('content');
        $table->dateTime('date_time');
        $table->ipAddress('ip');
        $table->engine = "InnoDB";
        $table->charset = 'utf8mb4';
        $table->collation = 'utf8mb4_unicode_ci';

    });

User.php:

public function userCommentsCount()
    {
        return $this->hasMany('App\Comment', 'commentable_id', 'id')->where('enable', '=', '1')->where('to_stats', '=', '0');
    }

I get my user list from this code:

    $users = User::ofRoleType($role)->withCount('commentsReceived')->paginate(50);


@foreach ($users as $user)
User id: $user->id <br/>
@endif

I need to display a list of users with the number of votes (total summary comments.rating per user) they have received.

In result I need:

@foreach ($users as $user)
    User id: $user->id has total votes: ...... <br/>
@endif

I want sort my result DESC (from max to min rating votes).

How can I make it?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Pkrq4U
via IFTTT

Aucun commentaire:

Enregistrer un commentaire