lundi 4 janvier 2016

Laravel 5 - route to create not working

I have 2 models, Project and Document. I have set it up so that a Project can have may Documents, and a Document belongs to a Model.

I have then done a route like so

Route::model('projects.documents', 'Document');
Route::bind('projects.documents', function($value, $route) {
    return App\Document::whereId($value)->first();
});
Route::resource('projects.documents', 'Docs\DocumentController');

If I view my routes, I can see that I have routes like

projects.documents.create
projects.documents.show 
etc

Now my form to create a new document looks like the following

{!! Form::model(new App\Document, [
    'class'=>'form-horizontal',
    'route' => ['projects.documents.store']
]) !!}

<div class="form-group">
    {!! Form::label('name', 'Document Name:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        {!! Form::text('name', null, array('class' => 'form-control')) !!}
    </div>
</div>
<div class="form-group">
    {!! Form::label('description', 'Document Description:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        {!! Form::textArea('description', null, array('class' => 'form-control')) !!}
    </div>
</div>
<div class="form-group">
    {!! Form::submit('Create Document', ['class' => 'btn btn-primary']) !!}
</div>

{!! Form::close() !!}

Now when on the create page for a Document, the url looks like the following

http://localhost:8000/projects/1/documents/create

This seems correct to me, as the document is under a project id (1). In my Documents store function, I am currently doing the following

public function store(Request $request, Project $project)
{
    $document = new Document();
    dd($project->id);
    $document->projectId = $project->id;

    return Redirect::route('documents.edit', compact('project', 'document'));
}

The result of the dd is null though, so its not recognising the project it is associated with. Also, when I submit the form, the url is like this

http://localhost:8000/projects/{projects}/documents

So it has lost the project id.

Why might this be happening?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire