【发布时间】:2020-12-26 19:05:49
【问题描述】:
您好,我正在构建一个人力资源管理系统,我想在数据表中只显示不是管理员的用户,但我遇到了错误,不知道我可能做错了什么。这是我的代码;
<table class="table table-hover">
<thead>
<th>Username</th>
<th>Department</th>
<th>Salary</th>
</thead>
<tbody>
@foreach($this->$users()->get() as $user)
@if($user->is_admin !== 1)
<tr>
<td>{{$user->username}} </td>
<td>{{$user->department}} </td>
<td>{{$user->salary}} </td>
</tr>
@endif
@endforeach
</tbody>
表格
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->boolean('is_admin')->nullable();
$table->string('department')->nullable();
$table->integer('salary')->nullable();
$table->string('image',255)->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
【问题讨论】:
-
你能分享一下你的控制器吗?通常你不希望在你的视图中进行任何查询。
-
"但我遇到了错误" 那会是什么错误?仅供参考,从您的表 (
->where('is_admin', '!=', 1)) 中获取“非管理员”用户比遍历 所有 用户要高效得多。另外,$this->$users()应该是$this->users() -
此代码应在控制器
users()->get()..,并在is_admin上添加条件 -
删除你在视图中添加的foreach循环,所有这些代码最好只添加到控制器并作为变量传递给视图。
标签: php mysql laravel laravel-5 laravel-7