Hi, I create a Project, then within this project I can create different Documents. I have a generic documents table and a document data table. The document data table store the key (input label) and value (input). Doing it this way, I can create any documents I want, all I need to do is create a new view for a new document. To give you an idea, my database might look like
project
id | projectName |
------------------
1 | Project One |
------------------
document
id | projectId | documentName |
--------------------------------
1 | 1 | DocumentA |
--------------------------------
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 |
----------------------------------------------
The create views form is linked to my Document model
{!! Form::model(new App\Document, [
'class'=>'form-horizontal',
'route' => ['projects.documents.store', $project->id]
]) !!}
In my store function I do
$document = new Document();
$document->projectId = $project->id;
$document->name = Input::get('documentType');
$document->description = Input::get('documentTypeDesc');
$document->save();
$input = $request->all();
foreach($input as $key => $value) {
$documentData = new DocumentData();
$documentData->documentId = $document->id;
$documentData->key = $key;
$documentData->value = $value;
$documentData->save();
}
return View::make('documentA.edit', compact('project', 'document'));
So this all works fine, and when I create documentA using documentA's create view, I get an output like the database tables I have shown above.
I am then shown the edit page for that document. I think I have the form's model for this correct because it is now linked to a Project.
{!! Form::model($project->document, [
'class'=>'form-horizontal',
'method' => 'PATCH',
'route' => ['projects.documents.update', $project->id, $document->id]
]) !!}
My issue is with the store function where I need to update this Document. Say for this Project I have created two documents; DocumentA and DocumentB. In the edit view for the document, if I do
{{ $document->documentData }}
I can see all the data related to that Document, no other document data is present (as it should be). However, in the update function, if I do
public function update(Request $request, Project $project, Document $document)
{
dd($project->document);
return null;
}
That shows me all documents, not just DocumentA as needed. As such, I think the model for the edit form is incorrect.
How can I get access to the correct Document within the update function?
Thanks
Hi, I am still struggling quite badly with this. In my edit view, I can output the correct Document and the data for that Document. When I submit though and it hits the update function, the Document is an empty Model.
Any advice appreciated.
Thanks
from what i understand the question to be; In your Project model do you have a function that looks something like this document function below?
public function document() { //Document Model whatever that is called. return $this->hasMany('Document', 'documentId', 'id'); }
if you do you can just use it like this $newDocument = $project->document()->create([ array of key values here ]); and you can get all the documents with something like this $projectDocuments = $project->document; or $projectDocuments = $project->document->get();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community