我正试图根据子对象上引用的ID从数据库中获取特定的项。
我有一个大喊答题表,每个大喊答题都由多个标签组成。到目前为止我有这样的想法:
public function shoutoutSpecific($hashtag) {
$hashtags = Hashtag::whereHashtag($hashtag)->get();
$shoutouts = '';
return view('shoutout', compact('shoutouts'));
}但这只给出了hashtag,其中每个hashtag都引用特定的shoutout。我需要得到大喊答题,这个标签是通过ID引用的。
但我想知道是否有办法获得呼喊,在呼喊有一个特定的标签引用他们。类似于:
Shoutouts = Shoutout::whereHashtag-referenced($hashtag)->get();发布于 2016-11-10 17:08:13
如果Shoutout类具有hasMany关系,如下所示:
Shoutout.php
public function hashtags()
{
return $this->hasMany(App\Hashtag::class);
}然后你可以试着:
Shoutouts = Shoutout::whereHas('hashtags', function($q) use($hashtag) {
$q->whereHashtag($hashtag);
})->get();https://stackoverflow.com/questions/40533220
复制相似问题