When I save() the “AppPost” returns the 500 error
Well, i got some very strange error, this very simple code returns the 500 error.
<?php namespace AppHttpControllers; use AppUser; use AppComment; use IlluminateHttpRequest; use IlluminateHttpResponse; use IlluminateSupportFacadesDB; use IlluminateSupportFacadesAuth; use IlluminateSupportFacadesStorage; use AppHttpControllersController; class CommentController extends Controller { public function NewComment(Request $request) { $cText = $request->commentText; $comment = new Comment; $comment->author_id = Auth::user()->id; $comment->current_text = $cText; $comment->save(); return $comment; } [...] }
When I comment the $comment->save(); Returns:
author_id: 1 current_text: "fsdfdfd"
OBS: “fsdfdfd” is the text I typed in the <‘textarea’> in the page.
As you can see returns the object normally, but when I try to save returns the error:
500 (Internal Server Error)
Migration file of the Comments:
Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->integer('author_id')->unsigned(); $table->foreign('author_id') ->references('id') ->on('users') ->onDelete('cascade'); $table->text('current_text')->nullable(false); $table->enum('edited', ['yes','no'])->default('no'); $table->string('history', 8000); $table->timestamps(); });
Comment.php
<?php namespace App; use IlluminateDatabaseEloquentModel; class Comment extends Model { protected $fillable = [ 'author_id', 'current_text', 'edited', 'history']; protected $guarded = [ 'updated_at', 'created_at']; function author(){ return $this->belongsTo('AppUser'); } }
Answer
I found what was wrong!!
The error was before any of those scripts, was in the AJAX function. The object that was sending the data, was something like this:
var objectComment = { 'commentText': textComment };
Because of it, I was getting a json like this in my php file:
{'commentText : Lorem ipsun' : null}
And was trying to get some value through this… #facepalm
Other thing that was wrong, is that i forget to configure the CSRF Token, it was disturbing the data sending and helping to drive me crazy a little more.
Hope I could help some of you with this error caused by a little daydream 🙂