Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 如何解决拉威尔关系_Laravel - Fatal编程技术网

Laravel 如何解决拉威尔关系

Laravel 如何解决拉威尔关系,laravel,Laravel,我对这种关系非常困惑,我尝试了视频教程,但无法找到解决这种关系的方法。如果有人能给我解释,请提前感谢 我在mysql中试过,效果很好,但我正在学习laravel,我想这样做 User --------- id email name address1 address2 country Product ------- id internal_name display_name Licence ------ id product_id key Linked_licence -------- li

我对这种关系非常困惑,我尝试了视频教程,但无法找到解决这种关系的方法。如果有人能给我解释,请提前感谢

我在mysql中试过,效果很好,但我正在学习laravel,我想这样做

User
---------
id
email
name
address1
address2
country

Product
-------
id
internal_name
display_name

Licence
------
id
product_id
key

Linked_licence
--------
licence_id
user_id
用户通过链接的许可证拥有多个许可证

我这样使用,但出现错误:

public function licences()
    {
        return $this->hasManyThrough(Licence::class, UserLicence::class, 'licences.id', 'user_id', 'licence_id');
    }
用户通过许可证拥有许多产品

public function licences()
        {
            return $this->hasManyThrough(Product::class, Licences::class, 'licences.id', 'user_id', 'licence_id');
        }
产品属于许可证

public function licences()
        {
            return $this->belogsTo(Product::class);
        }
这是一个错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'licences_user.licences.id' in 'field list' (SQL: select `licences`.*, `licences_user`.`licences`.`id` from `licences` inner join `licences_user` on `licenscs_user`.`id` = `licences`.`user_id` where `licences_user`.`licences`.`id` is null)
但问题是我不知道如何创建这种查询

更新:

SELECT
  `users`.*,
  `licences`.*,
  `licenses_user`.`license_id`
FROM `licences`
  INNER JOIN `licenses_user` ON `licenses_user`.`license_id` = `licences`.`id`
  INNER JOIN users ON licenses_user.user_id = users.id
WHERE `licenses_user`.`license_id` = 1

上面的查询工作正常,但如何使用laravel?

您的问题似乎与这行代码有关

Licences::class, 'licences.id', 'user_id', 'licence_id');
通过查看您的错误,表中似乎不存在该列


仔细检查拼写,它看起来像是在名为
licenses\u user
的表中查找名为
lincences.id

1的列。你上一个代码有一个输入错误,2。您遇到了什么错误?在尝试后将licenses.id更改为id only。实际上,我尝试过,但出现了相同的错误,关系查询中出现了问题。您使用的是哪个版本的laravel?