jeudi 25 juillet 2019

Comments count and avg in Laravel

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

I have this code:

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

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

    protected $fillable = [ 'enable', 'name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address', 'isCompany', 'isMailing', 'content'];

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

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

    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 loginHistory()
    {
        return $this->hasMany('App\UserLoginHistory');
    }

    public function images()
    {
        return $this->hasManyThrough('App\UploadedFiles', 'App\User', 'id', 'photoable_id');
    }

}

class Comments extends Model
{
    protected $quarded = [];
    protected $fillable = ['content', 'enable', 'rating'];
    public $timestamps = false;

    public function commentable()
    {
        return $this->morphTo(); 
    }

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

And my migrations:

User:

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';
        });
    }

Comments:

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->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';

        });

I need to get information in the USER class: - the number of comments received by the selected user, - average grade from received comments

In the "comments" table: - user_id = id of the user who wrote the comment - commentable_id = id of the user who received the comment - rating - the rating given in a given comment (value from 1 to 5).

How can I get information on the number of comments received by a given user and average ratings (these comments)?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire