php - Axios request returns error 500 (laravel, axios & vuejs)
Get the solution ↓↓↓My axios request (combined with Laravel) gives me a 500 error in the web console when I try to save a new question (= Frage):
"Error: Request failed with status code 500"
VueJS-method save():
save: function(){
axios.post('/api/save-frage', this.Frage) //passes the object this.Frage
.then(res => {
// nothing here
});
}
api.php:
Route::post('/save-frage','FragenController@store');
FragenController.php (Controller):
public function store(Request $request)
{
// validation coming soon :)
$frage = new Frage;
$frage->Frage = request('Fragentext');
$frage->save(); //if I comment this out, there's no error 500 :)
}
Frage.php (Model):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class Frage extends Model
{
protected $table = 'fragen';
protected $fillable = ['Frage']; // only field to fill is called "Frage"
}
I thought maybe the route was wrong (api.php), but if I change this, I then get a 404 error, so I guess this is correct, since otherwise there would have always been a 404 error. Then I checked the model if maybe the table or fields were protected but this looks good to me. What am I doing wrong here?
Answer
Solution:
Thanks guys, by looking in the XHR tab, as well as in laravel.log I saw the issue:
I reused an older table ("Frage") that
- didn't have the necessary "created_at" and "updated_at" columns.
- has lots of other columns beside "Frage" without a default value, that needed input as well.
My solution:
add the missing two columns
send the other column values in the this.Frage also.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: please make sure the php redis extension is installed and enabled.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.