The following code,
$userData = auth()->guard('user')->user();
gives the user data. How can I set the value of a field from auth()->guard('user')?
Say something like this:- auth()->guard('user')->fname = 'Test'; when $userData['fname'] is empty.
Is it at all possible?
I would advise to change the array of user after extracting it to some variable and then use that variable further on like so:
$userData = auth()->guard('user')->user();
if (empty($userData['fname'])) {
$userData['fname'] = 'Test';
}
//use $userData from now on for listing user details
However, if you really need to change the authorized user object itself, you will need to use "accessor" in laravel. Here is a thread to help you get that in detail.
I hope it helps.
depending on the end-goal, you can get the user by doing: Auth::user(); Then you can update the name and save like so:
$user = Auth::user();
$user->email = 'bla#bla.bla';
$user->save();
Related
How I can check the selected elements that I pass from front end using Log::info? I tried using this but I dont know how to check the result, or maybe the code is wrong?
public function filterQuery(Request $request){
$name= $request->name;
$age= $request->age;
Log::info('Showing user profile for user: '.$name);
$query = user::query();
if(!empty($request->name)){
$query->where('name',$name);
}
I am developing an web backend for swiggy type of application. In that back-end i want to display all restaurants added by that particular partner user in header and that drop-down will display on all pages, so i want to do that get all restaurants and store on some place so that header can access and display it on every page. so how to do this? I am try to execute below code
if (Auth::guard('partner')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
// if successful, then redirect to their intended location
$user = $userId = Auth::id();
echo '<pre>';
print_r($user);
exit;
return redirect('partners/employees');
}
but it gives me error not able to get user id. i want to display user's restaurants list which is added by current user in system on every page header as dropdown list. so how can i achieve this.
you can access user id like this
$userId = Auth::guard('partner')->user()->id;
Auth::user()->id;
Also... this is weird. Typo?
$user = $userId = Auth::id();
$userId = Auth::user()->id;
$user = Auth::user();
Top of my controller i have added:
use Auth;
function in my controller
public function details($id)
{
if(Auth::check())
{
$user = Auth::user();
$product = Product::find($id);
$cart->product_id = $product->id;
$cart->category_id = $product->category_id;
$cart->user_id = $user->id;
dd($cart->user_id); //check if its storing the value
}
else {
return redirect()->route('login');
}
}
when I run this i get error:
Creating default object from empty value
If I remove the $user->id line the error goes.
I tried adding constructor also but still got same error
public function __construct() {
$this->middleware('auth');
}
dd is showing other details after it checks if user is logged in.
Is there a way to get user id of logged in user from users table that is created by default.
Thanks!
The issue is you’re calling $cart->product_id, but the $cart variable isn’t defined as far as I can see. PHP doesn’t know what to do, so because you try and assign a property to it, PHP assumes you want $cart to be a class, hence the “Creating default object from empty value” message.
Other than that, you code could be improved by using middleware to authenticate your users, and relations on your Eloquent models so you’re not manually assigning relation IDs.
Try like this,
You forgot to initials cart object
$cart = new Cart;
$cart->product_id = $product->id;
$cart->category_id = $product->category_id;
$cart->user_id = $user->id;
dd($cart->user_id); //check if its storing the value
I have validated and authenticated a user and upon authentication I redirect him to the 'dashboard view file'.
What I want know is how do I send that user's data along with the redirected command.
My function looks like this:
if($validator->passes()) {
/-- validate user --/
return Redirect::to('users/login')->with('message', 'Thanks for registering!');
I would appreciate it if someone helped me add the user's data.
Thanks
You can pass it:
$user = User::find(1);
return Redirect::to('users/login')
->withMessage('Thanks for registering!');
->withUser($user);
And get it in your controller
$user = Session::get('user');
Or you could just pass the id
return Redirect::to('users/login')
->with('message', 'Thanks for registering!');
->with('user_id', $user->id);
And search for it again
$user = User::find(Session::get('user_id'));
I have a User model with a password property.
If I retrieve it from the Database using:
$user = User::find(3);
And change the password value(in Controller):
$user->password = Input::get('passwordValue')
I want to trigger the Eloquent updating event that will first create a salt and concatenate the $salt with $passwordValue before assigning the value to $user->password something like:
$user->password = md5($salt . $passwordValue)
After this I can save the $user:
$user->save();
I want this event to fire every time the user changes his password in his profile page.
You can define a setter for the password field in your model:
public function setPasswordAttribute($value)
{
$salt = 'whatever';
$this->attributes['password'] = md5($salt.$value);
}
Then, in your controller (or whatever) code, just set the password using the plaintext version:
$user->password = Input::get('new_password');
However, it should be noted that, in Laravel, you really shouldn't be setting the password by using md5 with a salt, but instead use the hashing functions that Laravel provides:
$this->attributes['password'] = Hash::make($value);
That'll create a much stronger, bcrypt-generated password. You do, however, have to change the way you check the password, though it's pretty simple:
if (Hash::check($plaintext_password, $user->password)) {
// password is correct
}
However, further to this, you should use the Auth functions to handle logins so you don't even need to use Hash::check() directly:
if (Auth::attempt(array('username' => Input::get('username'), 'password' => Input::get('password'))) {
// logged in, redirect
} else {
// attempt failed, set message and redirect
}