mardi 5 janvier 2016

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 http://ift.tt/1RpHsUR
via IFTTT

Aucun commentaire:

Enregistrer un commentaire