Dora
Dora

Reputation: 6970

POST 500 (Internal Server Error) - Ajax & laravel 4

Sorry if my explanation is bad. Anyways, I have this todo list made by using laravel 4 sometime last year. Been working great for the last few months and I stopped playing around with it. Somehow I was playing around with it just yesterday and now it's giving me this POST 500 (internal Server ERROR). The Ajax call was working perfectly before. I had this error with chrome last night and today I tried using firefox, somehow it worked but then few minutes after, I am getting the same error in firefox too. By the way, a few ajax call is used here. Adding an item uses ajax to post what's added with a done button (this ajax is working perfectly). When the done button is clicked it should become a delete button and this is the ajax that's giving me the error. I went to the app.php to turn the debug => true This error message is shown

{"error":{"type":"ErrorException","message":"Creating default object from empty value","file":"\/home2 \/.........\/TodoController.php","line":174}}

In my line 174 I have $query->done = 1;

that line is inside of this function

public function doubleDQuery($table, $id, $todoText, $done, $action){
    $query = $table::where('id', '=', $id)->where('todoText', '=', $todoText)
        ->where('done', '=', $done)->first();
    if($action == 'done'){
        $query->done = 1;
        $query->touch();
        $query->save();
    }

    if($action == 'delete'){
        $query->delete();
    }

}

if I commented out the $query->done = 1, it gives me an error to the next line

{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function touch() on a non-object","file":"\/home2\......\/TodoController .php","line":175}}

if I commented the above line up, it gives me the error of none-object to save().

No modification is done for the past months and it was working for like a minute in firefox as mentioned above. I just have no idea where I should start looking for an answer, can someone give me a hand where I should start looking?

I have read that since 500 is internal server error it might be the server instead of my codes but it's weird that the other ajax call in the same application works just this is giving me the headaches.

Upvotes: 0

Views: 183

Answers (1)

Marcel Gwerder
Marcel Gwerder

Reputation: 8520

That just means that $query is not what you think it is.

It seems to be null which means that there is no entry in the database matching your where clause. Although you expect it, respectively require it to be an object and also an instance of your model class for this code to work, without throwing an exception.

So you can either check for null manually and do whatever you wan't like:

if($query === null) {
    //No matching row found
}

Or use firstOrFail() instead of first() which will actually already throw an exception with a 404 status code which may be what you want because you probably want to handle it properly on the client side.

Upvotes: 1

Related Questions