首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用associate()方法不起作用(BadMethodCallException)

使用associate()方法不起作用(BadMethodCallException)
EN

Stack Overflow用户
提问于 2016-07-28 15:33:31
回答 1查看 121关注 0票数 1

我正在尝试使用associate注释模型从视图中获取文档模型当前的id。但按下按钮后。上面总是写着。

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

CommentController

代码语言:javascript
复制
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

代码语言:javascript
复制
//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);
}

路由

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

这就是我获得当前视图id的地方。

代码语言:javascript
复制
//COMMENT
Route::post('/document/{document}/comments',
[
'uses' => '\App\Http\Controllers\CommentController@postComments',
'as' => 'comments',
]);

视图

代码语言:javascript
复制
<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。有什么帮助或建议吗?谢谢!

更新

型号:

注释

代码语言:javascript
复制
class Comment extends Model
{
    protected $tables = 'comments';

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

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

文档

代码语言:javascript
复制
class Document extends Model
{

    protected $table = 'documents';

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

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-28 15:37:53

associate()方法被认为适用于关系。

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

代码语言:javascript
复制
$commentObject->documents()->associate($document);

此外,我认为你的方法应该如下。

代码语言:javascript
复制
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属性并将其设置为$document->id,即调用此查询的$document对象的id。然后保存$comment对象。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38640834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档