简体   繁体   中英

How to define relationship with pivot table laravel

I have two table one is pivot table and other is normal table that is

user_pharmacies

user_pharmacies_id  | user_id | pharmacy_id

user_price

price_id | product_id | user_pharmacies_id | price

you can see primary key of pivot table is foreign key in user_price table. My question is how to define the relation between these two table i don't want to make model of pivot table

Your table structure is entirely wrong. Please read the laravel documentation first on how to properly setup relationships by using a pivot table.

Which one is your pivot table? I do not think any of above is pivot table. For the above tables you have to use two models, but if you have any real pivot table then you do not need to create model for that pivot table in laravel. You can use eloquent relationship to fetch the data from pivot table.

For example, you have following three tables : users , roles and user_roles then you only need to create models for users and roles table. You can use eloquent relationship to fetch data from pivot table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM