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 AppDocument, [
'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/2022 |
----------------------------------------------

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-2022"}
]

I have a couple of questions. Firstly, in the edit page, what should the form Model too? Surely I can’t use new AppDocument 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

from Newest questions tagged laravel-5 – Stack Overflow http://ift.tt/1TGCORC
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.