Álvaro
Álvaro

Reputation: 322

Eloquent update doesn't work in Laravel 6

I'm trying to update a field after submitting in the following form:

   <form action="{{ route("comments.update") }}" method="post">

                @csrf

                <input type="hidden" name="commentIDToEdit" id="commentID">
                <div class="md-form mb-5">
                    <i class="fas fa-comment"></i>
                    <label for="toEditComment"></label>
                    <textarea name="toEditCommentary" id="toEditComment" cols="3" rows="5" style="resize: none"
                              class="form-control"></textarea>
                </div>
                <div class="modal-footer d-flex justify-content-center">
                    <button type="submit" class="btn btn-default">Modificar</button>
                </div>
            </form>

I have the CommentsController, where I process the data from the form. Here is the code:

    public function updateComment()
{

    request()->validate([
        "toEditCommentary" => "min:10|max:500"
    ]);

    if (Session::has("username") && getWarningCount(User::whereUsername(session("username"))->value("email")) > 0) {

        Caveat::where("commentID", request("commentIDtoEdit"))
            ->update(["updatedComment" => request("toEditCommentary")]);

    } else {
        die("No se cumple la condición");
    }

    if (Comment::where("commentID", request("commentIDToEdit"))->exists()) {
        Comment::where("commentID", request("commentIDToEdit"))
            ->update(["commentary" => request("toEditCommentary")]);
    }

    return back();

}

Curiosly, the comment is updated in his table, but not the warning. I was thinking in the fillable property in the model, but I don't have it, instead this, I have the following code:

protected $guarded = [];
const UPDATED_AT = null;
const CREATED_AT = null;

Upvotes: 0

Views: 79

Answers (1)

Luca Puddu
Luca Puddu

Reputation: 501

Your hidden input is named commentIDToEdit, but in the Controller you fetch the Caveat using request("commentIDtoEdit") (different case).

What you wrote:

Caveat::where("commentID", request("commentIDtoEdit"))

What you should have done: (note the different casing)

Caveat::where("commentID", request("commentIDToEdit"))

This is because in the view, the input name is commentIDToEdit, not commentIDtoEdit.

Upvotes: 1

Related Questions