J. Doe
J. Doe

Reputation: 19

LARAVEL: How to display all the same user type as the one currently logged in on a <table>?

Im new to laravel and im currently trying to get all the same user types as the one currently logged in to display on a table. For example, if a teacher is logged in, then all the other teacher type users must be listed on the table, only the teachers, no students no etc. I am currently using the make auth thingy.

table code

<table class="table table-striped table-bordered">
    <thead class="bg-danger">
      <tr>
        <th scope="col">First</th>
        <th scope="col">Email</th>
        <th scope="col">Type</th>
        <th scope="col">Action</th>
      </tr>
    </thead>
    <tbody>
      @foreach($users as $user)
        <tr>
          <td>{{ $user->name }}</td>
          <td>{{ $user->email }}</td>
          <td>{{ $user->user_type }}</td>
          <td style="width: 13%">
          <button type="button" class="btn btn-primary">{{ __('Edit') }}</button>
          <button type="button" class="btn btn-primary">{{ __('Delete') }}</button>
          </td>
        </tr>
      @endforeach
    </tbody>
  </table>
  </div>

Controller code


    public function index()
    {
        $users = User::all();

        return view('users.index')->with([
            'contentheader' => 'Users',
            'users' => $users,
        ]);
    }

migration code

 public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('username', 20)->unique();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->enum('user_type',['teacher','student']);
            $table->rememberToken();
            $table->timestamps();
        });
    }

Upvotes: 0

Views: 584

Answers (4)

PriyankMotivaras
PriyankMotivaras

Reputation: 740

Add middleware Auth in your route first, as

Route::get('your route and related method')->middleware('auth');

And run query in your controller Method as,

use Auth;//don't forgot to add this
 public function index()
    {
       if(Auth::check())
       {
        $users = User::where('user_type',Auth::user()->user_type)->where('id','!=',Auth::user()->id)->get();
        return view('users.index')->with([         
            'users' => $users,
        ]);
      }

    }

Upvotes: 2

Chirag Unadkat
Chirag Unadkat

Reputation: 21

In your Controller change $users = User::all(); with following,

$users = User::where('user_type', Auth::user()->user_type)->get();

Upvotes: 1

Rehan
Rehan

Reputation: 4013

You need to get only the particular user type instead of all user like this

 $users = User::where('user_type','=', auth()->user()->user_type)->get();

This auth()->user()->user_type will give you the type of user which is currently logged in, using that you can filter out users.

Upvotes: 1

zahid hasan emon
zahid hasan emon

Reputation: 6233

As you have Auth you can check the logged in user type from it.

public function index()
    {
        $users = User::where('user_type',Auth::user()->user_type)->get();

        return view('users.index')->with([
            'contentheader' => 'Users',
            'users' => $users,
        ]);
    }

This will fetch the same type of user as of currently logged in user. For teacher it will fetch all teachers and for student it will fetch all students.

Upvotes: 1

Related Questions