How to display related products through pivot table? - php

I want to display related item which is in same category.
Here is my 3 table structure
food item table
:
"food_item_id",
"name" ,
"image" ,
food item category table :
"food_item_category_id"
"name"
pivot table
id,
food_item_id
food_item_category_id
FooItem model:
public function foodItemCategory() {
return $this->belongsToMany(FoodItemCategory::class, 'food_items_have_categories', 'food_item_id', 'food_item_category_id')
->withPivot('food_item_id', 'food_item_category_id')
->withTimestamps();
}
Food Category Model:
public function foodItem() {
return $this->belongsToMany(FoodItem::class, 'food_items_have_categories', 'food_item_category_id', 'food_item_id')
->withPivot('food_item_id', 'food_item_category_id')
->withTimestamps();
}
I want to get all food items from a specific category. Suppose a user clicks on a food item which Id is 1 and It is belongs to category ID 2. Now I want to show more food items which is in the category ID 2. I want to display that in my view blade.
Now, how can I display the related products in view which is in the same category?

As your relation is many to many, you can do this:
$food = FoodItem::find(1);
$categories = $food->foodItemCategory;
$items = [];
foreach($categories as $category) {
$items[$category->id] = $category->foodItem;
}
then you can pass $items to your blade template.

Related

fetch join table data using eloquent laravel

I am new to laravel & want to implement eloquent relationship.
Let me explain.
Consider I have 2 tables
products
product_id
product_name
brand_id
price
brands
id
brand_name
Each product will have one brand Id.But in Brands table, there is no product id. One brand_id can be in multiple product rows, and one product has one brand_id only. I want to select some col from products table plus brand_name with respect to brand_id of products table using Model.SO in Product model I wrote:
public function brands()
{
return $this->hasOne('App\Brand','product_id');
}
and in Brand model I write:
public function products()
{
return $this->belongsTo('App\Product','brand_id');
}
Now I want the result:
product_name
price
brand_name
How can I fetch those data in controller using eloquent relation? Also, the way I wrote Model relationship, Is it ok??
Your Product Model relation will be below
public function brand(){
return $this->belongsTo('App\Brand','brand_id');
}
public function product(){
return $this->belongsTo('App\Product','product_id');
}
Now in controller you can add query as below.
$products = Product::with('brand','product')->get();
echo '<pre>'
print_r($products->toArray());
exit;
It looks to me like you want a one-to-many relationship, so one brand can have many products and many products belong to one brand.
Product model:
public function brand()
{
return $this->belongsTo('App\Brand');
}
Brand model:
public function products()
{
return $this->hasMany('App\Product');
}
Then you would be able to get the brand information like this:
The full brand model:
Product::first()->brand
The brand name:
Product::first()->brand->brand_name
From the docs:
A one-to-many relationship is used to define relationships where a
single model owns any amount of other models. For example, a blog post
may have an infinite number of comments.
P.S.:
Your table column names do not make much sense to me, why do you have product_id on products but then on brands it is just called id? Why do you have product_name but then just price? why is it not just name or product_price, so your at least consistent?

Laravel Category, subCategory and Post

I have a 1 category and subcategory table
table name: categories
another table is posts posts table
when click on google app script all post regarding category and sub category of google app script will show enter image description here
my controller code is and name of controller is bloglistController
$posts = post::where('category_id', $id)->paginate(7);
but this code shows only category id post I want post of both category and sub category
For category model
public function post()
{
return $this->hasMany(Post::class, 'parent_id', 'category_id');
}
In your controller
$category = Category::where('parent_id',1)->first();
$post = $category->post;

Querying Laravel Eloquent Relationship

I have three tables.
Categories
Products
Brands
I have a relation on my categories table to the products like so:
public function products()
{
return $this->belongsToMany('App\Product','product_sub_categories','subcategory_id','product_id');
}
I have a relation on my Products table to the brands like so:
public function manuf()
{
return $this->belongsTo('App\Brand','brand');
}
I'm querying the categories table to return products of that category by a certain brand.
For example.
I wan to see all products in Cars category with the brand Fiat.
I've tried the following but I feel Im missing something..
$search = 'fiat';
$products = $category->products()->where(function($query) use ($search){
$query->brand->name = $search;
})->get();
to return products of that category by a certain brand
I assume that you know brand ID and category ID and that products and categories have many to many relationship (since you're using belongsToMany) and product belongs to brand:
Product::where('brand_id', $brandId)
->whereHas('categories', function($q) use(categoryId) {
$q->where('id', $categoryId);
})
->get();

found error in laravel 5.4,Property [categories] does not exist on this collection instance

I am trying to display the product listing on listing page of the product. Each product has category.My table structure
categories
id name description
1 Cat1 Category 1
2 Cat2 Category 2
This is the category table having id name and description
products
id name description category_id
1 pro1 product 1 1
2 pro2 product 2 2
This is the product table having category_id.
Product Model
public function categories() {
return $this->belongsTo("App\Category");
}
This is the product model where the products are belongs to category
Category Model
public function products() {
return $this->hasMany("App\Product");
}
This is the Category model where the Category has many product
Now in the product controller on listing function I want the list of product with category name
public function index()
{
$product = Product::with('categories')->get();
print_r($product->categories);die;
return view('product.index')->with("list",$product);
}
I want my Output should be
products
id name description category name
1 pro1 product 1 cat1
2 pro2 product 2 cat2
I found this error "Property [categories] does not exist on this collection instance."
When you run:
$product = Product::with('categories')->get();
you are not getting single products but all products so it should be rather renamed to:
$products = Product::with('categories')->get();
Further, looking at your database structure, each product belongs to single category so in Product model you should rename
public function categories()
{
return $this->belongsTo("App\Category");
}
to
public function category()
{
return $this->belongsTo("App\Category");
}
If you do this change, you should then again change
$products = Product::with('categories')->get();
to
$products = Product::with('category')->get();
Going back to your controller assuming all your products have set category, you can display it like this:
foreach ($products as $product)
{
echo $product->id.' '.$product->name.' '.$product->description.' '.$product->category->name;
}
You can obviously do the same later in Blade view like so:
#foreach ($list as $product)
{{ $product->id }} {{$product->name}} {{ $product->description }} {{ $product->category->name }}
#endforeach
I have changed in ProductController which is my product controllers function
public function index()
{
$products = Product::with('category')->get();
return view('product.index')->with("list",$products);
}
Also changed in Product Model
public function category() {
return $this->belongsTo("App\Category");
}
After these changes I got my expected result.

Display posts in category (Laravel)

I have some tables:
category:
id: 1 title: Shirt
id: 2 title: Pant
id: 3 title: Hat
product:
... (product has some columns and 1 col is category_id)
And what I want to display in view is:
1. 5 newest posts for all categories
2. 5 newest posts for each category
Basically, it'll look like:
--* Title :All *--
5 Newest posts
--* Title :Shirt *--
5 Newest posts which category_id :1
--* Title :Pant*--
5 Newest posts which category_id :2
--* Title :Hat *--
5 Newest posts which category_id :3
Any advices, I've searched on google but didn't find the answer :((
Since you have two tables, you'd create two models, one for category and one for product. You can do this by running php artisan make:model Category and php artisan make:model Product
Now you have these models in your App/ folder.
Add the following to your category model in App\Category
public function products()
{
return $this->hasMany('\App\Product', 'category_id', 'id');
}
Go on to the controller that wants to process these data and add at the top
use App\Category;
use App\Product;
5 newest posts for all categories
$product = Product::take(5)->orderBy('id', 'desc')->get();
5 newest posts for each category
$categories = Category::get();
foreach ($Categories as $Category) {
$products[$Category->name] = $Category->products()->take(5)->orderBy('id', 'desc')->get();
}
Now to pass this on to the view, i like using a "$data" array, the contents of this array can be accessed directly in the views once passed like so
$data = array();
$data['all_products'] = $product;
$data['category_products'] = $products;
This can then be passed on as
return View('products.detail', $data);
where products.detail is your view
You can now loop through these data in your view as
--* Title :All *--
#foreach($all_products as $product)
{{$product->name}}
#endforeach
If you have category model like this:
class Category extends Model
{
public function posts()
{
return $this->hasMany('\App\Post', 'category_id', 'id');
}
}
You can take 5 Latest post's of every category easily:
$Categories = Category::get();
foreach ($Categories as $Category) {
$Posts = $Category->posts()->take(5)->orderBy('id', 'desc')->get(); // Get 5 Posts
}

Categories