Sven0188's avatar

Laravel - Controller Separation of Concerns with Relational Data

Good day to All,

I am just curious as to how you peeps are separating the controller workload.

Lets say I have the following controllers:

  1. ItemController
  2. ItemImageController

So it should be clear that an item may have multiple images.

Now when storing the data for relevant models:

How would you handle the upload / persisting of the Image record? Would you do your insert to db from the ItemController@store method?

If NOT - how do you do this as usually you would have the Item and Image fields on the same view and form?

Cause in my situation I am sitting with the following form:

        {!! Form::open(array('route' => 'items.store', 'method' => 'POST', "files" => true)) !!}
            <div class="row form-group">
                
                <div class="col-md-6">
                    {!! Form::label('name', 'Item:') !!}
                    {!! Form::text('name', "", ["class" => "form-control"]) !!}                     

                    {!! Form::label('description', 'Description:') !!}
                    {!! Form::textarea('description', "", ["class" => "form-control]) !!}
                    

                    {!! Form::label('photos[]', 'Photos:') !!}
                    {!! Form::file('photos[]', ["class" => "form-control", "multiple"]) !!}

                </div>
                </div>

            <div class="row form-group" >
                <div class="col-md-6">
                    {!! Form::submit("Save", ["class" => "btn btn-md btn-primary"]) !!}
                </div>
            </div>
        {!! Form::close() !!}

So you can see the form will be posted to: "items.store" ake ItemController@store. How will I store the photos from that controller? Should the ItemImageController@store method not be responsible for that and how do you reference ItemImageController@store from inside ItemController@store?

I really would like to keep controllers clean and also follow good practices.

Thanks :)

0 likes
4 replies
Sven0188's avatar

And lets say from ItemController@store I do call the ItemImageController@store method to take over the persisting of data.

How will my Laravel validation been handled on ItemImageController?

And also not finding a way to maintain my Repository pattern. When calling ItemImageController->store() I need to base a CrudInterface (my interacefor all crud controllers) parameter but not sure how to do this.

samalapsy's avatar

@Sven0188 I was thinking if you could do an ajax upload for your Image onChange. Validate your image both on client and server side too..

Hope It helps

Sven0188's avatar

Thanks for reply.

I was hoping for some example and more detailed advise. How would one structure and apportion your code in terms of the controllers.

Please or to participate in this conversation.