Laravel 5 – Display different views based on database query

I have the following database design 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

from Newest questions tagged laravel-5 – Stack Overflow http://ift.tt/1TEj2pQ
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.