Laravel我只想在表格中显示员工

问题描述

您好,我正在建立一个人力资源管理系统,我只想在数据表中仅显示非管理员用户,但出现错误,我不知道自己可能做错了什么。这是我的代码;

 <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);
        }

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...