vendredi 18 décembre 2015

Laravel 5 - relationships and handling data

I have a system whereby you can create Documents of different types. Initially, I had a new Model for each Document, but I don't think this is the best way because it can get messy fast. So what I wanted to do is make a generic Documents model, and have individual documents come from this. I have come up with the following type of design Database design

So a Document can have one DocumentA and one DocumentB. DocumentA and DocumentB can only ever be created once per project which is why I have this relationship. Now each form for each document has an upload button, where supporting documents can be uploaded alongside the generated document. So Documents can have one to many FileUploads.

This is where I am confused. A person visits my portal and selects the option to create DocumentA. A form is now displayed to them which looks something like the following DocumentA

So they enter the data for DocumentA, upload supporting documents, and then click submit.

Now I am thinking about how this can be handled within Laravel.

From what I understand, it will be something like the following

public function store(Request $request, Project $project)
{
    $document = new Documents();
    $document->documentName = 'Something';
    $document->documentA = new DocumentA();
    $document->documentA->startDate = Input::get('startDate');
    $document->documentA->endDate = Input::get('endDate');
    $document->documentA->notes = Input::get('notes');

    if (Input::hasFile('filePath')) {
        $files = Input::file('filePath');
        foreach($files as $file) {
            $fileString = "";
            $file->move(public_path('uploads'), $file->getClientOriginalName());

            $fileString .= public_path('uploads') . '/' . $file->getClientOriginalName();

            $document->fileUpload = new FileUploads();
            $document->fileUpload->filename = $file->getClientOriginalName();
            $document->fileUpload->mime = $file->getClientOriginalExtension();
            $document->fileUpload->filepath = $fileString;
            $document->fileUpload->documentId = $document->id;
        }
    }

    $document->save();

    return Redirect::route('projects.documentA.edit', compact('project', 'documents'));
}

Really, looking for advice as to whether I am designing this correctly, and whether I am handling it correctly within Laravel. I am going to end up with many different Documents, each of them accepting different input.

Any advice appreciated.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire