我正在尝试使用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');
}
}发布于 2016-07-28 15:37:53
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属性并将其设置为$document->id,即调用此查询的$document对象的id。然后保存$comment对象。
https://stackoverflow.com/questions/38640834
复制相似问题