gpt4 book ai didi

mysql - 在目标表具有一对一的情况下使用 hasManyThrough

转载 作者:行者123 更新时间:2023-11-29 09:36:46 25 4
gpt4 key购买 nike

如果我们取 Laravels examplehasManyThrough 上并将其更改如下:

countries
id - integer
name - string

users
id - integer
country_id - integer
website_id - integer
name - string

websites
id - integer
title - string

users作为中间关系,我可以在country模型上使用hasManyThrough()来获取websites

我尝试了这个,结果出现错误

namespace App;

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
/**
* Get all of the websites for the country.
*/
public function websites()
{
return $this->hasManyThrough('App\Website', 'App\User');
}
}

Illuminate\Database\QueryException:SQLSTATE[42S22]:未找到列:1054 未知列

也许用户必须拥有许多网站才能实现此功能?

最佳答案

您可以根据 @Samir 的评论更改表结构以匹配 Laravel 的期望,也可以自定义键以匹配现有的表结构。

https://laravel.com/docs/5.8/eloquent-relationships

Typical Eloquent foreign key conventions will be used when performing the relationship's queries. If you would like to customize the keys of the relationship, you may pass them as the third and fourth arguments to the hasManyThrough method. The third argument is the name of the foreign key on the intermediate model. The fourth argument is the name of the foreign key on the final model. The fifth argument is the local key, while the sixth argument is the local key of the intermediate model:

public function websites()
{
return $this->hasManyThrough('App\Website', 'App\User','country_id','id','website_id','website_id');
}

关于mysql - 在目标表具有一对一的情况下使用 hasManyThrough,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57448253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com