3

I'm trying to get the id of my model using different controller so I can sync this to my pivot table. I can get the id of my Model using this.

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

But when I try to get the id from another controller it gives me a error.

ErrorException in CommentController.php line 18: Missing argument 2 for App\Http\Controllers\CommentController::postComments()

Here is my Controller for getting the current model. Can I access the var $getId from different controller? Any tips how can I get the current id of the model?

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

View:

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

UPDATE

I tried to passed $id in my function. But when I to check it. It says. This error happened when I tried to passed the $id in my parameter.

ErrorException in CommentController.php line 19: Missing argument 2 for App\Http\Controllers\CommentController::postComments()

CommentController:

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

I tried to die and dump the $documentObject but it doesn't print anything.

Routes:

//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',
]);

UPDATE 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',
]);

View:

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

But it can't recognize the route that I registered. It always says.

Route [/comment/{id}] not defined. (View: C:\Users\JohnFrancis\LaravelFrancis\resources\views\document\read.blade.php)

Francisunoxx
  • 1,440
  • 3
  • 22
  • 45
  • You can pass it from your view, is there any view share with us – Iftikhar uddin Jul 28 '16 at 07:06
  • @Iftikharuddin Sure. I updated my post. My view has also a different controller. Please see. – Francisunoxx Jul 28 '16 at 07:09
  • According to you, you're not passing the id to the 'comments' route. Do that and you won't have any problems. i.e. `Route::post('/comments/{id}'` – Chibueze Opata Jul 28 '16 at 08:25
  • @ChibuezeOpata Yes I already did that. How can I passed that in my `action` should it be like these? `action = "{{ route ('comments/{id}') }}"`. I tried this but it say's `Route [comments/{id}] not defined` – Francisunoxx Jul 28 '16 at 08:29

1 Answers1

1

laravel can find automatic document : define route like this :

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

and in controller :

public function postComments(Request $request, Document $document)

laravel get document automatic.

set form action like :

http://www.ADDRESS.com/document/2/comments

to send comment to document with id 2.

Saman
  • 2,506
  • 4
  • 22
  • 41
  • I tried to `echo` the `$documentObject` but I can't get the value. Please see my updated post. – Francisunoxx Jul 28 '16 at 08:05
  • use `Document::find($id)->first()` and use `dd` function instead `echo` to see Document detail – Saman Jul 28 '16 at 08:13
  • It still give me the same error. When I tried to past `$id` in my parameter. I'm just trying to get the `id` of `Document` model so I can sync it into pivot table. – Francisunoxx Jul 28 '16 at 08:18
  • Are u sure document with that id exist..!? use this line to test document model `Documnet::all()` – Saman Jul 28 '16 at 08:20
  • Yes because this is the current view that I'm using. I tried to check this in my `Document Controller` this works. But when I tried this from another controller it doesn't work – Francisunoxx Jul 28 '16 at 08:22
  • change route for send comment to this : `Route::post('/document/{id}/comments', [ 'uses' => '\App\Http\Controllers\CommentController@postComments', 'as' => 'comments', ]);` and in comment form set action and put document id in {id} part. – Saman Jul 28 '16 at 08:28
  • Already did what you said. Is this correct? I passed this in my `action`. `action = "{{ route ('comments/{id}') }}"` But it says `Route [comments/id] not defined.` – Francisunoxx Jul 28 '16 at 08:32
  • This is what I'm looking for. You get the output that I want. Why should I passed the `{document}` in `Route::post('/document/{document}/comments'`? and how my routes in form will be? – Francisunoxx Jul 28 '16 at 09:03
  • this is one to many relation (document has many comment and every comment belongs to a document) . routine way that show comment form in document page . and when load document you can access document id in blade view . u just need set action to form like `actione="{{ url('/document/'.$document->id.'/comments') }}"` in blade file – Saman Jul 28 '16 at 09:13
  • I'm already near to my output! I just needed to get the current document in my url. I tried this `$documentObject = Document::find($document);` and die and dump this to `dd ($documentObject);` But it prints me a null in the screen. Thanks!! – Francisunoxx Jul 28 '16 at 09:18
  • don,t need find document . if use postcomment like answer , in `dd($document)` must be see document detail . be careful set document id in right place and don,t change name of variable in route and function input. – Saman Jul 28 '16 at 09:35
  • Oh I see I just wanted to get the document `id` and insert this into my `comment_user` pivot table. Can you explained the form routes` that you changes. Why I need to put `dot` in `/document/'.$document->id.'/comments`? – Francisunoxx Jul 28 '16 at 09:54
  • for one to many relation you don,t need pivot table.just add row in comment table with document_id name . i try use form action in blade file and when try use address with variable need dot to connect string and variable . – Saman Jul 28 '16 at 09:59
  • Oh I made a many to many relation and I linked document_id in my pivot table and I thought it doesn't make sense much better if you linked this into comments table. Do I need to make a thread table for the comments thread? Thanks! – Francisunoxx Jul 28 '16 at 10:08
  • i think need more table for handle this . just add document_id row . i think this is better way. – Saman Jul 28 '16 at 10:21
  • i think need more table for handle this . just add document_id row . i think this is better way. – Saman Jul 28 '16 at 10:29