ID does not pass from view to controller

Published 5 days ago by WallyJ

Relationships are set in the model

Form in view:

{!! Form::open(['route' => ['contactnotes.store', $contact->id]]) !!} 
     {{ csrf_field() }}
          <div class="form-group">
               {{Form::textarea('contactnotetext', null, array('placeholder' => 'Your note here', 'class' => 'form-control', 'rows' => '3'))}}
          </div>
          <div class="form-group">
               {{Form::submit('Add Note', array('class' => 'btn btn-primary'))}}
          </div>
{{Form::close()}}

ContactnotesController:

public function store(Contact $contact, Request $request)
    {
        $contactnote = new Contactnote;
        $contactnote->contact_id = $contact->id;
        $contactnote->contactnotetext = $request->contactnotetext;
        $contactnote->contact()->associate($contact);
        return $contactnote;
    }

I don't understand why the id is not being passed. When I submit the form I get:

{
"contact_id": null,
"contactnotetext": "adsf",
"contact": []
}
Best Answer (As Selected By WallyJ)
Cronix

You're not showing your route definition. Is this a resource route? If so, the store named route doesn't take a parameter like you are sending here

{!! Form::open(['route' => ['contactnotes.store', $contact->id]]) !!}

because you're creating something new. So my guess is you're not actually retrieving a Contact model result in the controller and then you try to use that model here

$contactnote->contact_id = $contact->id;

if you do dd($contact); as first thing in that controller method, does it retrieve the actual model with filled in fields?

I think it should be like

// remove $contact->id
{!! Form::open(['route' => ['contactnotes.store']]) !!} 

// create a hidden form field for contact_id
<input type="hidden" name="contact_id" value="{{ $contact->id}}">

Then in the controller, use that to retrieve the model.

public function store(Request $request) // remove the Contact model from here
{
    // retrieve the model with findOrFail so it will 404 if the model isn't found
    $contact = Contact::findOrFail($request->contact_id);

    // rest the same
    $contactnote = new Contactnote;
    $contactnote->contact_id = $contact->id;
    $contactnote->contactnotetext = $request->contactnotetext;
    $contactnote->contact()->associate($contact);
    return $contactnote;
}

I'm assuming that your route is defined as a resource route due to your contactnotes.store route name, like

Route::resource('contactnotes', 'ContactNotesController');

https://laravel.com/docs/5.7/controllers#resource-controllers

You'll notice the store named route takes no parameters.

D9705996
D9705996
5 days ago (121,180 XP)

You don't look to have anything in your form with contact_id. You do have an id from $contact->id

jlrdw
jlrdw
5 days ago (251,930 XP)

You forgot a $request.

jekinney
jekinney
5 days ago (215,385 XP)

People still use form helpers?

Store method with route model binding?

Did you:

public function store(Contact $contact, Request $request)
    {
    dd( $contact);
    }

If null or empty then your route is written wrong.

// Note contact not id
Route::post('/store/{contact}, 'Some [email protected]');

Also check out the default update method, and notice anything different?

public function update(Request $request, Model $model)

The order may not have anything to do with it?

WallyJ
WallyJ
5 days ago (3,770 XP)

@D9705996 , The url the form is on includes something like /contact/5, so the ID is being sent to that page. The contact_id is in the controller, and pulls the $contact->id

@jlrdw , The other fields are passed since "contactnotetext" shows up in the results.

D9705996
D9705996
5 days ago (121,180 XP)

@WallyJ - @jekinney has pretty much summed up how to get this to work.

If not

what code is producing this

{
"contact_id": null,
"contactnotetext": "adsf",
"contact": []
}

Can you share the route definition your form hits and if possible an example of the data your are sending to this route from the network tab of chrome dev tools

WallyJ
WallyJ
5 days ago (3,770 XP)

@jekinney , thanks for the ideas. The form helpers are part of LaravelCollective who continued developing them when Laravel took them out of the framework.

www.laravelcollective.com

I checked the dd and it shows nothing, however, I'm wondering how my route can be wrong when the form sends the information directly to the controller via this line:

{!! Form::open(['route' => ['contactnotes.store', $contact->id]]) !!}
Cronix
Cronix
5 days ago (786,650 XP)

You're not showing your route definition. Is this a resource route? If so, the store named route doesn't take a parameter like you are sending here

{!! Form::open(['route' => ['contactnotes.store', $contact->id]]) !!}

because you're creating something new. So my guess is you're not actually retrieving a Contact model result in the controller and then you try to use that model here

$contactnote->contact_id = $contact->id;

if you do dd($contact); as first thing in that controller method, does it retrieve the actual model with filled in fields?

I think it should be like

// remove $contact->id
{!! Form::open(['route' => ['contactnotes.store']]) !!} 

// create a hidden form field for contact_id
<input type="hidden" name="contact_id" value="{{ $contact->id}}">

Then in the controller, use that to retrieve the model.

public function store(Request $request) // remove the Contact model from here
{
    // retrieve the model with findOrFail so it will 404 if the model isn't found
    $contact = Contact::findOrFail($request->contact_id);

    // rest the same
    $contactnote = new Contactnote;
    $contactnote->contact_id = $contact->id;
    $contactnote->contactnotetext = $request->contactnotetext;
    $contactnote->contact()->associate($contact);
    return $contactnote;
}

I'm assuming that your route is defined as a resource route due to your contactnotes.store route name, like

Route::resource('contactnotes', 'ContactNotesController');

https://laravel.com/docs/5.7/controllers#resource-controllers

You'll notice the store named route takes no parameters.

WallyJ
WallyJ
5 days ago (3,770 XP)

@Cronix , You are correct. I am using a resource controller.

I didn't realize that those controllers could not receive parameters like that.

I guess this is one problem with combining so many learning resources that tutorials get combined into things that sometimes don't work together. I never thought about passing the variable through a hidden field in the form.

Now when I dd($contact), I get the entire record for the correct contact in the DB.

Edited the rest of my code and my form works like a champ now. Thanks!

And thanks to everyone for their feedback. This community is awesome!

Cronix
Cronix
5 days ago (786,650 XP)

I didn't realize that those controllers could not receive parameters like that.

@wallyj It can, but you'd have to make an additional custom route for it and have it before the resource route in the routes dir. Some methods do take parameters by default, like the show/edit/update/destroy methods because they are operating on something that already exists so you need the ID. But store doesn't because you're creating something that doesn't exist yet, so there is no id to pass to it.

Please mark as solved if you have this working now.

jlrdw
jlrdw
5 days ago (251,930 XP)

@WallyJ go over parameters in the documentation again you are getting request but not parameters.

Looks like you're passing but not properly getting it in the controller.

And to me normal or regular routes seem to work just fine.

Please sign in or create an account to participate in this conversation.