如何定义用户(作者)-OctoberCMS
How to define a user (author) - OctoberCMS
我需要帮助。我只需要能够在 OctoberCMS 中查看一个 post 的用户。
但所有用户都会出现在我面前。
这是有效的代码,但我不知道如何定义它以按我需要的方式工作。
public function getUserOptions()
{
$result = [0 => 'indikator.news::lang.form.select_user'];
$users = Db::table('backend_users')->orderBy('login', 'asc')->get()->all();
foreach ($users as $user) {
$name = trim($user->first_name.' '.$user->last_name);
$name = ($name != '') ? ' ('.$name.')' : '';
$result[$user->id] = $user->login.$name;
}
return $result;
}
使用此作为创建项目的用户是当前登录的用户,因此我们可以使用 current logged-in user
那个。
use BackendAuth;
public function getUserOptions()
{
// placeholder
$result = [0 => 'indikator.news::lang.form.select_user'];
// only back-end user which is currently logged in
// it will be available always as we are in back-end :)
$user = BackendAuth::getUser();
$result[$user->id] = $user->login; // or $user->email;
return $result;
}
It will get logged in user and show in drop-down for selection [ or even you skip place-holder to have only 1 selected option]
如有疑问请评论。
我需要帮助。我只需要能够在 OctoberCMS 中查看一个 post 的用户。
但所有用户都会出现在我面前。
这是有效的代码,但我不知道如何定义它以按我需要的方式工作。
public function getUserOptions()
{
$result = [0 => 'indikator.news::lang.form.select_user'];
$users = Db::table('backend_users')->orderBy('login', 'asc')->get()->all();
foreach ($users as $user) {
$name = trim($user->first_name.' '.$user->last_name);
$name = ($name != '') ? ' ('.$name.')' : '';
$result[$user->id] = $user->login.$name;
}
return $result;
}
使用此作为创建项目的用户是当前登录的用户,因此我们可以使用 current logged-in user
那个。
use BackendAuth;
public function getUserOptions()
{
// placeholder
$result = [0 => 'indikator.news::lang.form.select_user'];
// only back-end user which is currently logged in
// it will be available always as we are in back-end :)
$user = BackendAuth::getUser();
$result[$user->id] = $user->login; // or $user->email;
return $result;
}
It will get logged in user and show in drop-down for selection [ or even you skip place-holder to have only 1 selected option]
如有疑问请评论。