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.
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.
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community