mardi 19 avril 2016

Restructuring database and multiple views for model

I have a system where a user can input data into various forms and generate a custom document. When I set everything up, because each form has unique data, I create a Model/Controller for each type of document e.g. BriefDocument, InvoiceDocument etc.

I soon discovered that this became very messy, way too many Models and Controllers. It also took quite a long time to create a new document type. As such, I have rearranged my database.

I now have a Document model and a DocumentData model. A Document can have many Document Data. I envision something like this

Document
+----+---------------+-----------------+
| id | name          | description     | 
+----+---------------+-----------------+
| 1  | BriefDocument | Something       |    
+----+---------------+-----------------+

DocumentData
+----+--------------+-----------------+--------------+
| id | key          | value           | documentId   |
+----+--------------+-----------------+--------------+
| 1  | whatData     | inputted data   | 1            |  
+----+--------------+-----------------+--------------+
| 2  | whoData      | inputted data   | 1            |  
+----+--------------+-----------------+--------------+
| 3  | whyData      | inputted data   | 1            |  
+----+--------------+-----------------+--------------+
| 4  | howData      | inputted data   | 1            |  
+----+--------------+-----------------+--------------+

Doing this should allow me to create any type of Document using just these two models. My first problem is this, I have set up the routes as follows

Route::model('projects.document', 'Document');
Route::resource('projects.document', 'DocumentController', ['except' => ['index', 'show']]);

On a page I have a dropdown where the user can select the type of document they create. The dropdown has things like this

<li>{!! link_to_route('projects.document.create', 'Brief Document', array($project->id, 'Brief Document')) !!}</li>

So this will call the create function within my DocumentController

public function create(Project $project, $name)
{
    $briefDocument = Document::where('projectId', '=', $project->id)
        ->where('name', '=', $name)
        ->first();
}

What I am trying to do in this function is first determine whether the same Document has already been created for this Project, because a Project can only have many documents, but no repeat documents.

If I do this however, I get

Missing argument 2 for App\Http\Controllers\DocumentController::create()

But am I not passing it arguement 2 within the link_to_route? From what I can see, this is passing my Project instance, as well as the string Briefing Document.

Why would I be getting this error?

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire