When the below is executed:
$user = User::where('email','=',$email)->take(1)->get();
Auth::login($user);
I get the error:
Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Auth\UserInterface, instance of Illuminate\Database\Eloquent\Collection given ...
I have tried changing this to:
$user = User::where('email','=',$email)->take(1)->get();
$userid = $user->id;
$user=User::find($userid);
Auth::login($user);
But then I get the error:
Undefined property: Illuminate\Database\Eloquent\Collection::$id
when you are using get() it returns collection of users means you may have multiple records though you have used take(1) but that number may be different so it always returns collections, so you can`t directy access property of object.
you can use first() to get single user object.
try this
$user = User::where('email','=',$email)->first();
Auth::login($user);
EDIT
you can use firstOrFail() instead of first() here, as if you don't get specified user firstOrFail() through an error.
Related
I want to add additional data to $user collection so can get the profile fields in the view
I have tried using $user['profileFields'] = $this->getProfileFields() and pass $user to the view using compatct or with and this is working fine.
DD
However, I have found some reference over the net saying I can extend using map but when I tried it is giving the following error
BadMethodCallException
Call to undefined method App\User::map()
So here is what I am trying to understand
Question:
Is the below code is wrong and won't work for what I am looking for and the first approach is the solution? Is there any
recommended method to add additional data to the $user collection?
public function show(User $user)
{
$user->map(function ($user){
$user['profileFields'] = $this->getProfileFields();
return $user;
});
return view('admin.user.show', compact('user'));
}
collection methods are works on the collection, here you're getting the object of user.
$user->profileFields = $user->getProfileFields();
return view('admin.user.show', compact('user'));
If the profileFields is in another table and having a foreign key with model ProfileField, then try to add a one to one relation to the User model.
Inside the User model, add a function
public function profileFields()
{
return $this->belongsTo('App\ProfileField', 'foreign_key','other_key');
}
This will give you the profileFields in every user when calling using eloquent.
I'm very confused by this!
In a laravel controller:
$user = Auth::user();
return $user;
returns the entire user object
$user = Auth::user()->id;
return $user;
returns the id of $user as expected
however if I put that exact thing into a query such as:
$user = Auth::user();
$query = Model::where('user_id', $user->id)->get();
return $query;
I get an error that I'm trying to get an 'id' property of a non-object!!!
How can this be possible?
I've also checked 100 times that I'm logged in when testing this.
Editing to add that I've also tried:
$query = Model::where('user_id', 1)->get();
and that works fine
edit to show the function:
$user = Auth::id();
$result = Lesson::where('user_id', $user)
->whereNotNull('notes')
->get();
return response()->json($result, 200);
Expected results: A list of objects with a filled in "notes" column.
Actual results: an empty [] and an error saying can't get id of a non-object
Don't use Auth in Models.
You can set a relation between User and your Model and do query like:
Auth::user()->relation()->get();
This error occurs if the Model doesn't contain any raw with that user id. Make sure you have the raw in the Model where you are querying to get data by user id.
You should try this
$user = Auth::user();
$result = Model::where('user_id', $user['id'])->get();
The error occurs because of you passed property of non-object.
Even after error not solve then restart your xampp or wamp.
If possible, upload the screenshot of error.
Your code should work if you are authed, it seems your are not based on the error message. Since you return json, i assume this is an api request. Api-routes uses another auth driver as default in Laravel (token vs. session). Either change the driver/guard or move your route(s) from routes/api.php to routes/web.php.
See docs for auth
You can change the driver in config/auth.php.
try this
$user_id = Auth::user()->id;
$query = Model::where('user_id', $user_id)->get();
return $query;
I do request to Model:
$user = User::findOrFail($id)->get();
And afetr try to except some fields from $user collection:
$user = $user->except(['surname', 'is_buyer', 'password']);
But in result I get still full $user collection
The problem here is that you don't have here collection as you expect. You have here collection of models, so you will get all model fields. Also using:
$user = User::findOrFail($id)->get();
is really strange, as you will get always 1 record and put this into collection.
Depending on your needs you might want to choose only some fields from database like this:
$user = User::select('id', 'name')->findOrFail($id);
instead of using collections here.
I am having trouble creating user roles and permissions in my system.
I'm using Package acl / laravel, and documentation describes the stages. I wrote the following code UserController before the Get User Roles:
Documentation: https://github.com/kodeine/laravel-acl/wiki/Create-Roles
$users = User::all();
$users->getRoles();
return view('users.index', compact ('users'));
Error:
BadMethodCallException in Macroable.php line 81:
Method getRoles does not exist.
What is going wrong, and how can I fix it?
The problem is that getRoles method exists for User object and when you use User:all() in $users variable you will get all users and not the single one.
So you can do:
$users = User::all();
return view('users.index', compact ('users'));
and in your Blade template you can in foreach loop user getRoles() method for single user and display them
Laravel's Eloquent update() function returns a boolean, and not the updated entry. I'm using:
return User::find($id)->update( Input::all() )
And this returns only a boolean. Is there a way I can get the actual row, without running another query?
I think that's the behaviour that you want:
$user = User::find($id)->fill(Input::all());
return ($user->update())?$user:false;
I hope it works fine for you.
Another approach can is to use the laravel tap helper function.
Here is an example to use it for updating and getting the updated model:
$user = User::first();
$updated = tap($user)->update([
"username" => "newUserName123"
]);
tap($user) allows us to chain any method from that model. While at the end, it will return the $user model instead of what update method returned.