无法在视图中的 for 循环中使用 hasMany 关系 (Laravel)
Cannot use hasMany relationship on for loop in view (Laravel)
我正在尝试编写一个应用程序,其中包含多个 "Locations" 用户可以将其添加为收藏夹。我有一个位置模型、一个收藏夹模型和一个数据库 table,我在其中记录 user_id 和 location_id 对以指示用户最喜欢的位置。
在我的 Location 模型中,我定义了以下 hasMany 关系:
public function favorites(){
return $this->hasMany(Favorite::class);
}
我也在Favorite模型上定义了反比关系如下:
public function location(){
return $this->belongsTo(Location::class);
}
然后在一个视图中,我试图获取所有位置的列表并标记用户已标记为收藏的位置。为此,我遍历了所有位置,并试图计算该位置有多少个收藏夹。它应该是 1 或 0,因为 user_id/location_id 组合行仅在用户将特定位置作为最喜欢的位置时才存在。
这是代码的相关部分(我将位置列表传递给视图):
@foreach ($Locations_List as $Location)
{{ $Location->favorites()->get()->count }}
@endforeach
我是这样传递 $Locations_List:
public function Create_SearchResults(){
$Locations_List = Location::select('id', 'Name', 'Category', 'Logo', 'Status', 'Address_Number', 'Address_Street', 'Address_City', 'Updated_At')
->where('Status', '>=', 500)
->get();
return view('Location_Search/Index', compact('Locations_List'));
}
但是,当我尝试上述操作时,我得到 "Property [count] does not exist on this collection instance."。
我试过在 tinker 中做类似上面的事情,它确实有效。但是,我必须按如下方式解决 "array" 部分:
$loc = App\Location::find(2);
$loc->favorites()->get()->count();
如何解决视图中的 "array" 问题?提前致谢
在 foreach
循环中尝试 {{ $Location->favorites->count() }}
。
或者,您可以通过添加
预先加载控制器中的计数
->withCount('favorites')
初始化时$Locations_List
:
$Locations_List = Location::select('id', 'Name', 'Category', 'Logo', 'Status', 'Address_Number', 'Address_Street', 'Address_City', 'Updated_At')
->withCount('favorites')
->where('Status', '>=', 500)
->get();
在这种情况下,您可以简单地将 $Location->favorites_count
放入 foreach
循环中。
我正在尝试编写一个应用程序,其中包含多个 "Locations" 用户可以将其添加为收藏夹。我有一个位置模型、一个收藏夹模型和一个数据库 table,我在其中记录 user_id 和 location_id 对以指示用户最喜欢的位置。
在我的 Location 模型中,我定义了以下 hasMany 关系:
public function favorites(){
return $this->hasMany(Favorite::class);
}
我也在Favorite模型上定义了反比关系如下:
public function location(){
return $this->belongsTo(Location::class);
}
然后在一个视图中,我试图获取所有位置的列表并标记用户已标记为收藏的位置。为此,我遍历了所有位置,并试图计算该位置有多少个收藏夹。它应该是 1 或 0,因为 user_id/location_id 组合行仅在用户将特定位置作为最喜欢的位置时才存在。
这是代码的相关部分(我将位置列表传递给视图):
@foreach ($Locations_List as $Location)
{{ $Location->favorites()->get()->count }}
@endforeach
我是这样传递 $Locations_List:
public function Create_SearchResults(){
$Locations_List = Location::select('id', 'Name', 'Category', 'Logo', 'Status', 'Address_Number', 'Address_Street', 'Address_City', 'Updated_At')
->where('Status', '>=', 500)
->get();
return view('Location_Search/Index', compact('Locations_List'));
}
但是,当我尝试上述操作时,我得到 "Property [count] does not exist on this collection instance."。
我试过在 tinker 中做类似上面的事情,它确实有效。但是,我必须按如下方式解决 "array" 部分:
$loc = App\Location::find(2);
$loc->favorites()->get()->count();
如何解决视图中的 "array" 问题?提前致谢
在 foreach
循环中尝试 {{ $Location->favorites->count() }}
。
或者,您可以通过添加
预先加载控制器中的计数->withCount('favorites')
初始化时$Locations_List
:
$Locations_List = Location::select('id', 'Name', 'Category', 'Logo', 'Status', 'Address_Number', 'Address_Street', 'Address_City', 'Updated_At')
->withCount('favorites')
->where('Status', '>=', 500)
->get();
在这种情况下,您可以简单地将 $Location->favorites_count
放入 foreach
循环中。