相关疑难解决方法(0)

Laravel命令通过关系

我正在查看特定帖子的作者发布的所有评论.

foreach($post->user->comments as $comment)
    {
        echo "<li>" . $comment->title . " (" . $comment->post->id . ")</li>";
    }
Run Code Online (Sandbox Code Playgroud)

这给了我

I love this post (3)
This is a comment (5)
This is the second Comment (3)
Run Code Online (Sandbox Code Playgroud)

我如何通过post_id订购,以便上面的列表被命令为3,3,5

laravel laravel-4

96
推荐指数
4
解决办法
11万
查看次数

如何按Eloquent ORM中多对多关系的数据透视表的字段进行排序

我一直在使用Eloquent ORM已经有一段时间了,我知道它非常好,但我不能做到以下几点,而在Fluent中很容易做到.

我的用户有多对多的歌曲,中间表是song_user(就像它应该的那样).根据播放次数判断,我想获得用户的热门歌曲.当然,播放计数存储在中间表中.

我可以用Fluent做到:

$songs = DB::table('songs')
    ->join('song_user', 'songs.id', '=', 'song_user.song_id')
    ->where('song_user.user_id', '=', $user->id)
    ->orderBy("song_user.play_count", "desc")
    ->get();
Run Code Online (Sandbox Code Playgroud)

简单.但我想在Eloquent中做到这一点,当然这不起作用:

$songs = Song::
    with(array("song_user" => function($query) use ($user) {
        $query->where("user_id", "=", $user->id)->orderBy("play_count", "desc");
    }))
Run Code Online (Sandbox Code Playgroud)

laravel eloquent

30
推荐指数
2
解决办法
2万
查看次数

标签 统计

laravel ×2

eloquent ×1

laravel-4 ×1