我正在尝试使用不同的 Controller 获取我的模型的 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;
}
}
但是当我尝试从另一个 Controller 获取 id
时,它给我一个错误。
ErrorException in CommentController.php line 18: Missing argument 2 for App\Http\Controllers\CommentController::postComments()
这是我的 Controller ,用于获取当前模型。我可以从不同的 Controller 访问 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
时发生此错误。
ErrorException in CommentController.php line 19: Missing argument 2 for App\Http\Controllers\CommentController::postComments()
评论 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
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>
但它无法识别我注册的路线。它总是说。
Route [/comment/{id}] not defined. (View: C:\Users\JohnFrancis\LaravelFrancis\resources\views\document\read.blade.php)
最佳答案
laravel 可以自动查找文档: 像这样定义路线:
Route::post('/document/{document}/comments',
[
'uses' => '\App\Http\Controllers\CommentController@postComments',
'as' => 'comments',
]);
在 Controller 中:
public function postComments(Request $request, Document $document)
laravel 自动获取文档。
设置表单 Action :
http://www.ADDRESS.com/document/2/comments
将评论发送到 id 为 2 的文档。
关于php - 如何从另一个 Controller 获取模型的当前 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38629253/