Eloquent relationships laravel - laravel-4

I have 3 tables
categories
id
name
categories_product
id
category_id
product_id
and a product table
products
id
name
I want products based on catergori_id, how kan i do that with laravel Eloquent?

Simple as this:
Product::whereCategori_id($category)->get();

Related

laravel eloquent ORM Joint multiple table without Foreign key And Multiple model

I have two tables. One is user and the other is category, I have written this Eloquent ORM query to retrieves data from category
$category = Category::where(['status'=>'active'])->orderBy('id','asc')->get();
In category table here has a field called user_id. I want to join the user table and want to retrieve the user name where category table 'user_id' == user table 'id'.
How can I do this in Laravel eloquent ORM without Foreign key And Multiple models.

How to access the fields in the table in many-to-many intermediate tables?

How to access the fields in the table in many-to-many intermediate tables?
What kind of eloqunet builder is required to access the isAlternative field in the table named category_product?
Category table
categories
- id
- name
Product table
products
- id
- name
- price
CategoryProduct table
category_product
- category_id
- product_id
- isAlternative

Lavavel Eloquent: Relationships with 5 table

I have an app consisting of these tables
users
id
name
user_location
id
user_id
cities_id
cities
id
state_id
country_id
name
states
id
country_id
name
countries
id
name
Then how to get relationship this five table?
you have to create a many-to-many relationship between tables and use pivot table to show the relationship between table rows using foreignId

how to get data from pivot table inside a many to one relation in laravel 5.6

I have 3 tables name like "product" , "user", "product_type" so in my case user and product_type having many to many relationship and user and product having one to many relationship and product and product_type having one to one relationship.
I create one pivot table for user and product_type. inside that pivot table, I added one more column for description. so in product listing page I need to display description from that pivot table.
My code look like this:
Product::with('user)->with('product_type')->get();
To get extra fields from pivot table you need to use withPivot in function of your model Like this:
public function product_type() {
return $this->belongsToMany('App\Product','product_type','product_id','user_id')->withPivot('column1', 'column2');
}
you may also refer laravel docs for it:
https://laravel.com/docs/5.7/eloquent-relationships

add field to foregin key table and query with eloquent

I've 2 Models, let's say: Cart and Product.
Cart has many Product. Product belongs to Cart
3 tables cart, product, cart_product (id, cart_id, product_id)
I insert another field to cart_product, ex: (id, cart_id, product_id, quantity).
When I attach a Product to a Cart how can I query the relation to insert a quantity?
self answer:
$cart->products()->attach($p, array('blablabla', 'blablabla'));

Resources