0

I am very beginner in Laravel. I have Laravel 5.8 in my project.

I have this code:

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->char('enable', 1)->default(0);
            $table->string('name', 120)->nullable();
            $table->string('surname', 120)->nullable();
            $table->string('email', 120)->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            ........
            $table->rememberToken();
            $table->timestamps();
            $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); // 1-5
            $table->text('content');
            $table->dateTime('date_time');
            $table->ipAddress('ip');
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';

How can I show users list:

  1. sort after number of comments held

  2. sort after number of votes which user has (comments-> rating)?

2 Answers 2

1

Try this

//User model

public function comments()
{
    return $this->hasMany('App\Comment');
}

public function usersSortedByCommentCount()
{
    $users = User::with('comments')->get()->sortBy(function($user)
    {
        return $user->comments->count();
    });

    return $users;
}

public function usersSortedByRating()
{
    return User::whereHas('comments', function ($q) {
        $q->orderBy('rating', 'desc');
    })->get();
}

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

1 Comment

dd(User::whereHas('commentsReceived', function ($q) { $q->orderBy('rating', 'desc'); })->get()); - this is not working :(
0
$users_sorted_by_comments = User::with('comments')->get()->sortBy(function($user){
 return $user->comments->count();
})

The other condition is quite similar, you need to load ratings on comments and then just sort by that count like this:

$users_sorted_by_ratings_on_comments = User::with('comments')->get()->sortBy(function($user){
 return $user->comments->reduce(function($carry, $comment) {
  return $carry + $comment->rating;
}, 0 );
})

Laravel Collection reduce

5 Comments

Ok, this working. How can I sort after number of votes which user has (comments-> rating)??
I have error: Call to undefined relationship [votes] on model [App\Comment].
No I have Call to undefined relationship [ratings] on model [App\Comment].
No problem :) Now this code run, but it's not sorting correctly
What kind of output do you wish to obtain vs what does it give you?

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.