Laravel where 子句中的 ManyToMany null id
Laravel ManyToMany null id in where clause
我遇到了 this 同样的问题。完全相同的问题接受不同的 class 和 table 名称。有没有人有一些见解?
Site:
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function categoryBlocks()
{
return $this->belongsToMany(Category::class, 'blocked_category');
}
Category:
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function sites()
{
return $this->belongsToMany(Site::class, 'blocked_category');
}
从类别到站点而不是从站点到类别查询时,查询正确生成。 (我试过重命名站点模型上的方法,但它根本没有帮助。)
$catBlocks = $category->sites()->get(); // Query creates a category_id value in the query
$blocks = $site->categoryBlocks()->get(); // Query doesn't create a site_id value in the query
Table: blocked_category
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`site_id` int(10) unsigned NOT NULL,
`category_id` int(10) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
Table: sites
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`publisher_id` int(10) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`status_id` int(11) NOT NULL DEFAULT '1',
Table: categories
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
尝试将第三个参数传递给 'category_id' 的 categoryBlocks 关系:
public function categoryBlocks()
{
return $this->belongsToMany(Category::class, 'blocked_category', 'category_id');
}
或者您在 blocked_category table 中对 category_id 的称呼。如果这不起作用,请回复您的数据库架构。
所以我发现了问题。用于查询的对象实例未完全水化,这导致查询构建器无法正确查看关系。
我正在获取传递给方法的对象实例,但由于某种原因它不是完全水化的实例,所以我只需要在 运行 查询之前手动水化 Site 对象。
我遇到了 this 同样的问题。完全相同的问题接受不同的 class 和 table 名称。有没有人有一些见解?
Site:
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function categoryBlocks()
{
return $this->belongsToMany(Category::class, 'blocked_category');
}
Category:
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function sites()
{
return $this->belongsToMany(Site::class, 'blocked_category');
}
从类别到站点而不是从站点到类别查询时,查询正确生成。 (我试过重命名站点模型上的方法,但它根本没有帮助。)
$catBlocks = $category->sites()->get(); // Query creates a category_id value in the query
$blocks = $site->categoryBlocks()->get(); // Query doesn't create a site_id value in the query
Table: blocked_category
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`site_id` int(10) unsigned NOT NULL,
`category_id` int(10) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
Table: sites
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`publisher_id` int(10) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`status_id` int(11) NOT NULL DEFAULT '1',
Table: categories
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
尝试将第三个参数传递给 'category_id' 的 categoryBlocks 关系:
public function categoryBlocks()
{
return $this->belongsToMany(Category::class, 'blocked_category', 'category_id');
}
或者您在 blocked_category table 中对 category_id 的称呼。如果这不起作用,请回复您的数据库架构。
所以我发现了问题。用于查询的对象实例未完全水化,这导致查询构建器无法正确查看关系。
我正在获取传递给方法的对象实例,但由于某种原因它不是完全水化的实例,所以我只需要在 运行 查询之前手动水化 Site 对象。