0
votes
public function index(Request $request)
{
    $data = Admin::when($request->keyword, function ($query) use ($request) {
        $query->where('nama_lengkap', 'like', "%{$request->keyword}%");
    })->paginate(5);

    $data->appends($request->only('keyword'));

    return view('admin.crud_admin.index', compact('data'));
}

Model Admin.php

protected $fillable = [
    'avatar', 'nama_lengkap', 'email', 'password',
];

public function getAvatar()
{
    if (!$this->avatar) {
        return asset('images/default.png');
    }

    return asset('images/' . $this->avatar);
}

index.blade.php

<a class="nav-link dropdown-toggle dropdown-user-link" id="dropdown-user" href="javascript:void(0);"
   data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <div class="user-nav d-sm-flex d-none">
        <span class="user-name font-weight-bolder">{{ Auth::user()->nama_lengkap }}</span>
        <span class="user-status">Admin</span>
    </div>
    <span class="avatar"
        ><img class="round" src="{{ $data->getAvatar('avatar') }}"
          alt="avatar" height="40" width="40"
        /><span class="avatar-status-online"></span>
    </span>
</a>

this does not appear even there is an error:

"Method Illuminate\Database\Eloquent\Collection::getAvatar does not exist. (View: C:\xampp\htdocs\Helpdesk\resources\views\admin\crud_admin\index.blade.php)"

Can you help me? please this is important to me

1

1 Answers

0
votes

because $data is a Collection, since you assign to it this:

$data = ...->paginate(5);

Thus, you have to loop over it:

@foreach($data as $user)
   ...
   {{ $user->getAvatar('avatar') }}
   ...
@endforeach