php - Eager Loading is not working laravel 5.3 -
i have 3 related models. 1.user model
public function users_wishlst(){ return $this->hasmany('app\users_wishlst'); }
2.product model
public function users_wishlst(){ return $this->belongsto('app\users_wishlst'); }
3.users_wishlst model
public function user(){ return $this->belongsto('app\user'); } public function product(){ return $this->hasmany('app\product'); }
in users_wishlsts table have followibg columns
- id
- user_id
- product_id
i want product info of users wishlist. have tried
public function showwishlist(){ $id= auth::id(); $wishlist = wishlist::with('product')->where(['user_id'=>$id])->get(); return json_encode($wishlist); }
but gives me following error
sqlstate[42s22]: column not found: 1054 unknown column 'products.users_wishlst_id' in 'where clause' (sql: select *
products
products
.users_wishlst_id
in (1, 2, 3)) problem
without knowing database structure seems problem foreign keys. eloquent tries automatically guess key. based on error seems products table doesn't contain users_wishlst_id column (maybe named different?). try looking @ database , give laravel correct foreign_key.
https://laravel.com/docs/5.3/eloquent-relationships#one-to-many
Comments
Post a Comment