I get an error
Call to undefined method Illuminate\Auth\GenericUser::update()
here is code
$user = Auth::user();
$user->name = 'name';
$user->update();
return redirect()->back();
You need update user from User model and then update Auth onject
$user = User::find(Auth::user()->id);
$user->name = 'name';
$user->save();
Update Auth:
Auth::setUser($user);
You should check if user is authenticated, then you can update name:
if (auth()->check()) {
auth()->user()->update(['name' => 'name']);
} else {
dd('User is not authenticated');
}
Related
I need to return message but I get an error.
My controller:
$user = User::findOrFail($id) ;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->role_id = $request->input('role_id');
if($user->update()){
return new UserResource($user);
}
I tried:
return new UserResource($user)->with('status', 'Profile updated!');
You have to place the creation of the UserResource instance between brackets so php knows you are using the with function on this new instance.
return (new UserResource($user))->with('status', 'Profile updated!');
I have made a middleware for admins
And I applied it in my routes.
So the problem when the user is admin he can go to users page and change his permissions and the page keeps redirecting and breaks when he clicks change permission to his profile :
ERR_TOO_MANY_REDIRECTS
my method to change user to admin :
public function admin($id){
$user = User::findOrFail($id);
$user->admin = 1;
$user->save();
session()->flash('success','Changed to admin');
return redirect()->back();
}
And to change user to Author :
public function notAdmin($id){
$user = User::findOrFail($id);
$user->admin = 0;
$user->save();
session()->flash('success','Changed to Normal');
return redirect()->back();
}
So how I can prevent the logged in user from changing his permissions?
I'm really confused about this.
Thank you
Redirect the user if he tries to change his own permissions by using
Auth->id() and comparing with $id so that if these things match then he is trying to change his own permission.
public function admin($id){
if(Auth->id() == $id) {
session()->flash('error','Permission denied');
return redirect()->back();
}
$user = User::findOrFail($id);
$user->admin = 1;
$user->save();
session()->flash('success','Changed to admin');
return redirect()->back();
}
i'm doing a project where ALL user can create a post and pubblic it!
if user not logged blade enable a section with email and password to create account and post.
I would like do:
Creation user
Log user in session auth ( HERE IS MY PROBLEM, HOW CAN I DO THIS? )
Create Post (last point because i need user_id)
CONTROLLER
// if user NOT registered my form will enable email and password field --
if(!null($request->input('email') && request->input('password'))) {
// create user
$user = new User();
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = bcrypt($request->input('name'));
$user->save();
// login user after create user
HOW CAN I LOG USER AFTER CREATED ACCOUNT ???
Thank you for your help!
// Get ID of user created
$get_user_id = User::where('email', '=', $request('email'))->first();
// create post
$post = new Post();
$post->title = $request->input('title');
$post->slug = str_slug($request->input('title'),'-');
$post->country = $request->input('country');
$post->zone = $request->input('zone');
$post->phone = $request->input('phone');
$post->address = $request->input('address');
$post->user_id = $get_user_id->id;
$post->save();
return redirect('/')->with('message-success', 'Post creato con successo!');
// IF USER LOGGED
}else{
$post = new Post();
$post->title = $request->input('title');
$post->slug = str_slug($request->input('title'),'-');
$post->country = $request->input('country');
$post->zone = $request->input('zone');
$post->phone = $request->input('phone');
$post->address = $request->input('address');
$post->user_id = $request->input('user_id');
$post->save();
return redirect('/')->with('message-success', 'Post creato con successo!');
}
The answer is:
Auth::login($user);
Check out the doc.
Is not so clear what you want to do here, the way you get the user id is insecure.
I'll recommend you to use $user = Auth::user(); instead to get the user id, ($user->id).
to check the login manually use
if (Auth::attempt(['email' => $email, 'password' => $password])) {}
so in your example I'll do something like:
if (!Auth::attempt(['email' => $email, 'password' => $password])) {
// create User
} else {
// $userId = Auth::user()->id;
}
// finally create post
Add a __construct() method to your class, and add following code:
public function __construct() {
$this->middleware('auth', ['except' => ['index', 'show']]);
}
And it must actually work
I want to change database data before and after App function in filter.php file.
I try to do like in that tutorial:
http://alex.leonard.ie/2013/08/02/laravel-tracking-last-activity-date-of-authorised-user/
App::before(function($request)
{
$user = Auth::user();
$user->online = 1;
$user->save();
});
But i get error: ErrorException (E_UNKNOWN) Creating default object from empty value
Mybe someone can help?
App::before(function($request)
{
if (Auth::check()){
$user = Auth::user();
$user->online = 1;
$user->save();
}
});
Note that this will only work if the user is logged in.
Try this.
App::before(function($request){
if(Auth::check()){
$user = Auth::user();
$user->online = 1;
$user->save();
}
});
i want to add record to a new user right after regeneration complete the problem that i get the user->id with null value after calling user->save() .
Here is my code
public function register(){
$input=Input::all();
$rules=array(
'username'=>'required|unique:bradoseusers|alphaNum|min:2',
'email'=> 'required|unique:bradoseusers|email',
'password'=>'required|alphaNum|min:6'
);
$v=Validator::make($input,$rules);
if($v->passes())
{
$password=$input['password'];
$password=Hash::make($password);
$user=new Bradoseusers();
$user->username=$input['username'];
$user->Email=$input['email'];
$user->password=$password;
$user->save();
return Redirect::to('login');
}
else
{
return Redirect::to('/Register')->withErrors($v);
}
}
You may try something like this:
public function register()
{
$input=Input::all();
$rules=array(...);
$v = Validator::make($input,$rules);
if($v->passes()) {
$user = new Bradoseusers;
$user->username = $input['username'];
$user->Email = $input['email'];
$user->password = Hash::make($input['password']);
$user->save();
// probably you want to redirect to
// default landing page for logged in user
return Redirect::to('login'); // i.e /dashboard or /
}
else {
return Redirect::to('/Register')->withErrors($v)->withInput();
}
}
the problem was that i have set in the model the primaryKey property to "Email" . So that would override the primary key value so you wouldn't have an "id" field.