laravel 5.2 many to many relation retrieve data with intermediate table - php

I have tables :
products
id
categories
id
product_category
product_id
category_id
by using eloquent model relation, how to get all product data from a specific category id?

Assuming that you defined relationships on your models, you can try this:
$productName = 'Some product';
$category = Category::findOrFail($categoryId);
$product = $category->products->where('product_name', '=', $productName)->get();

Related

Laravel Relationship Query to load hasMany + BelongsToMany

I need to get brands data in specific collection page but only has more than 1 product.
Here are relations between models.
Brand -> HasMany -> Product
Product <= BelongsToMany => Collection
I was able to get brands data that have more than 1 products for all collections as following:
$brands = Brand::has("products")->get(); //this will return all brands that have more than 1 product.
Now I need to add collection limitation here.
I can get collection from $slug for specific page.
$collection = Collection::where("slug", $slug)->first();
Can anyone please help me how to get brands for specific collection page?
Try this:
$brands = Brand::has("products")
->whereHas('products.collections',function($q) use ($slug){ // your relation to product model
$q->where("slug", $slug);
})
->get();

How to return model with relationsip in laravel?

I have two model Product and Category.
The categories table have id, and name columns.
While the products table have id, name, and category_id [foreign_key to category].
Here is my below code (already have category with values of 1, "School Supply"):
$product = new Product;
$product->category_id = 1;
$product->name = "pencil";
$product->save();
return $product->with('category');
My question is why it returns an error below when I try to get the data with the relation model? Someone knows how to do it exactly?
[34;4mIlluminate\Database\Eloquent\Builder[39;24m {#4069}
You can lazy eager load the relations using load method instead of with.
with is used for eager loading.
$product->load('category');
return $product;

Laravel collection get occurrences of a MtoM relation

this is my situation:
I have a collection of products and i want all the categories of those products.
Between Product and Category there is a many-to-many relation already created, and i want to do something like this:
$prods = Product::where(something)->get();
$categories = $prods->categories();
But obviously this doesn't work, and i would avoid getting all categories for each products, and add it to a collection only if is not alredy in it... something like this:
select *
from categories join pivot on categories.id = pivot.id
where pivot.product_id IN (1,2,3,4,5)
where 1,2,3,4,5 are the ids of the products
Is there any way to do it without QueryBuilder (using Eloquent)?
You should define a many-to-many relationship in your Product model
public function categories()
{
return $this->belongsToMany(Category::class);
}
And then you can do this
$ids = [1,2,3,4,5];
$prods = Product::with('categories')->findOrFail($ids);
$categories = $prods->flatMap->categories;
The with call eager loads your categories, and then you retrieve them easily + flatten (to avoid multi-dimensional collection) thanks to flatMap method.

How to write query to get result form multiple tables in laravel

I am doing project in laravel. There are two tables "categories" and "subcategories". "subcategories" table have categoryid as a foreign key.
I want to fetch categoryname from categories table and subcategoryid,subcategoryname from subcategories table where categoryid = $categoryid.
Here is something what I did. $cat contains many categoryid's but, from this I got only subcategories details,
foreach($cat as $categoryid){
$subcategories[] = Subcategory::where('categoryid', '=', $categoryid)->get();
}
I am not getting how to do this,please help.

Pull all the fields from both tables in Laravel Many-to-Many relationships

Please any help me that how i pull all the fields from both tables in laravel many to many relationship.
I have two table
categories
id
cname
ctitle
description
products
id
pname
ptitle
pdescription
price
and bridge table
products_to_categories
id
category_id
product_id
So i have pull all the fields from both tables based on product id.
UPD: As commented patricus, the "bridge" table should be called "category_product"
First, you should define the "belongsToMany" relationship in the Product model:
// ...
class Product extends Eloquent {
// ...
function categories()
{
return $this->belongsToMany('Category');
}
}
After it you should load the product by id:
$product = Product::find($product_id);
And now you can access its categories through the "categories" property:
foreach($product->categories as $category)
{
// do what you want with the $category
}
Official docs for M2M relationships:
http://laravel.com/docs/4.2/eloquent#many-to-many

Categories