EnzoTrompeneers
EnzoTrompeneers

Reputation: 1091

Eloquent $model->update() does not make changes on database record

When I update my comment it goes back to the page and changes the comment back to orignal, so the update hasn't been done. No errors or something.

db: comments

Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('articleID')->unsigned();
        $table->string('comment');
        $table->timestamps();
    });

model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    protected $fillable = ['comment', 'articleID'];

    public function article() {
        return $this->belongsTo('App\Article');
    }
}

CommentsController

public function edit($commentID) {
    $comment = Comment::findOrFail($commentID);
    return view('pages.comments.edit', compact('comment'));
}

public function update($commentID, CommentRequest $request) {
    $comment = Comment::findOrFail($commentID);
    $comment->update($request->all());
    return view('/');
}

editComment view

    <form action="{{ route('updateComments', ['commentID' => $comment->id]) }}"     class="form-horizontal" method="get">
        {{ csrf_field() }}
        {{ method_field('PATCH') }}
        <!-- Article data -->
        <div class="form-group">
            <label class="col-sm-3 control-label" for="body">Comment</label>
            <div class="col-sm-6">
                <textarea class="form-control" id="body" name="body" maxlength="1000">{{ $comment->comment }}</textarea>
            </div>
        </div>
        <!-- Add Article Button -->
        <div class="form-group">
            <div class="col-sm-offset-3 col-sm-6">
                <button class="btn btn-default" type="submit"><i class="fa fa-pencil-square-o"></i> Edit comment</button>
            </div>
        </div>
    </form>

Upvotes: 1

Views: 1369

Answers (1)

num8er
num8er

Reputation: 19372

Your problem is:

  1. You cannot do form submit with get method, even with hidden method_field('PATCH') in fact it does not event get to update action (:

  2. Your form field name body is not correct if we look at fillable field of model

So just fix Your form:

<form 
      action="{{ route('updateComments', ['commentID' => $comment->id]) }}"
      method="post"
      class="form-horizontal">

    {{ csrf_field() }}

    <!-- Article data -->
    <div class="form-group">
        <label class="col-sm-3 control-label" for="comment">Comment</label>
        <div class="col-sm-6">
            <textarea 
              id="comment" 
              name="comment" 
              maxlength="1000"
              class="form-control"
            >{{ $comment->comment }}</textarea>
        </div>
    </div>

    <!-- Add Article Button -->
    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-6">
            <button class="btn btn-default" type="submit">
              <i class="fa fa-pencil-square-o"></i> Edit comment
            </button>
        </div>
    </div>
</form>

or change Your schema and model to have field called body not comment

p.s. also fix Your update action:

public function update(CommentRequest $request, $commentID) {
    $comment = Comment::findOrFail($commentID);
    $comment->update($request->except(['articleID'])); // for safety we are ignoring "possible existence" of articleID in forma
    return redirect(route('updateComments', compact('commentID')));
}

doing return view('/') is not correct - it's trying to find file with name / that of course does not exist.

Upvotes: 1

Related Questions