I am using Laravel API along with VueJS Frontend application. I send axios
POST
call along with some information which the user puts about himself. In the backend, I take that data and update the user record in the MySQL database.
This is my backend code:
$user = User::where(['logintoken' => $request->login_token, 'remember_token' => $request->remember_token])->first();
if (!$user) {
$status = 404;
$response = ['status' => false, 'message' => 'User not found.'];
} else {
$affectedRows = $user->update([
'country' => $request->input('country'),
'city' => $request->input('city'),
'state' => $request->input('state'),
'dob' => $request->input('dob'),
'language' => $request->input('language'),
'street' => $request->input('street'),
'house' => $request->input('house'),
'avatar' => $request->input('avatar'),
'birthplace' => $request->input('birthplace'),
'type' => $request->input('userType'),
'bio' => $request->input('bio'),
'zip' => $request->input('zip'),
'has-completed-profile' => 1
]);
if (!$affectedRows) {
$response = ['status' => false, 'message' => 'Error updating'];
$status = 500;
} else {
$response = ['status' => true, 'message' => 'User updated successfully', 'updated_record' => $user, 'request' => $affectedRows, 'zip' => $request->input('zip')];
}
}
Now, this code is working perfectly fine, except, it is not updating the zip
field. I console.log()
the response in VueJS side, and I see that the zip
value is correct and also the data type fo zip
field in the DB is string.
Still, this is not working for the zip
column. When I had created the users
migration, there was no zip
column, but after some time, when I needed it, I created another migration and used it to alter the table to insert the zip
field.
Any idea on why it isn't working?
You need to add that field in Respective Model class.
This should solve your problem.
$fillable
serves as a "white list" of attributes that should be mass assignable. So, to get started, you should define which model attributes you want to make mass assignable. You may do this using the $fillable
property on the model. For example, let's make the zip
attribute of our User
model mass assignable:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['zip', // your rest column names];
}
This example should help you sort your problem.
Here is the documentation.
You probably missed zip
in you model's $fillable
.
So add it, it should look like this:
protected $fillable = [
'country', 'city', 'state', 'dob', 'language', 'street', 'house', 'avatar', 'birthplace', 'type', 'bio', 'zip', 'has-completed-profile'
];
Find documentation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With