Support the ongoing development of Laravel.io →
Views Blade Forms

My controller and view code is getting messy. This post is more of a general question as to what others do in their development. So many tutorials on the web regarding separating code use such basic examples it's difficult to apply them to a real world situations. Thus, why I'm using a real world situation and asking for help.

In this example, I'm using a Project edit method in my controller and added comments as to what the code is for. Model Relationships: Groups ----> Projects Projects ----> Expeditions Projects ----> Actors Expeditions <----> Actors (for processing each expedition according to the project's actors and recording results) Expeditions ---- WorkflowManagers (Managers what is being processed)

/**
 * Show the form for editing a project.
 *
 * @param $id
 * @return \Illuminate\View\View
 */
public function edit($id)
{
    // Get the project being edited with needed relationsships
    $project = $this->project->findWith($id, ['group', 'actors', 'expeditions.workflowManager']);
	
    // Check if a Project expedition is being processed. Cannot change Projects actors if process exists. Sets Actor select box to readonly in view if process exists
    $workflowCheck = false;
    foreach ($project->expeditions as $expedition)
    {
        $workflowCheck =  ! $expedition->workflowManager->isEmpty() ? true : false;
    }

    // Get Actor's select box list.
    $actors = $this->actor->selectList();

    // Get current user and check if Admin or not.
    $user = $this->user->getUser();
    $isSuperUser = $user->isSuperUser();

    // Get available groups for drop down depending on if user is Admin or not 
    $allGroups = $isSuperUser ? $this->group->findAllGroups() : $user->getGroups();
    $groups = $this->group->selectOptions($allGroups);

    // Add Select to select group options
    $selectGroups = ['' => '--Select--'] + $groups;

    // Cancel button to not edit Project
    $cancel = URL::previous();

    return View::make('projects.edit', compact('project', 'actors', 'workflowCheck', 'selectGroups', 'cancel'));
}

In my view, there is also a bit of messiness when dealing with input arrays. Below is a section dealing with Actors. In essence, I want to show select inputs for all Actors that exist so a user can select which ones they want associated with the project. In the same vein, I show what is already selected.

@for($i = 0; $i < count($actors); $i++)
<div class="form-group {{ ($errors->has(actor['.$i.'])) ? 'has-error' : '' }}">
    {{ Form::label(actor['.$i.'], trans('forms.actor'), array('class' => 'col-sm-2 control-label')) }}
    <div class="col-sm-10">
        {{ Form::select(actor['.$i.'], ['' => '--Select--'] + $actors, (isset($project->actors[$i]) ? $project->actors[$i]->id : null), array('class' => 'form-control', 'placeholder' => trans('forms.actor'))) }}
    </div>
    {{ ($errors->has(actor['.$i.']) ? $errors->first(actor['.$i.']) : '') }}
</div>
@endfor

So.... is there a better place to place the controller code and pass it to a view? What about handling foreach loops in the view? Perhaps a helper that runs the loop given the values and view partial? Any ideas on what others do would be helpful.

Edit: I didn't put in the WorkflowCheck in the above view. I was working on that when I thought, "There must be a better way."

Last updated 2 years ago.
0

Blade has many of these things cleaned up for you and then just use building methods to do all data processing in the controller then pass to blade for rendering. I also like to follow some kinda of convention when building my pages and i have found that a plugin system is best when trying to keep view data processing out of the controlling handling all the loading.

Last updated 2 years ago.
0

Well, there's something you can do about looping in the view for your select input.

Assume I have a model class called Categories.

In this model I have a list of available categories to the user in a table. (fields-> id, category_name)

In my controller method that presents the view to the user, I can pass an array of that data to our view for parsing, but first I need to re-organize it into an array of key/value pairs (key = id, value = category_name ).

/* in the controller */
public function index() {
        $categories = Categories::all(); //get all categories
        $categoriesArray = array(); //initialise array
        $categoriesArray[0] = "--Please Select A Category--"; //gotta have that default
        foreach($categories as $category) { //construct new array of key/val pairs
            $categoriesArray[$category->id] = $category->category_name;
        }

        return View::make('<view path>', compact('categoriesArray'));
}

Then, in your view, you can simply just write this line inside your form:

{{ Form::select('category', $categoriesArray , null, ['id' => 'categorySelect'] ) }}

*null value is where you can put the value of the default

Laravel will automagically take the array you used and construct all of the select items in the form using the key as the select value and the value of that key as the text for each item.

Let me know if this is a bit better/worse for you.

Last updated 2 years ago.
0

Thanks for the replies. @jgetner - What do you mean by build methods? Perhaps pass the actors to a method that builds the html, then pass that result to the view? Or a plugin to do this?

@ayyobro - My $this->group->selectGroups pretty much does what you are explaining. The reason I have to run the ternary operation on the groups is due to how Sentry works and whether the person is an Admin. Each user also belongs to a group called Users, which I don't show to them. Thus, the building of the selectGroups.

The actors select list is retrieved using Eloquent list(); However, a select input is generated for each actor that exists. Meaning, if there are 3 actors, then 3 select inputs, each holding the 3 actors as options, appear on the form so they can select the order by which they wish to process the actors. Hence, why I use the @for loop to build them.

Thanks

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

rbruhn rbruhn Joined 16 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.