问题描述
您好,我正在建立一个人力资源管理系统,我只想在数据表中仅显示非管理员用户,但出现错误,我不知道自己可能做错了什么。这是我的代码;
<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();
});
解决方法
您可以尝试@continue
跳过循环
<table class="table table-hover">
<thead>
<th>Username</th>
<th>Department</th>
<th>Salary</th>
</thead>
<tbody>
@foreach($this->$users()->get() as $user)
@php
if($user->is_admin == 1){
@continue
}
@endphp
<tr>
<td>{{$user->username}} </td>
<td>{{$user->department}} </td>
<td>{{$user->salary}} </td>
</tr>
@endforeach
</tbody>
,
您可以通过只选择不是管理员用户的用户来扩展查询,而不是遍历users表并检查每个用户是否是admin用户。
where('is_admin','!=' 1)
这样,您已经拥有了要显示的所有用户,并且无需在视图中添加额外的逻辑。
,我忘了在控制器中写代码
public function addEmployee()
{
$users = User::all();
return view('admin/addEmployee')->with('users',$users);
}