I've three tables and I would like to use pivot table data when getting results.
These tables are products, store, store_products, cart_items and their fields are below:
products (id, name, price)
store (id, name etc.)
store_products (store_id, product_id, store_price, store_slug)
cart_items (id, user_id, product_id, store_id, quantity)
My CartItem model is like
class CartItem extends Model
{
public function product()
{
return $this->belongsTo('App\Product');
}
public function store()
{
return $this->belongsTo('App\Store');
}
}
When I call CartItem::with('product')->get(), I would like it to return product data (from product table and store_price, store_slug from store_products) with matching cart_items.store_id and cart_items.product_id from pivot table "store_products".
How can I create this relation by on my CartItem model? If possible, by just using Eloquent ORM functions instead of query builder or raw queries.
In products Model add,
public function stores(){
return $this->belongsToMany(Store::class)->withPivot('store_price', 'store_slug');
}
In Store Model add,
public function products(){
return $this->belongsToMany(Products::class)->withPivot('store_price', 'store_slug');
}
Then call,
CartItem::with(['product.stores'])->get();
For more explanation read this article:
http://laraveldaily.com/pivot-tables-and-many-to-many-relationships/
Related
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?
I have a users table, a products table, and and an assets table. A user can have many assets, as can a product, meaning both Users, and Products have a 1:n relationship with assets. In Laravel how would show this relationship in Eloquent, is this a Polymorphic relationship, with the struct of the assets table columns being something like,
ID, type, file_path, ownable_id, ownable_type
I think your assets table columns should be
id, type, file_path, assetable_id, assetable_type
Add this relation to your user and product model
public function assets()
{
return $this->morphMany(Asset::class, 'assetable');
}
Next, add this relation to your asset model
public function assetable()
{
return $this->morphTo();
}
relation of user model
public function assets()
{
return $this->morphMany(Asset::class, 'assetable');
}
relation of asset model
public function assetable()
{
return $this->morphTo();
}
For example i have 4 tables:
buildings
id, title, ...
rooms
id, building_id, title, ...
companies
id, title, ...
companies_to_buildings
company_id, building_id
and i want to get companies from the room model.
From building model i can do something like
class Building extends Model
{
public function companies(){
return $this->belongsToMany('App\Company', 'company_building');
}
}
or
class Building extends Model
{
public function companies(){
return $this->belongsToMany('App\Company', 'company_building', 'building_id', 'company_id');
}
}
but if i do this in room model, it would not work. it will try to find rows in companies_to_buildings table where companies_to_buildings.building_id=room.id. how can i get companies from the room model?
You can make use of hasManyThrough
Example:
public function series()
{
return $this->hasManyThrough('Series', 'Product');
}
I have two models:
class Order extends Eloquent
{
public function User()
{
return $this->belongsTo('User');
}
public function Product()
{
return $this->belongsToMany('Product');
}
}
and the second one is:
class Product extends Eloquent
{
public function Order()
{
return $this->belongsToMany('Order');
}
}
My question is how can i access a value of second table column using pivot table:
product table:
id
title
image
order table:
id
status
pivot table(order_product):
id
product_id
order_id
I need to access title column of products from orders. for example if a user orders many products in one order I can fetch all product title and show theme.
I don't like use join , instead I like to use Laravel functionality itself.
I found the answer:
$orders = Order::orderBy('created_at', 'DESC')->paginate($page_number);
foreach($orders as $item){
foreach($item->product as $item){
{{$item->title}}<br>
}
}
I can access products from order.
I am trying to use Eloquent to get a specific product that has a brand_id column that maps to a brands table, the brand array is coming back empty.
Is there anything obvious here that needs to be changed?
$product = Product::with('images')->with('brand')->select($fields)->where('display', '=', 1)->find($id);
//Product model
class Product extends Eloquent {
...
public function brand()
{
return $this->belongsTo('Brand');
}
//Brand model
class Brand extends Eloquent {
...
public function products()
{
return $this->hasMany('Product');
}
You have this:
$product = Product::with('images', 'brand')
->select($fields)
->where('display', 1)
->find($id);
You are getting null for brand and it could be because you have some specific fields and most probably you didn't select the foreing_key from the products table that creates the relationship with Brand, so if your products table contains the foreign_key (probably brand_id) of brand table then you have to select that foreign_key from the products table too. So, just add that foreign_key/brand_id in the $fields variable. Without the relation builder key (FK) the Brand won't be loaded.