jeudi 7 janvier 2016

Laravel 5 - Handling data across tables

I have a system whereby different Documents can be created. Rather than make a new table for each Document, I changed the design as follows Design

I have a generic Document Controller, and for the create view for a particular Document, I have the form set up like so

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

In the store function, I create a Document by filling in the data for all necessary Models

$document = new Document();
$document->projectId = $project->id;
$document->save();

$documentType = new DocumentType();
$documentType->documentId = $document->id;
$documentType->name = Input::get('documentType');
$documentType->description = Input::get('documentTypeDesc');
$documentType->save();

$input = $request->all();

foreach($input as $key => $value) {
    if($key !== "filePath" && $key !== "documentType" && $key !== "documentTypeDesc") {
        $documentData = new DocumentData();
        $documentData->documentId = $document->id;
        $documentData->key = $key;
        $documentData->value = $value;
        $documentData->save();
    }
}

This all works great. Different Documents are represented by different views, each linked to the Document Model.

Say I am in Project 1, and I decide to create a Document call DocumentA. The view for DocumentA is then displayed too me, and it expects the inputs clientName, projectName, contact and startDate. When I fill out this Document, my database may look like the following

document

 id | projectId  | 
------------------
 1  | 1          | 
------------------


document_type

 id | documentId | name       |  description            |  
---------------------------------------------------------
 1  | 1          | Document A |  Document for new client|  
---------------------------------------------------------

document_data

 id | documentId | key         |  value      |  
----------------------------------------------
 1  | 1          | clientName  |  Google     |   
----------------------------------------------
 2  | 1          | projectName |  Analytics  |  
----------------------------------------------
 3  | 1          | Contact     |  Mr Sharp   |  
----------------------------------------------
 4  | 1          | startDate   |  29/12/2016 |  
----------------------------------------------

So you can see how everything is linked, and you can see how the document_data table is handling different types of inputs for different documents.

If a Document has already been created, the edit view for that Document should be displayed. So after creating the above, if I select this Document again, I should see this Documents edit page. I have this working, and I can run the following query to pass the Documents data to the view

$documentData =
    $project->document()
        ->join('document_data', 'documents.id', '=', 'document_data.documentId')
        ->select('document_data.key', 'document_data.value')
        ->where('documents.id', $result->documentId)
        ->get();

return View::make($documentLink.'Doc.edit', compact('project', 'documentData'));

Within the edit page, if I output documentData, I get the following which is correct

[
    {"key":"_token","value":"KH5LbzoamDwlp5HWk2wUBjekgS1INOgZDmXT0dwn"},
    {"key":"clientName","value":"Google"},
    {"key":"projectName","value":"Analytics"},
    {"key":"contact","value":"Mr Sharp"},
    {"key":"startDate","value":"29-12-2016"}
] 

I have a couple of questions. Firstly, in the edit page, what should the form Model too? Surely I can't use new App\Document again, because it is not a new object.

Secondly, I have the data in my view now, how can I display this in the inputs? I know I could use the old command, but not sure where to put it when I create my inputs like so

{!! Form::input('text', 'clientName', null, ['id' => 'cientName', 'class' => 'form-control']) !!}

Any information appreciated, really interested about what I should model the form too.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire