3
I’m starting with Laravel 5.1 and I’m a little lost yet.
I need to list categories, subcategories and "sub-subcategories".
Table:
Categorias
------------------------
id | int
nome | varchar
categoria_pai | int
Dice:
id | nome | categoria_pai
----------------------------------
1 | Informática | NULL
2 | Mouse | 1
3 | Sem fio | 2
4 | Eletrônicos | NULL
Model:
class Categoria extends Model
{
protected $table = 'categorias';
protected $fillable = ['nome', 'descricao', 'categoria_pai'];
public function produtos() {
$this->belongsToMany('App\Produto');
}
public function categoriaPai()
{
return $this->belongsTo('App\Categoria');
}
public function categoriaFilho()
{
return $this->hasMany('App\Categoria');
}
}
Controller:
Method that mounts select when adding another category. It works, but I don’t know how to filter it to make optgroup and separate.
public function create()
{
$categorias = Categoria::all()->lists('nome', 'id');
return view('categorias.adicionar', compact('categorias'));
}
I have no idea how to start, I’ve looked for similar things but I haven’t found.
With pure PHP I’ve already made, but using the Laravel, I’m cracking my skull.
But what do you need? Do relationship in SELECT or Model ?
– Diego Souza
@Diegosouza, on the model. I put my model in the question.
– buback