我在 laravel 7 上显示用户的个人资料照片时出错
I'm getting an error displaying the user's profile photo on laravel 7
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'));
}
型号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>
即使有错误也不会出现:
"Method Illuminate\Database\Eloquent\Collection::getAvatar does not exist. (View: C:\xampp\htdocs\Helpdesk\resources\views\admin\crud_admin\index.blade.php)"
你能帮帮我吗?请这对我很重要
因为 $data
是一个集合,因为你给它赋值:
$data = ...->paginate(5);
因此,你必须遍历它:
@foreach($data as $user)
...
{{ $user->getAvatar('avatar') }}
...
@endforeach
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'));
}
型号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>
即使有错误也不会出现:
"Method Illuminate\Database\Eloquent\Collection::getAvatar does not exist. (View: C:\xampp\htdocs\Helpdesk\resources\views\admin\crud_admin\index.blade.php)"
你能帮帮我吗?请这对我很重要
因为 $data
是一个集合,因为你给它赋值:
$data = ...->paginate(5);
因此,你必须遍历它:
@foreach($data as $user)
...
{{ $user->getAvatar('avatar') }}
...
@endforeach