How to create a Grouped List in Laravel 4 from an entity with self-relationship

6

I'm trying to create a select box com \Form::select() (grouped list) in Laravel 4 and I have the following entity:

Items

id = id do item
nome = nome do item
pai = fk dessa mesma entidade

I have already created the model with hasMany and I see that it returns in a linked structure the parent entity and its children recursively.

But I can not create the select box that only accepts an array as its parameter. Is there any way in laravel to "convert" this structure of Items to an array in the format accepted by \Form::select() ?

    
asked by anonymous 12.12.2013 / 12:06

2 answers

7

Following the development standards, do so:

In your Item model, create the following method:

/**
 * getList method
 * Retorna a lista de itens
 *
 * @access public
 * @return Array
 * @since 1.0
 * @version 1.0
 * @author rogersneves
 */
public static function getList($optional = true)
{
  if ($optional) {
    return array('' => 'Selecione (opcional)') + static::lists('nome', 'id');
  } else {
    return static::lists('nome', 'id');
  }
}

On your controller

$items = Item::getList();
return View::make('sua_view')->with('items', $items);

Explaining:

  • The $optional parameter of this method is just to set whether to have a default (non-value) option in your Grouped List , for example: Select an item .
  • After this there is only one check if it has been informed or not, otherwise returns only the list of elements

Note:

If you wanted to add a condition in this method, just do the following:

return array('' => 'Selecione (opcional)') + static::where('status', 1)->lists('nome', 'id');

Or, in my case:

return array('' => 'Selecione (opcional)') + static::active()->lists('name', 'id');

Being active() a scope defined in the model (this is an example only, not applied to your case, or if you wanted, change the name of the fields):

/* ----------------------------------------------------------------------------
| Scopes
| -----------------------------------------------------------------------------
|
| Escopos pré-definidos
|
*/
/**
 * scopeActive method
 *
 * @access public
 * @param Array $query
 * @return void
 */
public function scopeActive($query)
{
  return $query->where('active', 1)->orderBy('name');
}
    
13.12.2013 / 13:18
2

Let's say that your model that inherits from Eloquent is called Items , so to retrieve all the records, you can use:

Itens::all()

This returns the data but does not solve the problem, you could even try to populate the data with

{{ Form::select('meucampo', Itens::all() )}}

But this would result in a json in the field, which we can solve with

{{ Form::select('meucampo', Itens::all()->lists('nome', 'id')) }}

See, to Form :: select I'm passing 2 parameters, the first is the field name in the form, the second is the list of values.

Itens::all() returns all items of Model , and lists turns into array associative, with fields to pass as parameter.

I could not clearly understand the question, but I think that's it.

    
12.12.2013 / 19:43