No, Answer here?
How to fix No query results for model [App\User] settingedit in Laravel 5.6?
working with Laravel 5.6 and mysql. in My application i am going to show for edit current loging user name and email. this is User Model
public function scopeUserr($query)
{
return $query->where('name',Auth::user()->name);
}
and UserController
public function settingedit()
{
$users = User::userr()->get();
return view('users.settingedit')->withUsers($users);
}
and My user data edit link is as following,
<a href="/users/settingedit" class="btn btn-primary">Update User</a>
and route is like this,
Route::get('users/settingedit', [
'uses' => 'UserController@settingedit',
'as'=> 'users.settingedit',
]);
but when I click Update User link following error is occured,
2/2) NotFoundHttpException
No query results for model [App\User] settingedit
how can fix this problem?
return $query->where('name',Auth::user()->name);
Your scope makes no sense. You already have the current logged in users instance (Auth::user()), so why do you need that to query the database just to get it again?
This also doesn't make sense.
$users = User::userr()->get();
return view('users.settingedit')->withUsers($users);
You're only retrieving a single user (the current user), so why call it $users (plural)? That kind of thing is confusing.
With authentication, you always have access to the current user. It queries it and retrieves it for you automatically on every request, so it's available via auth()->user(), or Auth::user(), etc. There is NO need to requery to retrieve what you already have loaded.
Given that, 1) you don't need to query the user and 2) you don't need to return the user to the view. The current user is already available in the view, so just access it directly in the view with one of those methods.
<input type="text" name="name" value="{{ old('name', auth()->user()->name) }}">
That will populate it with their current user name (upon page load), or the submitted the form and there were validation errors it will populate it with the submitted name.
Please or to participate in this conversation.