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

时间:2016-07-28 03:43:04

标签: php laravel laravel-5.2 laravel-routing

我有一个视图,其中有一个文本编辑器,其内容值为 DocumentController CommentController 的文本区域。我想在同一视图中发表评论。以下是我的观点。

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 这里当我试图死亡并转储请求时,它只获取我的令牌的请求,但在我的textarea中它没有获得该值。我怎么解决这个问题?有什么提示吗?

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

发布评论内容的路线
//COMMENT

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

1 个答案:

答案 0 :(得分:2)

您错过了name上的textarea属性。

改变这个:

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