Dominic
Dominic

Reputation: 510

Axios request returns error 500 (laravel, axios & vuejs)

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?

Upvotes: 1

Views: 492

Answers (1)

Dominic
Dominic

Reputation: 510

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

  1. didn't have the necessary "created_at" and "updated_at" columns.
  2. has lots of other columns beside "Frage" without a default value, that needed input as well.

My solution:

  1. add the missing two columns

  2. send the other column values in the this.Frage also.

Upvotes: 1

Related Questions