【问题标题】:Laravel hasMany returns an empty array or error [duplicate]Laravel hasMany 返回一个空数组或错误[重复]
【发布时间】:2020-06-01 16:33:04
【问题描述】:

我看到了大量相关问题,但似乎无法弄清楚发生了什么。 我正在使用 Laravel 7.x,我有一个 Categories 和一个 Products 表。

分类:

CREATE TABLE categories (
    id int(11) unsigned NOT NULL AUTO_INCREMENT,
    name varchar(255) DEFAULT '',
    PRIMARY KEY (`id`)
)

产品:

CREATE TABLE products (
    id int(11) unsigned NOT NULL AUTO_INCREMENT,
    name varchar(255) DEFAULT '',
    category_id int(11) unsigned DEFAULT NULL,
    PRIMARY KEY (id),
    KEY category_id (category_id),
    CONSTRAINT products_ibfk_1 FOREIGN KEY (category_id) REFERENCES categories (id) ON DELETE CASCADE ON UPDATE CASCADE
)

在 Laravel 中,我可以使用 Product::first()->catgory 然后获取相关类别,而 hasMany 关系似乎是梨形的。

这样定义类别:

public function products()
{
    return $this->hasMany(Product::class);
}

给出错误: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categories.category_id' in 'where clause' (SQL: select * from categories where categories.category_id = 1 and categories.category_id is not null)

添加外键和本地键只返回一个空数组:

Category::find(1)->products;

...

public function products()
{
    return $this->hasMany(Product::class, 'id', 'category_id');
}

...

Illuminate\Database\Eloquent\Collection {#306 ▼
   #items: []
}

我在这里缺少什么?

*编辑: 如上所述添加 FK 仍然会导致上述 SQL 错误

class Category extends Model
{
    public function products()
    {
        return $this->hasMany(Product::class, 'category_id');
    }
}

尝试过...

class Category extends Model
{
    public function products()
    {
        return $this->hasMany(Product::class, 'category_id', 'id');
    }
}

【问题讨论】:

  • 只是在您的类别模型中包含return $this->hasMany(Product::class); 给您一个错误?
  • 只为两个模型添加外键。产品和类别返回 $this->hasMany(Product::class, 'category_id');和.......返回 $this->belongsTo(Category::class, 'category_id');
  • @Digvijay 是的,它给出了上面的 sql 错误
  • @nikitag 仅使用 FK 会导致相同的 SQL 错误:return $this->hasMany(Product::class, 'category_id');
  • 我从头开始,现在一切正常 - 一定是拼写错误或数据库错误。感谢您的帮助

标签: php laravel eloquent orm


【解决方案1】:

试试这个

   public function products()
    {
        return $this->hasMany(Product::class, 'category_id','id');
    }

【讨论】:

    【解决方案2】:

    您的问题在于这种关系:

    public function products()
    {
        return $this->hasMany(Product::class, 'id', 'category_id');
    }
    

    hasMany 将多个表中关系的外键作为第二个参数... 第三个参数代表本地主键......在这种情况下,类别本地主键..默认情况下应该是'id' 当你写 category_id 作为第三个参数 laravel 寻找 类别中的列 category_id 并没有找到它...

    在类别模型中,关系应该是:

    public function products()
    {
        return $this->hasMany(Product::class, 'category_id');
    }
    

    Product 类中的相反关系应该是:

     public function category()
        {
            return $this->belongsTo(Category::class, 'category_id');
        }
    

    更多细节

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

    【讨论】:

    • 我也试过了,看看我的编辑。
    • 你试过了:return $this->hasMany(Product::class, 'id', 'category_id');不返回 $this->hasMany(Product::class, 'category_id');
    • public function products() { return $this->hasMany(Product::class, 'category_id'); }public function category() { return $this->belongsTo(Category::class, 'category_id'); } 仍然给出 sql 错误
    • 将更新后的答案与Category::find(1)->products; 一起使用仍会出现SQL 错误:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categories.category_id' in 'where clause' (SQL: select * from categories` 其中categories.category_id = 1 和categories.category_id 不为空)`
    • 请确保您设置了 protected $table = 'products'; protected $fillable = [your fillable column] ,在产品模型和类别模型中相同
    猜你喜欢
    • 2021-10-25
    • 2014-09-11
    • 2018-07-21
    • 2016-07-31
    • 2012-10-02
    • 1970-01-01
    • 2018-05-18
    • 2019-04-17
    • 2020-05-23
    相关资源
    最近更新 更多