一个视图中有两个不同的控制器

Two different controllers in one view

我有一个视图,它有一个文本编辑器,其内容值用于 DocumentController,文本区域用于 CommentController。我想 post 在同一视图中发表评论。以下是我的看法。

read.blade.php

<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>

<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" 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>

这是我的 DocumentController 函数,用于访问 read.blade.php.

的内容
class DocumentController extends Controller
{
//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);
}
}

CommentController 在这里,当我试图死掉并转储请求时,它只获取我的令牌请求,但在我的文本区域中它没有获取值。我怎么解决这个问题?有什么建议吗?

class CommentController extends Controller
{
    public function postComments(Request $request)
    {
        dd($request);
        $this->validate($request,
        [
            'comment' => 'required',
        ]);

        $commentObject = new Comment();

        $commentObject->comment = $request->comment;
        $commentObject->save();
    }
}

获取DocumentController

内容的路由
Route::get('/document/{id}',
[
    'uses' => '\App\Http\Controllers\DocumentController@readDocuments',
    'as' => 'document.read',
    'middleware' => 'auth',
]);

使用 CommentController

post 评论内容的路由
//COMMENT

Route::post('/comments',
[
    'uses' => '\App\Http\Controllers\CommentController@postComments',
    'as' => 'comments',
]);

您的 textarea 上缺少 name 属性。

改变这个:

<textarea class = "form-control" rows = "4" id = "comment" placeholder = "Leave a feedback"></textarea>

为此:

<textarea class="form-control" rows="4" name="comment" id="comment" placeholder="Leave feedback"></textarea>