please format your code with three backticks ``` on a line before and after the code block
Email validation on user email update.
Validation not working when i update the user email it accept the dublicate email that already exist in the table.
public function UpdateAuthUserEmail(Request $request) {
$user = Auth::user();
$this->validate($request, [
'email' => 'required|string|email|max:255|unique:users,email,'.$user->id,
]);
$user = User::find($user->id);
$user->email = $request->email;
$user->save();
Session::flash('success', 'User Email Updated');
return redirect()->back();
}
Please advice.
@Snapey thanks for figure out about format the code. I also mentioned you in my 2nd question for help I do not figure out about the user and usermeta relationship please also help me there.
Regards Thanks in advance.
Is it you updating the user email or is it the user updating their own email?
The code you show will only work for the user updating their own email address
yes in my account user updating there email but this could for now because same i was checking the same user email was validate but.
Now the issue is i cannot figure out how to update the auth user billing information.
$user = Auth::user();
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'address1' => 'required|string',
'address2' => 'required|string',
'zip' => 'required|regex:/\b\d{5}\b/',
'state' => 'required|string',
'city' => 'required|string',
]);
$usermeta = User::find($user->id);
$usermeta->fname = $request->input('fname');
$usermeta->lname = $request->input('lname');
$usermeta->address1 = $request->input('address1');
$usermeta->address2 = $request->input('address2');
$usermeta->zip = $request->input('zip');
$usermeta->state = $request->input('state');
$usermeta->city = $request->input('city');
Auth::user()->UserMeta()->save($usermeta);
It ask me Column not found:
$usermeta = User::find($user->id);
You are finding the User model not the Usermeta model
$usermeta = $user->usermeta;
and when saving, just save usermeta
$usermeta->save();
but i need to update now insert a new record is it work in that case ?
Yes, get current user meta for user and change the fields and save. It does not need to be re-associated with the User
See my update to previous answer
i update as per your code but it says
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fname' in 'field list' (SQL: updateuserssetupdated_at= 2018-09-29 08:32:16,fname= Tehseen Billing,lname= mushtaq,address1= tehseen mushtaq address1,address2= tehseen mushtaq address2,zip= 12344,state= california,city= rawalpindi whereid= 1)
Why it shows the users table ? i need to update the user meta of login user only ?
because you get User model. as I said.
$usermeta = User::find($user->id);
$usermeta->fname = $request->input('fname');
$usermeta->lname = $request->input('lname');
$usermeta->address1 = $request->input('address1');
$usermeta->address2 = $request->input('address2');
$usermeta->zip = $request->input('zip');
$usermeta->state = $request->input('state');
$usermeta->city = $request->input('city');
$usermeta = $user->usermeta;
$usermeta->save();
is that your mean @Snapey
but id declare the use App\UserMeta; above my controller
$usermeta = User::find($user->id);
gets user
change to
$usermeta = $user->usermeta;
REMOVE the line you have added before save
Still got error with this code column not found :'(
$user = Auth::user();
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'address1' => 'required|string',
'address2' => 'required|string',
'zip' => 'required|regex:/\b\d{5}\b/',
'state' => 'required|string',
'city' => 'required|string',
]);
$usermeta = User::find($user->id);
$usermeta->fname = $request->input('fname');
$usermeta->lname = $request->input('lname');
$usermeta->address1 = $request->input('address1');
$usermeta->address2 = $request->input('address2');
$usermeta->zip = $request->input('zip');
$usermeta->state = $request->input('state');
$usermeta->city = $request->input('city');
$usermeta->save();
This is the whole method to allow login user to updater their billing information.
public function UpdateAuthUserBilling(Request $request) {
$user = Auth::user();
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'address1' => 'required|string',
'address2' => 'required|string',
'zip' => 'required|regex:/\b\d{5}\b/',
'state' => 'required|string',
'city' => 'required|string',
]);
$usermeta = User::find($user->id);
$usermeta->fname = $request->input('fname');
$usermeta->lname = $request->input('lname');
$usermeta->address1 = $request->input('address1');
$usermeta->address2 = $request->input('address2');
$usermeta->zip = $request->input('zip');
$usermeta->state = $request->input('state');
$usermeta->city = $request->input('city');
$usermeta->save();
}
$usermeta = $user->usermeta;
$usermeta->fname = $request->input('fname');
$usermeta->lname = $request->input('lname');
$usermeta->address1 = $request->input('address1');
$usermeta->address2 = $request->input('address2');
$usermeta->zip = $request->input('zip');
$usermeta->state = $request->input('state');
$usermeta->city = $request->input('city');
$usermeta->save();
Method Illuminate\Database\Eloquent\Collection::save does not exist.
is that something with relationship issue ? with user and usermeta table ?
What is the name of your relationship?
in my UserMeta model i have
public function user() {
return $this->belongsTo('App\User');
}
}
and in my User model
public function UserMeta() {
return $this->hasMany('App\UserMeta');
}
and the user_id is the FKey in my user_metas table
Its good practice to stick with conventions. Relationships are normally lowercase
Change your code;
$usermeta = $user->UserMeta;
$usermeta->fname = $request->input('fname');
$usermeta->lname = $request->input('lname');
$usermeta->address1 = $request->input('address1');
$usermeta->address2 = $request->input('address2');
$usermeta->zip = $request->input('zip');
$usermeta->state = $request->input('state');
$usermeta->city = $request->input('city');
$usermeta->save();
still get this error
Method Illuminate\Database\Eloquent\Collection::save does not exist.
with below code
public function UpdateAuthUserBilling(Request $request) {
$user = Auth::user();
$this->validate($request, [
'fname' => 'required',
'lname' => 'required',
'address1' => 'required|string',
'address2' => 'required|string',
'zip' => 'required|regex:/\b\d{5}\b/',
'state' => 'required|string',
'city' => 'required|string',
]);
$usermeta = $user->UserMeta;
$usermeta->fname = $request->input('fname');
$usermeta->lname = $request->input('lname');
$usermeta->address1 = $request->input('address1');
$usermeta->address2 = $request->input('address2');
$usermeta->zip = $request->input('zip');
$usermeta->state = $request->input('state');
$usermeta->city = $request->input('city');
$usermeta->save();
}
OK, I looked at your relationship. Why can a user haveMany user meta?
This should be a hasOne ?
Yes the user have only one entry you are right so if i change this to hasOne and run the code is that working ?
Yes its working now and one important thing i remember in my mind i have the usermeta table name like user_metas and model name is UserMeta is this convention is right according to laravel or not?
Conventions
model: UserMeta or Usermeta
table: user_metas or usermetas
relationship to user: usermeta
So in my case i am right ?
Yes or no ?
just your relationship name should be usermeta not UserMeta
public function usermeta() {
return $this->hasMany('App\UserMeta');
}
Got it @Snapey Love you bro.
Please or to participate in this conversation.