2017-04-22 87 views
0

我很困惑这种关系,我试过视频教程,但无法得到如何解决这种关系。在此先感谢如果有人能解释我。如何解决laravel关系

我试过在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 

用户具有

我使用这样的,但得到的错误通过linked_licence多牌:

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) 

但问题是我不知道laravel关系如何创建这些类型的查询。

更新时间:

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使用?

+1

1.你有一个错字在你最后的代码列,2。你得到什么错误? –

+0

已更新任何帖子 – Earon

+0

尝试将licences.id更改为仅限ID。 –

回答

0

您的问题似乎与这行代码

Licences::class, 'licences.id', 'user_id', 'licence_id'); 
看来该列没有在表中从看你的错误存在

做。

仔细检查你的拼写,它看起来像它看起来的表中调用licences_user名为lincences.id