samedi 26 octobre 2019

Sorting after user comments rating in Laravel

I am beginner in Laravel. I make project in Laravel 5.8. I have 2 tables: users and comments. Each comment has a rating. One user can have many comments.

I have this code:

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $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);
            $table->text('content');
            $table->dateTime('date_time');
            $table->ipAddress('ip');
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';

        });


class Comment extends Model
{

    use scopeActiveTrait;

    protected $quarded = ['id'];
    protected $fillable = ['content', 'enable', 'rating', 'user_id', 'commentable_type', 'commentable_id', 'date_time', 'ip'];
    public $timestamps = false;

    public function commentable()
    {
        return $this->morphTo(); // typ komentowanego obiektu
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;
    use psCMS\Presenters\UserPresenter;
    use scopeActiveTrait;

    public static $roles = [];
    public $dates = ['last_activity'];

    protected $fillable = [.......];



    protected $hidden = [
        'password', 'remember_token',
    ];


    // Show paid and active users
    public function scopeUserAvailable($query)
    {
        return $query->active()
            ->where('email_verified_at', '<>', null)
    }

    public function roles()
    {
        return $this->belongsToMany('App\Role');
    }

    public function mainRole()
    {
        return $this->hasOne('App\Role');
    }

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

    public function commentsReceived()
    {
        return $this->hasMany('App\Comment', 'commentable_id', 'id');
    }

    public function hasRole(array $roles)
    {

        foreach ($roles as $role) {

            if (isset(self::$roles[$role])) {
                if (self::$roles[$role]) return true;
            } else {
                self::$roles[$role] = $this->roles()->where('name', $role)->exists();
                if (self::$roles[$role]) return true;
            }
        }
        return false;
    }

    public function scopeOfRoleType($query, $types)
    {
        return $query->whereHas('roles', function ($q) use ($types) {
            $q->whereIn('name', $types);
        });
    }
    public function userRatingCount()
    {
        return $this->hasMany('App\Comment', 'commentable_id', 'id')->where('enable', '=', '1')->where('to_stats', '=', '0');
    }

}


$users= User::ofRoleType($role)

And now I want show my user list with summary rating (comment.rating).

I have code:

@foreach($users as $user)
     - User:  has 
@endfor

I try this code, but this is not working:

   $users= User::ofRoleType($role)->with('userRatingCount')->sum('comments.rating')

How can I repair this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire