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

Asked

Viewed 744 times

7

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 already created the model with hasMany and I see that it returns in a structure chained to the parent entity and its children recursively.

But I cannot create a select box that only accepts an array as a parameter. There is in the Laravel some "predicted" way of converting this structure from Items to an array in the format accepted by \Form::select() ?

  • Have you tried the lists()?

  • I also use creation through the method lists('descricao','id')

2 answers

8

Following the development patterns, do so:

In his model Item 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 parameter $optional of this method is only to define whether you will have a default option (no value) in your Grouped List, for example: Select an item.
  • After this there is only one check whether it has been informed or not, otherwise it returns only the list of elements

Obs:

If you wanted to add a condition to 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 model-defined Scope (this is 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');
}
  • If someone’s reply to your question has helped you, be sure to mark as solved by clicking the arrow below the voting area.

  • +1 There is someone who understands Laravel well :)

3

Say your model who inherits from the Eloquent is called Items, then to recover all the records, you can use:

Itens::all()

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

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

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

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

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

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

I couldn’t quite understand the question, but I guess that’s it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.