php - 急切加载不适用于 laravel 5.3

标签 php mysql laravel-5.3 one-to-many eager-loading

我有三个相关模型。 1.用户模型

public function users_wishlst(){
        return $this->hasMany('App\Users_wishlst');
    }

2.产品型号

public function users_wishlst(){
        return $this->belongsTo('App\Users_wishlst');
    }

3.Users_wishlst模型

public function user(){
        return $this->belongsTo('App\User');
    }

    public function product(){
        return $this->hasMany('App\Product');
    }

在 users_wishlsts 表中我有 followibg 列

  1. 编号
  2. 用户编号
  3. 产品编号

我想获取用户心愿单的产品信息。这个我试过了

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

但这给了我以下错误

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

最佳答案

在不完全了解您的数据库结构的情况下,这似乎是您的外键问题。 Eloquent 尝试自动猜测 key 。根据错误,您的产品表似乎不包含 users_wishlst_id 列(也许您将其命名为不同?)。尝试查看您的数据库并为 Laravel 提供正确的 foreign_key。

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

关于php - 急切加载不适用于 laravel 5.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43411090/

相关文章:

php - 发送收到的订单 woocommerce 给经销商发送电子邮件通知

php - 如何在 HTTP 请求和 cli 类对象之间进行对话

mysql - 不会写;模式更新时表中的重复键

php - 公共(public)路径列出 View 中的所有文件

javascript - 将 JavaScript 显示为文件中的可编辑代码

PHPExcel 无法打开保存的文件

mysql - 与数据库在线聊天 - 最快的方式?

mysql - Phusion 乘客 - ActiveRecord::PendingMigrationError

php - 使用 Html 表单 Laravel 更新数据库

laravel - 使用laravel 5.3通行证从Android应用程序登录API