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!');
Related
I am currently working on POS in laravel 8. I am having an error when I login in that "credentials do not match our records" When I login in through the default Register page than it works fine but when I register through internal panel than the data goes to the database but I face error when I log in.
Code for registration through panel:
public function store(Request $request)
{
$users = new User;
$users->name = $request->name;
$users->email = $request->email;
$users->password = Hash::make($request->name);
// $users->password = bcrypt($request->name);
$users->is_admin = $request->is_admin;
$users->save();
if($users){
return redirect()->back()->with('User Created Successfully');
}
return redirect()->back()->with('User Failed Created');
}
Default register code:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Your panel registration form is hashing the name input and not the password input.
$users->password = Hash::make($request->name);
Should probably be
$users->password = Hash::make($request->password);
#Code for registration through panel code should be like this#
public function store(Request $request){
$users = new User;
$users->name = $request->name;
$users->email = $request->email;
$users->password = Hash::make($request->password);
$users->is_admin = $request->is_admin;
$users->save();
if($users){
return redirect()->back()->with('User Created Successfully');
}
return redirect()->back()->with('User Failed Created');
}
hello i have two tables users and profiles and i have a form to update the user profile, whene i try to update, it's just refresh the page but nothing was updated, and there is no errors
this is my code :
ProfilesControllers:
public function update(Request $request)
{
$this->validate($request,[
'name'=>'required',
'email'=>'required|email',
'about'=>'required',
'facebook'=>'required|url',
'instagram'=>'required|url',
]);
$user = Auth::user();
if($request->hasFile('avatar')){
$avatar = $request->avatar;
$avatar_new_name = time() . $avatar->getClientOriginalName();
$avatar->move('uploads/avatars', $avatar_new_name);
$user->profile->avatar = 'uploads/avatars'.$avatar_new_name;
$user->profile->save();
}
$user->name = $request->name;
$user->email = $request->email;
$user->profile->facebook = $request->facebook;
$user->profile->instagram = $request->instagram;
$user->profile->about = $request->about;
if($request->has('password')){
$user->password=bcrypt($request->password);
$user->save();
}
$user->save();
$user->profile->save($user->profile);
return redirect()->back();
}
profile.blade.php :
<form action="{{ route('user.profile.update')}}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
/// here is my input fields
</form>
and i'm using Laravel : 6.20.5
could someone tell me where is the problem in my code? thanks
you could try this:
instead of $avatar = $request->avatar;
use
$avatar = $request->file('avatar');
and instead of $avatar->move('uploads/avatars', $avatar_new_name);
use
$request->file('avatar')->storeAs('uploads/avatars', $avatar_new_name);
and when using back() there's no need for redirect(). back() generates an HTTP redirect.
so
return back();
I have an error in my update user admin form where I want to update the profile that's corresponding to the user if he has a profile. But it throws this error:
Creating default object from empty value
I have set relationships in my model between the User and profile Profile::where('id', $user->profile_id)->first(); but when I dd it's always a null, even if there is a corresponding profile.
User.php
public function profile()
{
return $this->hasOne('App\Profile', 'user_id', 'id');
}
Profile.php
public function user()
{
return $this->belongsTo('App\User');
}
function error is in
public function update(Request $request, User $user)
{
if(\Auth::check()) {
if(\Auth::user()->type == 'admin') {
$validated = $request->validate([
'name' => 'required',
'email' => 'required|email',
'password' => 'confirmed'
]);
if(!empty($validated['password'])){
if(!$user->profile){
//Has no profile
$user->name = $validated['name'];
$user->email = $validated['email'];
$user->password = bcrypt($validated['password']);
$user->update();
} else {
//Has profile
$profile = Profile::where('id', $user->profile_id)->first();
$profile->username = $validated['name'];
$profile->email = $validated['email'];
$profile->update();
$user->name = $validated['name'];
$user->email = $validated['email'];
$user->password = bcrypt($validated['password']);
$user->update();
}
} else {
if(!$user->profile){
//Has no profile
$user->name = $validated['name'];
$user->email = $validated['email'];
$user->update();
} else {
//Has profile
$profile = Profile::where('id', $user->profile_id)->first();
$profile->username = $validated['name'];
$profile->email = $validated['email'];
$profile->update();
$user->name = $validated['name'];
$user->email = $validated['email'];
$user->update();
}
}
}
}
}
on these lines $profile = Profile::where('id', $user->profile_id)->first();
Since you are using user_id as a foreign key in the profiles table, you would expect this as your statement:
Profile::where('user_id', $user->id)->first();
Or, by using the relation:
$user->profile;
You can just use your relation and also compress the logic as :
$user->name = $validated['name'];
$user->email = $validated['email'];
if(!empty($validated['password'])){
$user->password = bcrypt($validated['password']);
$profile = $user->profile;
$profile->username = $validated['name'];
$profile->email = $validated['email'];
$profile->save();
}
$user->save();
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');
}
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