4 years ago

Model not being saved after AJAX call

Posted 4 years ago by Demers94

Hello everyone,

I'm building a small forum and now working on the post editing feature, for posts and topics. When the user clicks on the "Edit" button, the text of the post/topic becomes editable (contenteditable set to true) and when they click save, it's not editable anymore, and the new content is sent to the controller so that it can edit it in the database, after making the proper checks.

Here's how it's like in the Javascript, with jQuery :

$.post("/Forum/public/ajax/edit-topic", {topicID : contentID, content: content}, function(data){

Here is the route :

Route::post("/ajax/edit-topic", "TopicController@save");

And is the save() method of the controller :

public function save(Request $request)
    $topicID = $request->input("topicID");
    $content = $request->input("content");

    // Find the topic
    $topic = App\Topic::find($topicID);

    $topic->content = $content;         

I did a echo() of the $topic after it was saved, and logged that to the console in the JS, and it shows me the JSON object with the modifier content, with the new content appended at the end.

However, if I refresh the page, it reverts back to the previous version, because the changes are not saved in the database.

Am I missing something obvious here with AJAX/Eloquent? I'm sure that the save() method receives all it needs, and it's able to find the topic. It's just not saving it for some reason.

Thanks for the help. :)

Please sign in or create an account to participate in this conversation.