简体   繁体   中英

Eager Loading is not working laravel 5.3

I have three 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 i have the followibg columns

  1. id
  2. user_id
  3. product_id

I want to get the product info of an users wishlist. I have tried this

public function showWishList(){
        $id= Auth::id();
        $WishList = wishlist::with('product')->where(['user_id'=>$id])->get();
        return json_encode($WishList);
    }

But this gives me the following error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.users_wishlst_id' in 'where clause' (SQL: select * from products where products . users_wishlst_id in (1, 2, 3)) what is the problem

Without knowing all about your database structure this seems like a problem with your foreign keys. Eloquent tries to automatically guess the key. Based on the error it seems like your products table doesn't contain a users_wishlst_id column (maybe you named it different?). Try looking at you Database and give Laravel the correct foreign_key.

https://laravel.com/docs/5.3/eloquent-relationships#one-to-many

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