This is my first post in SO. I am beginner in Laravel. I use in my project Laravel 5.8. I have this code:
User.php
// Show paid and active users
public function scopeUserAvailable($query)
{
return $query->active()
->where('account_paid_for', '>=', Carbon::now()->format('Y-m-d'))
->where('category', '<>', 0)
->where('email_verified_at', '<>', null)
->where('hide_profil', '=', 0);
}
public function roles()
{
return $this->belongsToMany('App\Role');
}
public function mainRole()
{
return $this->hasOne('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 scopeOfRoleType($query, $types)
{
return $query->whereHas('roles', function ($q) use ($types) {
$q->whereIn('name', $types);
});
}
I have function to show my user list:
public function getFrontUserList(string $query, string $sortColumn, string $sortMethod, int $userType, $request)
{
$role = [];
if ($userType == 1) {
$role = ['UserNormal'];
} elseif ($userType == 2) {
$role = ['UserNormal2'];
} elseif ($userType == 3) {
$role = ['UserNormal3'];
} elseif ($userType == 4) {
$role = ['UserNormal4'];
} else {
$role = ['UserNormal', 'UserNormal2', 'UserNormal4'];
}
return User::ofRoleType($role)
->where(function ($q) use ($query, $sortColumn, $sortMethod) {
$q->userAvailable()->orderBy($sortColumn, $sortMethod);
})->orderBy($sortColumn, $sortMethod)->paginate(15);
}
And my role:
Role.php
class Role extends Model
{
protected $quarded = ['id'];
public $timestamps = false;
public function users()
{
return $this->belongsToMany('App\User');
}
}
RoleUser.php
class RoleUser extends Model
{
protected $quarded = [];
protected $fillable = ['user_id', 'role_id'];
protected $table = 'role_user';
public $timestamps = false;
}
This code work fine.
I display a list of users who have a paid account, active, verified email address.
I would like UserNormal2 users to display even if they don't have a paid account. How can I change the scopeUserAvailable function to do this?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire