I'm starting with Laravel 5.1 and I'm a bit lost yet.
I need to list categories, subcategories and "sub-subcategories".
Table:
Categorias
------------------------
id | int
nome | varchar
categoria_pai | int
Data:
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 assembles the select when adding another category. It works, but I do not know how to filter this to 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 have not found it.
With pure PHP I've done it, but using Laravel , I'm breaking my head.