Laravel 4加载数据库中播放列表中的所有歌曲

时间:2015-01-19 22:21:38

标签: php laravel

嘿,伙计们喜欢在标题中解释我在Laravel遇到了一些麻烦, 我正在创建一个系统,用户可以拥有各种播放列表(最多5个),并且在每个播放列表中都可以拥有大量歌曲(最多50个)。我试图将所有这些结合到此并尝试按如下方式打印出来:

我有以下播放列表类:

<!-- language: lang-php -->

    <?php
    class Playlist extends Eloquent {
        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'playlists';
        public $timestamps = false;
        /**
         * Whitelisted model properties for mass assignment.
         *
         * @var array
         */
        protected $primaryKey='playlistid';

        protected $fillable = array('playlistname', 'userid');

        public function songs()
        {
            return $this->hasMany('Song', 'playlistid');
        }
    }




And the following class for a song:

&#13;
&#13;
       <?php
        class Song extends Eloquent {
            /**
             * The database table used by the model.
             *
             * @var string
             */
            protected $primaryKey='songid';
            protected $foreignKey='playlistid';
            protected $table = 'songs';
            public $timestamps = false;
            protected $fillable = array('songname', 'songurl', 'playlistid', 'position');
    
            public function playlistid()
            {
    return $this->belongsTo('Playlist', 'playlistid');
            }
    public function fromList($id)
    {
    return $this->where('playlistid', $id);
            }
        }
&#13;
&#13;
&#13;

然后我想在我的主页上调用它,如:

&#13;
&#13;
        @foreach (Song::find(1)->fromList(1) as $song)
        echo $song->songname
        @endif
&#13;
&#13;
&#13;

但它似乎不起作用,有没有人知道更好的方法来做这个或修复?

1 个答案:

答案 0 :(得分:0)

不确定这是否是您想要的,但这里是如何打印一个播放列表的所有歌曲的:

@foreach(Playlist::find(1)->songs as $song)
    {{ $song->songname }}
@endforeach