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

Mikejs's avatar

Filters working, but getting error trying to display avatar,Attempted to lazy load [assignedUser]

So after several hours of trial and error my tasks filter by user or if admin shows all tasks.

This is my code I am trying to use to display the users avatar:

    @if ($todo->assignedUser)
                                                    @foreach ($todo->assignedUser as $assignedUser)
                                                        <span
                                                            class="min-w-[60px] text-s leading-4 px-4">{{ $assignedUser->name }}</span>
                                                        <div
                                                            class="h-6 w-6 rounded-full ring-1 ring-secondary-500 img-active transition-all duration-150">
                                                            @if ($assignedUser->media_file_name)
                                                                <img src="{{ asset('images/users/' . $assignedUser->media_file_name) }}"
                                                                    alt="{{ $assignedUser->name }}"
                                                                    class="w-full h-full object-cover rounded-full">
                                                            @else
                                                                <span>{{ $assignedUser->name }}</span>
                                                            @endif
                                                        </div>
                                                    @endforeach
                                                @endif

which is giving this erorAttempted to lazy load [assignedUser] on model [App\Models\Todo] but lazy loading is disabled.

In my controller I have

       public function index($all = false)
{
    $user = Auth::user();

    if ($all && ($user && $user->isAdmin())) {
        $todos = Todo::with('assignedUser')->orderBy('created_at', 'asc')->get();
    } else {
        $todos = $user ? $user->todos()->with('assignedUser')->latest()->get() : collect();
    }

    $users = $all ? User::all() : collect();

    $todos = $this->loadAvatars($todos);

    return view('apps.todo', compact('todos', 'users'));
}


/**
 * Helper function to retrieve avatars of assigned users.
 *
 * @param  \Illuminate\Support\Collection  $todos
 * @return \Illuminate\Support\Collection
 */
private function loadAvatars(Collection $todos): Collection
{
    $assignedUserIds = $todos->pluck('user_id')->toArray();
    $assignedUsers = User::whereIn('id', $assignedUserIds)->get();

    return $todos->map(function ($todo) use ($assignedUsers) {
        $assignedUser = $assignedUsers->where('id', $todo->user_id)->first();
        $todo->setRelation('assignedUser', $assignedUser); // Set the assignedUser relationship on the Todo model
        return $todo;
    });
}

in my todo model:

       public function assignedUser()
{
    return $this->belongsTo(User::class, 'user_id');
}

public function users()
{
    return $this->belongsToMany(User::class, 'todo_user', 'todo_id', 'user_id');
}

and in my user model

             public function registerMediaCollections(): void
{
    $this->addMediaCollection('avatar')
        ->singleFile();
}

public function getFileNameAttribute()
{
    $media = $this->getFirstMedia('avatar');
    if ($media) {
        return $media->file_name;
    }
    return null;
}
public function userMedia()
{
    return $this->hasOne(Media::class, 'model_id')->where('model_type', 'App\Models\User');
}

public function getMediaFileNameAttribute()
{
    return $this->userMedia ? $this->userMedia->file_name : null;
}
public function registerMediaConversions(Media $media = null): void
{
    $this
        ->addMediaConversion('preview')
        ->fit(Manipulations::FIT_CROP, 300, 300)
        ->nonQueued();
}

/**
 * Local scope to exclude auth user
 * @param $query
 * @return mixed
 */
public function scopeWithoutAuthUser($query): mixed
{
    return $query->where('id', '!=', auth()->id());
}

/**
 * Local scope to exclude super admin
 * @param $query
 * @return mixed
 */
public function scopeWithoutSuperAdmin($query): mixed
{
    return $query->where('id', '!=', 1);
}

public function isAdmin(): bool
{
    $roles = $this->getRoleNames(); // Assuming you are using Spatie's laravel-permission
    \Illuminate\Support\Facades\Log::info('User roles: ' . json_encode($roles));
    return $this->hasRole(['admin', 'super_admin']);
}
public function getAssignedUserAvatarAttribute()
{
    $assignedUser = $this->assignedUser; // Use the assignedUser relationship to get the user

    if ($assignedUser && $assignedUser->media_file_name) {
        return asset('images/users/' . $assignedUser->media_file_name);
    }

    return null;
}

public function isRegularUser(): bool
{
    return !$this->isAdmin();
}

and my routes

              // For admin and super admin users (show all tasks)
Route::get('/todo', 'TodoController@index')->name('todo.index')->middleware('todo.access');
// For regular users (show only their tasks)
Route::get('/todo/user', 'TodoController@index')->name('todo.index_user')->middleware('todo.access:user');


Route::post('/todo', [TodoController::class, 'store'])->name('todos.store');
Route::get('/todo/{id}', [TodoController::class, 'show'])->name('todos.show');
Route::put('/todo/{id}', 'TodoController@update')->name('todo.update');
Route::get('/todo/{id}/edit', [AppsController::class, 'edit'])->name('todos.edit');
//Route::get('/todo', [AppsController::class, 'index_all'])->name('todos.index');
Route::get('/todo', [TodoController::class, 'index_all'])->name('todo');
Route::put('/todo/{id}/update-status', [TodoController::class, 'updateStatus'])->name('todos.updateStatus');
Route::post('/todo/{id}/update-stared-status', 'TodoController@updateStaredStatus')->name('update-stared-status');
Route::post('/todo/{id}/delete', 'TodoController@delete')->name('todo.delete');

any idea how to prevent the lazy load, I am going around in circles! I have 3 tables users, todos and todos_user to store the pivot table users have many tasks, one user per task!

0 likes
4 replies
Mikejs's avatar

Updated my blade admin shows avatars just fine

                                                          @if ($todo->assignedUser)
<span class="min-w-[60px] text-s leading-4 px-4">{{ $todo->assignedUser->name }}</span>
<div class="h-6 w-6 rounded-full ring-1 ring-secondary-500 img-active transition-all duration-150">
    @if ($todo->assignedUser->getMediaFileNameAttribute())
        <img src="{{ asset('images/users/' . $todo->assignedUser->getMediaFileNameAttribute()) }}"
             alt="{{ $todo->assignedUser->name }}"
             class="w-full h-full object-cover rounded-full">
    @else
        <span>{{ $todo->assignedUser->name }}</span>
    @endif
</div>

@endif

and todoController

          public function index($all = false)
{
    $user = Auth::user();

    if ($all && ($user && $user->isAdmin())) {
        $todos = Todo::with('assignedUser')->orderBy('created_at', 'asc')->get();
    } else {
        $todos = $user ? $user->todos()->with('assignedUser')->latest()->get() : collect();
    }

    $users = $all ? User::all() : collect();

    $todos = $this->loadAvatars($todos);

    return view('apps.todo', compact('todos', 'users'));
}
Mikejs's avatar

i USED protected $with = ['assignedUser']; SEEMS TO HAVE FIXED THE PROBLEM!!

Snapey's avatar

Do you have Laravel debugbar installed so you can see instances of excessive database queries (such as when performing an eloquent query inside a loop)?

Beware when using with inside a model. You will run an extra query EVERYTIME you load the model, for whatever reason

Please or to participate in this conversation.