I am using Laravel with Vue making http request with the axios. It is giving 500 error. My request code is
axios.post('user', {
first_name: this.user.first_name,
last_name: this.user.last_name,
email: this.user.email,
phone_number: this.user.phone_number,
is_active: this.user.is_active,
entity: this.user.entity,
role_id: this.user.role_id
})
Lavael Route is
Route::resource('user', 'UserController');
And Controller is
public function store(Request $request)
{
$this->validate($request, [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required',
'is_active' => 'required'
]);
$user = User::create([
'first_name' => request('first_name'),
'last_name' => request('last_name'),
'email' => request('email'),
'is_active' => request('is_active'),
'phone_number' => request('phone_number'),
'role_id' => request('role_id')
]);
return response()->json([
'user' => $user,
'message' => 'Success'
], 200);
}
I am using Laravel with Vue making http request with the axios. It is giving 500 error. My request code is
axios.post('user', {
first_name: this.user.first_name,
last_name: this.user.last_name,
email: this.user.email,
phone_number: this.user.phone_number,
is_active: this.user.is_active,
entity: this.user.entity,
role_id: this.user.role_id
})
Lavael Route is
Route::resource('user', 'UserController');
And Controller is
public function store(Request $request)
{
$this->validate($request, [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required',
'is_active' => 'required'
]);
$user = User::create([
'first_name' => request('first_name'),
'last_name' => request('last_name'),
'email' => request('email'),
'is_active' => request('is_active'),
'phone_number' => request('phone_number'),
'role_id' => request('role_id')
]);
return response()->json([
'user' => $user,
'message' => 'Success'
], 200);
}
Share
Improve this question
asked Feb 23, 2018 at 8:24
Hasan RazaHasan Raza
651 gold badge3 silver badges7 bronze badges
3
-
Can you try
/user
? If this does not work please recheck whether the full path is/user
? – Sergii Vorobei Commented Feb 23, 2018 at 8:26 - Could you please provide the error. – amit rawat Commented Feb 23, 2018 at 8:26
- Provide the error... – Borjante Commented Feb 23, 2018 at 8:29
3 Answers
Reset to default 3Your store method is correct. You just need to specify the correct path to your axios post request, which means your ponent method should be like this :
axios.post('/user/store', {
first_name: this.user.first_name,
last_name: this.user.last_name,
email: this.user.email,
phone_number: this.user.phone_number,
is_active: this.user.is_active,
entity: this.user.entity,
role_id: this.user.role_id
}).then((res) => {
// here clear your form and fetch your users list if you want
})
.catch(error => {
// here catch error messages from laravel validator and show them
});
and your store method should return validator messages error :
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'first_name' => 'required|max:255',
'last_name' => 'required|max:255',
'email' => 'required',
'is_active' => 'required'
]);
if ($validator->fails()) {
return response()->json(['errors'=>$validator->errors()],422);
}
$user = User::create([
'first_name' => $request('first_name'),
'last_name' => $request('last_name'),
'email' => $request('email'),
'is_active' => $request('is_active'),
'phone_number' => $request('phone_number'),
'role_id' => $request('role_id')
]);
return response()->json([
'user' => $user,
'message' => 'Success'
], 200);
}
axios.post('/user/store', {
first_name: this.user.first_name,
last_name: this.user.last_name,
email: this.user.email,
phone_number: this.user.phone_number,
is_active: this.user.is_active,
entity: this.user.entity,
role_id: this.user.role_id
})
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error.response)
});
Try this code instead & always console.log your error with error.response to get all info & objects with full error details. Check in chrome console that all your POST datas from form are passed correctly to your database. In my case i figured out that my phone field where not passed due to typo mistake. My console error Image file
or you can try 2nd method below for laravel users :-
poser require barryvdh/laravel-cors
- Add the Cors\ServiceProvider to your config/app.php provider’s array
Barryvdh\Cors\ServiceProvider::class,
- To allow CORS for all your routes, add the HandleCors middleware in the $middleware property of app/Http/Kernel.php
protected $middleware = [ \Barryvdh\Cors\HandleCors::class, ];
- php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"
php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"
Hope this helps you :)
Try to change the following code block:
$user = User::create([
'first_name' => request('first_name'),
'last_name' => request('last_name'),
'email' => request('email'),
'is_active' => request('is_active'),
'phone_number' => request('phone_number'),
'role_id' => request('role_id')
]);
Replace with below:
$user = User::create([
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name'),
'email' => $request->input('email'),
'is_active' => $request->input('is_active'),
'phone_number' => $request->input('phone_number'),
'role_id' => $request->input('role_id')
]);