Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como obter categorias e subcategorias Query Builder em Laravel


Eu usaria modelos eloquentes
class Category extends Model
{
    protected $table = 'tbl_form'; // 'posts'

    public function posts()
    {
        return $this->hasMany(Post::class, 'category', 'category')->select(['title']);
    }
}
class Post extends Model
{
    protected $table = 'tbl_form'; // 'posts'

    public function category()
    {
        return $this->belongsTo(Category::class, 'category', 'category');
    }
}

E recupere assim:
$categories = Category::all();
dd($categories[0]->posts);