I have the following database design
Within the Models, I have set up the relationships appropriately. Now this is the tricky part I am having difficulties solving. I have many different documents, each of them expect different inputs. I have a view for each of these different documents so the forms for them can capture the correct information. I then use the above database design to store a documents data and files etc.
I have the following routes
projects.documents.create
projects.documents.store
projects.documents.edit
projects.documents.update
So although I have several documents, they use the same routes, just different views. Within my projects page, I provide a dropdown where a user can select the type of document they want to produce. The links take the following form
<li>{!! link_to_route('projects.documents.create', 'Project Identified', array($project->id, 'documentType' => 'projectIdentified')) !!}</li>
So I am passing the documentType so I can keep track of it. A project can have many documents, but only one document of a particular type.
This is where I am having difficulty. In my Document Controllers create function, I need to see all Documents which have been created for a project. I can do this by doing something like the following
public function create(Project $project)
{
$documentTypesCreated =
$project->document()
->join('document_type', 'documents.id', '=', 'document_type.documentId')
->select('document_type.documentId', 'document_type.name')
->get();
dd($documentTypesCreated);
$documentLink = $_GET['documentType'];
}
$documentTypesCreated is an array of all the Documents created for that Project. $documentLink is the variable I pass in the link_to_route so I know what type of Document the user is trying to create.
If the type of Document they are trying to create has already been created, then the user should see the edit page for this Document. Otherwise, they should see the create view. So I have something like this
public function create(Project $project)
{
$documentTypesCreated =
$project->document()
->join('document_type', 'documents.id', '=', 'document_type.documentId')
->select('document_type.documentId', 'document_type.name')
->get();
$documentLink = $_GET['documentType'];
foreach($documentTypesCreated as $documentName) {
if(!$documentName->name == $documentLink) {
return View::make('projectIdentifiedDoc.create', compact('project'));
} else {
return Redirect::route('projects.documents.edit', compact('project'));
}
}
}
It is essentially saying this project has these Documents, the user is trying to create this document type, if it has already been created, show the view page.
I kind of have it working, however, for the edit view, I also need to pass the Document Object so I can display all the old information.
How would I get the Object for the chosen Document, and then pass it to the edit page? Thanks
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire