0

Hi I'm building a human resources management system, I want to display only users that are not admin in a data table but I'm getting error dont know what I might be doing wrong. Here is my code;

 <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>

Table

 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();
        });
kris7i
  • 71
  • 2
  • 11
  • Can you please share your controller too? Usually you do not want to have any querying being done in your view. – Andy Abi Haidar Sep 08 '20 at 11:44
  • "_but I'm getting error_" What would that error be? fyi, it is way more performant to just get the "non-admin" users from your table (`->where('is_admin', '!=', 1)`) than to loop over _all_ users. Also, `$this->$users()` should probably be `$this->users()` – brombeer Sep 08 '20 at 11:46
  • this code should be in controller `users()->get()..`, and add condition on `is_admin` – Devsi Odedra Sep 08 '20 at 11:46
  • Remove the foreach loop you have added in your view, all this code should be preferably added to controller only and passed as a variable to view. – Ankit Jindal Sep 08 '20 at 12:06

3 Answers3

0

you can try @continue to skip loop

<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>
Kamlesh Paul
  • 11,778
  • 2
  • 20
  • 33
0

Instead of looping through the users table and checking every user to see if they are an admin user, you can extend your query by only selecting users that aren't admin users in the first place.

where('is_admin', '!=' 1)

This way you'll already have all the users that you want to show and you won't have to add extra logic to the view.

user3478148
  • 433
  • 1
  • 8
  • 26
0

I had forgotten to write code in the controller

 public function addEmployee()
        {
            $users = User::all();
            return view('admin/addEmployee')->with('users',$users);
        }
kris7i
  • 71
  • 2
  • 11