正在对 Laravel 5.8 中的用户列表进行排序
Sorting the list of users in Laravel 5.8
我是 Laravel 的初学者。我在我的项目中使用 Laravel 5.8。
我有这个代码:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->boolean('enable')->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->bigInteger('counter')->default(0);
$table->string('url_address', 160);
$table->string('account_paid_for', 12)->nullable();
$table->string('premium_for', 12)->nullable();
$table->string('hits', 12)->nullable();
$table->rememberToken();
$table->timestamps();
$table->engine = "InnoDB";
});
public function getUserList(string $query, string $sortColumn, string $sortMethod)
{
if ($query != "") {
return User::ofRoleType(['user', 'userPremium', 'userCompany', 'userSponsor', 'userGuest'])
->where(function ($q) use ($query, $sortColumn, $sortMethod) {
$q->where('email', 'LIKE', '%' . $query . '%')
->orWhere('id', 'LIKE', '%' . $query . '%')
->orWhere('name', 'LIKE', '%' . $query . '%')
->orWhere('surname', 'LIKE', '%' . $query . '%')
->orderBy($sortColumn, $sortMethod);
})->paginate(2);
} else {
return User::ofRoleType(['user', 'userPremium', 'userCompany', 'userSponsor', 'userGuest'])->paginate(2);
}
}
和用户模型:
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use cms\Presenters\UserPresenter;
public static $roles = [];
protected $fillable = ['name', 'surname', 'email', 'email_verified_at', 'password', ];
protected $hidden = [
'password', 'remember_token',
];
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;
}
}
我需要按以下顺序显示用户列表:
首先,拥有高级帐户的用户(即 premium_for 字段的日期比今天大),
那么该领域人数最多的用户"hits"
其他用户
你怎么做到的?有谁知道如何为这种类型自定义我的 getUserList 函数?
我不了解 Laravel 经验,但我认为您要求的是:
->orderBy(DB::raw('IF(premium_for > CURDATE(), 0, 1)'))
->orderBy('hits', 'DESC');
(未测试)这意味着声明:
- 未来保费到期的人优先。 (以后多少都无所谓。)
- 然后按命中排序 - 从大到小。
解释 DB::raw()
的资源:https://laraveldaily.com/select-with-dbraw-make-your-database-work/
我还建议您 WS_CONCAT()
您的 WHERE 列并且只进行一次 LIKE 比较。像这样:
我是 Laravel 的初学者。我在我的项目中使用 Laravel 5.8。
我有这个代码:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->boolean('enable')->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->bigInteger('counter')->default(0);
$table->string('url_address', 160);
$table->string('account_paid_for', 12)->nullable();
$table->string('premium_for', 12)->nullable();
$table->string('hits', 12)->nullable();
$table->rememberToken();
$table->timestamps();
$table->engine = "InnoDB";
});
public function getUserList(string $query, string $sortColumn, string $sortMethod)
{
if ($query != "") {
return User::ofRoleType(['user', 'userPremium', 'userCompany', 'userSponsor', 'userGuest'])
->where(function ($q) use ($query, $sortColumn, $sortMethod) {
$q->where('email', 'LIKE', '%' . $query . '%')
->orWhere('id', 'LIKE', '%' . $query . '%')
->orWhere('name', 'LIKE', '%' . $query . '%')
->orWhere('surname', 'LIKE', '%' . $query . '%')
->orderBy($sortColumn, $sortMethod);
})->paginate(2);
} else {
return User::ofRoleType(['user', 'userPremium', 'userCompany', 'userSponsor', 'userGuest'])->paginate(2);
}
}
和用户模型:
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use cms\Presenters\UserPresenter;
public static $roles = [];
protected $fillable = ['name', 'surname', 'email', 'email_verified_at', 'password', ];
protected $hidden = [
'password', 'remember_token',
];
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;
}
}
我需要按以下顺序显示用户列表:
首先,拥有高级帐户的用户(即 premium_for 字段的日期比今天大),
那么该领域人数最多的用户"hits"
其他用户
你怎么做到的?有谁知道如何为这种类型自定义我的 getUserList 函数?
我不了解 Laravel 经验,但我认为您要求的是:
->orderBy(DB::raw('IF(premium_for > CURDATE(), 0, 1)'))
->orderBy('hits', 'DESC');
(未测试)这意味着声明:
- 未来保费到期的人优先。 (以后多少都无所谓。)
- 然后按命中排序 - 从大到小。
解释 DB::raw()
的资源:https://laraveldaily.com/select-with-dbraw-make-your-database-work/
我还建议您 WS_CONCAT()
您的 WHERE 列并且只进行一次 LIKE 比较。像这样: