我想了解laravel雄辩的关系,所以我创建了两个表。在laravel迁移定义的表的结构是 为mostpopular表用laravel雄辩的关系选择数据
Schema::create('mostpopulars',function(Blueprint $table){
$table->increments('id');
$table->integer('song_id')->unsigned();
$table->index('song_id');
$table->foreign('song_id')->references('id')->on('song_details')->delete('cascade');
});
为song_detail表
Schema::create('song_details', function (Blueprint $table) {
$table->increments('id');
$table->string('song_title');
$table->string('song_name');
$table->string('song_album');
$table->string('song_singer');
$table->string('song_musicby');
$table->string('song_location');
$table->string('song_album_image');
$table->string('song_language');
$table->string('song_lyrics',3000);
$table->timestamps();
});
然后在Mostpopular模型定义的函数对与2表
public function song_detail()
{
return $this->hasOne('App\Song_detail','id');
}
和控制器索引功能我想要做这样的事情
$songs = Song_detail::select()->latest()->get();
$malayalamSongs = Mostpopular::select('song_id')->groupBy('song_id')->havingRaw('COUNT(*) > 2')->get();
$mp = $malayalamSongs;
dd($mp->song_detail);
,但得到的错误
Undefined property: Illuminate\Database\Eloquent\Collection::$Song_detail
请帮我找出错误和我所试图做的是从song_details表,其中song_id
发生两次以上song_id
获得歌曲的细节是mostpopular表。
您song_detail模型需要需要通过belongsTo –
确定我加入公共职能songsdetail( ) { \t return $ this-> belongTo('App \ Mostpoular'); } –
但我得到相同的错误 –