samedi 21 juillet 2018

Laravel, debug workflow of a variable

I am building a simple chat app that uses Vuejs. when I submit the message form I get a 500 internal server error. Looking at laravel.log, I see this:

local.ERROR: SQLSTATE[23000]: Integrity constraint 

violation: 19 NOT NULL constraint failed: 
messages.message (SQL: insert into "messages" 
       ("message", "user_id",        "updated_at",        "created_at") 
values (         ,         3, 2018-07-20 21:34:32, 2018-07-20 21:34:32)) 

As you see, the value of message field is missing.

The ajax method to post the form works, because using this code on app.js:

addMessage(message) {
  //add to exiting messages
  this.messages.push(message);
  //persist to database
    console.log(this.messages);
    axios.post('/messages').then(response => {

    });
}

console.log prints the message data. So I went to the next step.

This is the route code for the post. It is very hard to debug this, because nothing I put there to print the data somewhere works. I've tried error_log and Log::error(request()) without success, I don't know how to see the data structure here.

Route::post('/messages',function(){
    $user = Auth::user();
    //error_log(print_r(request(),TRUE));
    $message = $user->messages()->create([
        'message' => request()->get('message')
    ]);
    return ['status' => 'OK'];
});

This is the migration schema, for reference:

    Schema::create('messages', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->text('message');
        $table->integer('user_id')->unsigned();
    });

And the model:

protected $fillable = ['message'];
public function user(){
    return $this->belongsTo(User::class);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire