我正在尝试id
使用其他控制器获取模型的模型,因此可以将sync
其添加到数据透视表中。我可以id
使用此模型。
class DocumentController extends Controller
{
//READ
public function readDocuments($id)
{
//Find the document in the database and save as var.
$document = Document::find($id);
$getId = $document->id;
echo $getId;
}
}
但是,当我尝试id
从另一个控制器获取时,它给我一个错误。
CommentController.php第18行中的ErrorException:App \ Http \ Controllers \ CommentController :: postComments()缺少参数2
这是我的控制器,用于获取当前模型。我可以var
$getId
从其他控制器访问吗?有任何提示如何获取id
模型的最新信息?
class CommentController extends Controller
{
public function postComments(Request $request, $id)
{
$this->validate($request,
[
'comment' => 'required',
]);
$commentObject = new Comment();
$documentObject = Document::find($id);//GET THE CURRENT ID OF THE (MODEL) DOCUMENT
$aa = $documentObject->id;
echo $aa;
$commentObject->comment = $request->comment;
$commentObject->save();
}
}
视图:
<!--DOCUMENT CONTROLLER-->
<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 = "{{ route ('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>
更新
我试图通过$id
我的职能。但是当我检查它时。它说。当我尝试$id
在参数中传递时,发生了此错误。
CommentController.php第19行中的ErrorException:App \ Http \ Controllers \ CommentController :: postComments()缺少参数2
CommentController:
public function postComments(Request $request, $id)
{
$this->validate($request,
[
'comment' => 'required',
]);
$commentObject = new Comment();
$documentObject = Document::find($id);//GET THE CURRENT ID OF THE (MODEL) DOCUMENT
echo $documentObject;
$commentObject->comment = $request->comment;
$commentObject->save();
}
我试过了die and dump
,$documentObject
但是什么也没打印。
路线:
//FOR DOCUMENT CONTROLLER
Route::get('/document/{id}',
[
'uses' => '\App\Http\Controllers\DocumentController@readDocuments',
'as' => 'document.read',
'middleware' => 'auth',
]);
//FOR COMMENT CONTROLLER
Route::post('/comments',
[
'uses' => '\App\Http\Controllers\CommentController@postComments',
'as' => 'comments',
]);
更新1:
//DocumentController
Route::get('/document/{id}',
[
'uses' => '\App\Http\Controllers\DocumentController@readDocuments',
'as' => 'document.read',
'middleware' => 'auth',
]);
//CommentController
Route::post('/document/{id}/comments',
[
'uses' => '\App\Http\Controllers\CommentController@postComments',
'as' => 'comments',
]);
视图:
<!--COMMENT CONTROLLER-->
<div class = "col-md-6">
<form class = "form-vertical" method = "post" action = "{{ route ('/comment/{id}') }}">
<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>
但是它无法识别我注册的路线。它总是说。
路由[/ comment / {id}]未定义。(视图:C:\ Users \ JohnFrancis \ LaravelFrancis \ resources \ views \ document \ read.blade.php)
laravel可以找到自动文档:这样定义路由:
Route::post('/document/{document}/comments',
[
'uses' => '\App\Http\Controllers\CommentController@postComments',
'as' => 'comments',
]);
并在控制器中:
public function postComments(Request $request, Document $document)
laravel自动获取文档。
将表单动作设置为:
http://www.ADDRESS.com/document/2/comments
向ID为2的文档发送评论
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句