Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mikejs's avatar

Controller not passing data to view, php it works ?

This template is driving me nuts.

I am trying to setup the todo so that when a new task is created is the user is Admin/Super Admin they can assing tasks to other users, if its a user they can create their own task and under assignee it will only display there name, for admin role they can choose the user:

In my blade file I have:

					 @if (auth()->user()->hasRole('admin') || auth()->user()->hasRole('super-admin'))
 <label for="assigned" class="form-label">Assignee</label>
 <select name="assigned" id="assigneds" class="form-control w-full mt-2 py-2">
    <option value="" disabled selected>Select Assignee</option>
    @foreach ($users as $user)
        <option value="{{ $user->id }}">{{ $user->name }}</option>
    @endforeach
  </select>
 @else
 <input type="hidden" name="assigned" value="{{ auth()->user()->id }}">
 <p>Assignee: {{ auth()->user()->name }}</p>
  @endif

I added this to my userModel:

       public function isAdmin(): bool
{
    return $this->hasRole(['admin', 'super_admin']);
}

Now if I add this at the top of the blade it works:

@php
$user = Auth::user(); // Get the authenticated user

if ($user->hasRole('super-admin')) {
    $users = App\Models\User::all(); // Retrieve all users for super-admin
} elseif ($user->hasRole('admin')) {
    $users = App\Models\User::whereDoesntHave('roles', function ($query) {
        $query->where('name', 'super-admin');
    })->get(); // Retrieve users for admin (excluding super-admin)
} else {
    $users = null; // Set users to null for regular user
}
@endphp

However if I use this in my controller:

   public function create()
{
    $user = Auth::user();

    if ($user->hasRole('super-admin')) {
        $users = User::all(); // Retrieve all users for super-admin
    } elseif ($user->hasRole('admin')) {
        $users = User::whereDoesntHave('roles', function ($query) {
            $query->where('name', 'super-admin');
        })->get(); // Retrieve users for admin (excluding super-admin)
    } else {
        $users = null; // Set users to null for regular user
    }

    return view('todo', compact('users'));
}

I get Undefined variable $users, chatGPT keeps going around

0 likes
3 replies
vincent15000's avatar

I think that there is a problem here.

$users = User::whereDoesntHave('roles', function ($query) {
    $query->where('name', 'super-admin');
})->get(); // Retrieve users for admin (excluding super-admin)

This query retrieves the users without any roles and who have the name super-admin.

You probably need this.

$users = User::whereDoesntHave('roles', function ($query) {
    $query->where('name', '!=', 'super-admin');
})->get(); // Retrieve users for admin (excluding super-admin)

But you probably don't need to exclude the super-admin because super-admin is a role, isn't it ?

So you can simplify the query like this.

$users = User::whereDoesntHave('roles')->get(); // Retrieve users for admin (excluding super-admin)
Snapey's avatar
Snapey
Best Answer
Level 122

My guess is that you are using the todo view from more than one controller method (eg index) and you don't create a $users variable in that view.

It works when you do the logic actually in the view.

Its normally a code smell to have the same view from two different controller methods.

1 like
Mikejs's avatar

Legend, thank you, the way the template is setup there is another AppsController which loaded the view

Thank you

Please or to participate in this conversation.