Eloquent $ model-> update()不会对数据库记录进行更改

时间:2017-01-26 21:36:42

标签: php laravel eloquent

当我更新我的评论时,它会返回页面并将评论更改回orignal,因此更新尚未完成。没有错误或其他什么。

db:comments

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

模型

<?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视图

    <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>

1 个答案:

答案 0 :(得分:1)

你的问题是:

  1. 您无法使用get方法进行表单提交,即使隐藏method_field('PATCH')实际上它也没有事件获得更新操作(:

  2. 如果我们查看模型的body字段,您的表单字段名称fillable就不正确

  3. 所以只需修复你的表格:

    <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>
    

    或将您的架构和模型更改为名为body而非comment

    的字段

    P.S。还修复了您的更新操作:

    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')));
    }
    

    执行return view('/')不正确 - 它正在尝试查找名称为/但当然不存在的文件。