Laravel 5 – Get data via relationships

I am having issues with my system. Essentially, I create a project. Within this project, I can then create a document. I have a choice of several different documents, each taking different inputs etc. I initially had one table per document, but this got messy. As such, I changed my database design to be like the following Database design

So now I can use a single controller to handle all my different documents. On my projects show page, I have links for each document type e.g

<li>{!! link_to_route('projects.documents.create', 'Project Identified', array($project->id, 'documentType' => 'projectIdentified')) !!}</li>

The above shows the link if I decide to create a Project Identified Document.

Now if I select the Project Identified Document, I should be taken to the view for this Document. That is pretty straight forward because I can get the documentType parameter passed via the url

public function create(Project $project)
$documentType = $_GET["documentType"];

if($documentType == "projectIdentified") {
return View::make('projectIdentifiedDoc.create', compact('project'));

So that seems to work, and I can successfully store a document. The problem is this. Each project should only have one document of a certain type. So if I have already created a Project Identified Document for Project 123, I should not be able to create a new one. Now I think my relationships are set up correctly for this, and I have mapped these within the Models for the related classes.

This is what I am trying to achieve now. If a Project Identified Document has been created for the project I am within, if I select this Document again, it should show me the edit page for this Document Type. In essense, I am trying to do something like the following

public function create(Project $project)
$documentType = $_GET["documentType"];
$projectId = $project->id;

$criteria = ['projectId' => $projectId, 'documentTypeId' => $project->documentTypeId];

$documentCreated = Document::where($criteria)->first();

if(!$documentCreated) {
return View::make('projectIdentifiedDoc.create', compact('project'));
} else {
return View::make('projectIdentifiedDoc.edit', compact('project'));

So get all Documents where the project ID is the current Project ID, and the documentType Name is projectIdentified. If such Document exist, show the edit page for this document. If it does not exist, show the create page.

The clause I am using at the moment does not work. Because I only have the Document Object, I need to somehow use the documentTypeId to get access to the Document Name, so I can see if it is projectIdentified.

This is where I am having difficulties, I presume it needs a join or something.

Hopefully I have made it clear what I am attempting to do. Any suggestions or advice on how I can achieve this would be great.

Many thanks

from Newest questions tagged laravel-5 – Stack Overflow

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.