使用associate()方法将无法正常工作(BadMethodCallException)

时间:2016-07-28 15:33:31

标签: php laravel laravel-5.2

我正在使用评论模型从视图中associate我的文档模型当前ID。但按下按钮后。它总是说。

  

调用未定义的方法Illuminate \ Database \ Query \ Builder :: associate()

CommentController

class CommentController extends Controller
{

    public function postComments(Request $request, Document $document)
    {

        $this->validate($request,
        [
            'comment' => 'required',
        ]);

        $document = Document::find($document);

        $commentObject = new Comment();

        $commentObject->comment = $request->comment;
        $commentObject->associate($document);
        $commentObject->save();

        return redirect()->back();
    }
}

我正在尝试从视图中获取模型的当前id,以便我可以在$commentObject中将其关联起来。

DocumentController

//READ
public function readDocuments($id)
{
    //Find the document in the database and save as var.
    $document = Document::find($id);

    return view ('document.read')->with('document', $document);
}

路由

Route::get('/document/{id}',
[
    'uses' => '\App\Http\Controllers\DocumentController@readDocuments',
    'as' => 'document.read',
    'middleware' => 'auth',
]);

这是我获取视图的当前ID的地方。

//COMMENT
Route::post('/document/{document}/comments',
[
'uses' => '\App\Http\Controllers\CommentController@postComments',
'as' => 'comments',
]);

查看

<div class = "col-md-6">

    <div class = "form-group">

        <textarea id = "content">{{ $document->content }}</textarea>

    </div>

    <div class = "form-group">

        <button type = "submit" class = "btn btn-success">Approve</button>

    </div>
</div>

<!--COMMENT CONTROLLER-->
<div class = "col-md-6">                                        
    <form class = "form-vertical" method = "post" action = "{{ url('/document/'.$document->id.'/comments') }}">

        <div class = "form-group {{ $errors->has('comment') ? ' has-error' : '' }}">

            <label for = "comment">Comment:</label>
            <textarea class = "form-control" rows = "4" id = "comment" name = "comment" placeholder = "Leave a feedback"></textarea>

            @if ($errors->has('comment'))
                <span class = "help-block">{{ $errors->first('comment') }}</span>
            @endif

        </div>

        <div class = "form-group">

            <button type = "submit" class = "btn btn-primary">Comment</button>

        </div>

        <input type = "hidden" name = "_token" value = "{{ Session::token() }}">

    </form>
</div>

我认为我的 CommentController 出错了,因为当我尝试保存插入时。我认为它无法获得模型的当前ID。任何帮助或提示?谢谢!

更新

型号:

注释

class Comment extends Model
{
    protected $tables = 'comments';

    protected $fillable =
    [
        'comment',
        'document_id',
    ];

    public function documents()
    {
        return $this->belongsTo('App\Models\Document');
    }
}

文档

class Document extends Model
{

    protected $table = 'documents';

    protected $fillable = 
    [
        'title',
        'content',
        'category_id',
    ];

    public function comments()
    {
        return $this->hasMany('App\Models\Comment');
    }
}

1 个答案:

答案 0 :(得分:1)

associate()方法应该应用于关系。

我相信(考虑到你Comment模型有documents关系),你应该使用

$commentObject->documents()->associate($document);

此外,我相信你的方法应该如下..

public function postComments(Request $request, Document $document)
{

    $this->validate($request,
    [
        'comment' => 'required',
    ]);

    //No need to do the following - Laravel 5.2 will Model Bind it because of Document $document in parameters above
    //$document = Document::find($document);

    $commentObject = new Comment();

    $commentObject->comment = $request->comment;

    $document->comments()->save($commentObject);
    //..other tasks


    return redirect()->back();
}

$document->comments()是一个查询构建器,当您将->save($comment)传递给它时,它会更新$comment对象的document_id属性并将其设置为{{ 1}},即调用此查询的$document->id对象的id。然后保存$document对象。