Francisunoxx Francisunoxx - 4 months ago 12
PHP Question

Two different controllers in one view

I have a view where it has a text-editor with a content value which for DocumentController and text-area for CommentController. I wanted to post a comment within the same view. Here's my view below.

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>


Here is my function for DocumentController to access the content of 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 Here when I tried to die and dump the request it only gets the request of my token but in my textarea it's not getting the value. How can I solve this problem? Any tips?

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();
}
}


Routes for getting the content of DocumentController

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


Routes for post the content of comment using CommentController

//COMMENT

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

Answer

You are missing the name attribute on your textarea.

Change this:

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

To this:

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