Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get many to many relationship items in laravel

There is a data structure for a e-shop:

Series -> (many to many) -> categories -> (many to many) -> products

For example, series is "Outdoor Series" , categories is "t-shirt" , products are "t-shirt A, t-shirt B, etc... "

And here is the controller that list out products in one category

public function view($series = 0, $cat = 0, $page = 1) {
        $category = Category::find($cat);

        $totalItems = count($category->product);
        $itemsPerPage = 30;
        $currentPage = $page;
        $urlPattern = "/ums/product/view/$series/$cat/(:num)";

        $this->data['product_list'] = $category->product()->orderBy('created_at', 'desc')->skip(($page - 1) * $itemsPerPage)->take($itemsPerPage)->get();
        $this->data['paginator'] = new Paginator($totalItems, $itemsPerPage, $currentPage, $urlPattern);
        $this->data['category'] = $category;
        $this->data['page'] = $page;
        return view('product/list')->with($this->data);
    }

Now, the problem is , I would like to rewrite the code so that instead of showing one category, I would like to show one series as well.

That means if the $series = 0 , then it shows products in one category, if the $cat = 0, then it shows products in multi category

In laravel how to get the products in multi category? try $series->category->product() but no luck, also how to rewrite that function to support showing of the series?

Thanks a lot.

like image 529
user782104 Avatar asked Jun 03 '16 08:06

user782104


People also ask

What is many to many relationship in Laravel?

Laravel Many To Many Relationship relates a record on the table to one or many records in another table and vice versa. Unlike Laravel One to Many and One to One, the key is a pivot table in this relationship. The pivot table is a table where you define between two tables to link them.

What is a pivot table in Laravel one to many?

Unlike Laravel One to Many and One to One, the key is a pivot table in this relationship. The pivot table is a table where you define between two tables to link them. A pivot table allows the relationship id from one model to be related to many other models and vice-versa. One real-life example we can think of is products and categories.

How do you define a many to many relationship in SQL?

The key in many to many relationship is the join (or pivot) table. The pivot table allows the relationship id from one model to be related to many other models and vice-versa. Many-to-many relationships are defined by writing a method that returns the result of the belongsToMany. In our example, we will define the two models.

What is many to many relationship in Salesforce?

What is Many To Many Relationship? Many To Many Relationship relate a record in on the table to one or many records in another table, and vice versa. Unlike One to Many and One to One, in this relationship type, the key is the join (pivot table) you define between two tables to link them.


3 Answers

Assuming Laravel Model classes - Series, Category and Product

For the Series Model Class, create a function

   public function categories()
   {
        return $this->belongsToMany('App\Category');
   }

For the Category Model Class, create a function

   public function products()
   {
        return $this->belongsToMany('App\products');
   }

Now for a given Series, you can easily retrieve all related categories using the simple function call

$categories = $series->categories();

Finally coming to the main problem of showing products under multiple categories.

for($categories as $category)
{
     $productsOfThisCategory = $categories->products();
     //save into some other data structure, say an array $allProducts
}

$allProducts will have multi-category products for a specific Series.

Refer : Standard eloquent relationship -Many to Many

like image 67
Paras Jain Avatar answered Oct 05 '22 12:10

Paras Jain


You can use this answer for sorting.

How to sort by a field of the pivot table of a many-to-many relationship in Eloquent ORM

like image 29
Himanshu Raval Avatar answered Oct 05 '22 13:10

Himanshu Raval


If I understand you correctly, then your models looks like below

class Series extends Model
{

    // other code

    public function categories() {
        return $this->belongsToMany('App\Category');
    }

    // other code
}

class Category extends Model
{

    // other code

    public function series() {
        return $this->belongsToMany('App\Series');
    }

    public function products() {
        return $this->belongsToMany('App\Product');
    }

    // other code
}

class Product extends Model
{

    // other code

    public function categories() {
        return $this->belongsToMany('App\Category');
    }

    // other code
}

Further to get all products of certain series you need to do so

public function view($series = 0, $cat = 0, $page = 1)
{
    if (!empty($series)) {
        $seria = Series::with(['categories' => function($query) {
            $query->with('products');
        })->find($series);
        // or may be this will work, don't know
        // Series::with('categories.products')->find($series);

        // get all caegories from seria or certain one
        if (empty($cat)) {
            $categories = $seria->categories; 
        }
        else {
            $categories = $seria->categories()->where('id', $cat)->get; 
        }

        // retrieve produts form each category and making from them collection
        $products = $categories->map(function($category) {
            return $category->products;
        })->flatten();

        // or use this approach if above not working
        /*$products = collect([]);

        foreach ($categories as $category) {
            $produts = $products->merge($category->products);
        }*/

        // do your magic
    }
    else {
        // not exactly understand what you want to do when $series is not set
    }

    // do your magic
}
like image 37
huuuk Avatar answered Oct 05 '22 12:10

huuuk