【问题标题】:How to get the current id of model from another controller?如何从另一个控制器获取模型的当前 ID?
【发布时间】:2016-12-02 10:13:18
【问题描述】:

我正在尝试使用不同的控制器获取我的模型的 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

评论控制器:

 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)

【问题讨论】:

  • 你可以从你的观点传过来,有没有观点分享给我们
  • @Iftikharuddin 当然。我更新了我的帖子。我的观点也有不同的控制器。请看。
  • 根据您的说法,您没有将 id 传递给“cmets”路线。这样做,你不会有任何问题。即Route::post('/comments/{id}'
  • @ChibuezeOpata 是的,我已经这样做了。我怎样才能在我的action 中通过它应该是这样的吗? action = "{{ route ('comments/{id}') }}"。我试过这个,但它说是Route [comments/{id}] not defined

标签: php laravel laravel-5.2 laravel-routing


【解决方案1】:

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 的文档发送评论。

【讨论】:

  • 我尝试echo $documentObject 但我无法获得价值。请查看我更新的帖子。
  • 使用Document::find($id)-&gt;first() 并使用dd 函数而不是echo 来查看文档详细信息
  • 它仍然给我同样的错误。当我试图在我的参数中过去 $id 时。我只是想获得Document 模型的id,以便将其同步到数据透视表中。
  • 您确定具有该 ID 的文档存在..!?使用此行测试文档模型Documnet::all()
  • 是的,因为这是我正在使用的当前视图。我试图在我的Document Controller 中检查这一点,这很有效。但是当我从另一个控制器尝试这个时它不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多