dimanche 5 février 2017

4 level laravel eloquent relationship

I have four tables and i want to make a 4 level relationship. My table looks like this:

------------|------------|------------|------------|
    City    |  Category  |Subcategory |   Company  |
------------|------------|------------|------------|
     id     |     id     |     id     |     id     |
    name    |    name    |    name    |    name    |
------------|------------|------------|------------|

I did something like this:

City.php
public function categories()
{
    return $this->belongsToMany(Category::class);
}

Category.php
public function subcategories()
{
    return $this->belongsToMany(Subcategory::class);
}

Subcategory.php
public function category()
{
    return $this->belongsTo(Category::class);
}

But it's not giving the result i need. I have many cities that can have many companies by their category and subcategories. For example i can save city_id, category_id, subcategory_id to company table and get the right comapnies doing the query: $company->where('city_id', 1)->where('category_id', 2)->where('subcategory_id', 3)->get();

But i think it's not a best practice. I hope somebody helps. Thanks.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lezP8q
via IFTTT

Aucun commentaire:

Enregistrer un commentaire