vendredi 18 mars 2016

Passing Object between Models

I have a a couple of Models like so (some code removed for simplicity)

class Poll extends Model
{
    public function poll()
    {
        return $this->hasOne('App\PollQuestion', 'pollId');
    }
}

class PollQuestion extends Model
{
    public function poll()
    {
        return $this->belongsTo('App\Poll');
    }
}

I then have all my routes set up, for the PollQuestion they currently look like this

Route::model('polls.questions', 'PollQuestion');
Route::get('/admin/polls/{id}/addquestions', [
    'as'   => 'polls.questions.create',
    'uses' => 'PollQuestionController@addQuestions'
]);
Route::post('/admin/polls/{id}/storequestions', [
    'as'   => 'polls.questions.store',
    'uses' => 'PollQuestionController@storeQuestion'
]);

In my PollQuestionController, to see the questions view I have

public function addQuestions(Request $request)
{
    $poll = Poll::where('id', '=', $request->id)->first();
    return view('poll.admin.questions', compact('poll'));
}

Inside this view if I dump the poll e.g.

{{ dd($poll) }}

I can see what I expect to see. For the question form, I am doing

{!! Form::model(new App\PollQuestion, [
   'route' => ['polls.questions.store', $poll]
]) !!}

So I presume that should pass my store function the Poll Object I previously dumped. However, in the store function, I do

public function storeQuestion(Request $request, Poll $poll) {
    dd($poll);
}

And it shows Null. Why would this happen seeing that I am passing it the Poll Object I had previously dumped?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire