Laravel 4雄辩的查询,用于组合单个表中的数据

Laravel 4雄辩的查询,用于组合单个表中的数据,laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,我正试着制定一个电视节目表,每个频道都会播放当前和下一个即将播出的节目。 我想这样做: <table class="table"> <thead> <tr> <th></th> <th>Now Playing</th>

我正试着制定一个电视节目表,每个频道都会播放当前和下一个即将播出的节目。 我想这样做:

            <table class="table">
                <thead>
                    <tr>
                        <th></th>
                        <th>Now Playing</th>
                        <th>Next Upcoming Show</th>
                    </tr>
                </thead>
                <tbody>
                @foreach ($shows as $show)
                    <tr>
                    <td>
                        <strong>{{ $show->channel->name}}</strong></td>
                    <td><strong>{{ date('H:i', strtotime($show->now->start)) }}</strong>
                       {{ $show->now->title }}</td>
                    <td><strong>{{ date('H:i', strtotime($show->next->start)) }}</strong>
                       {{ $show->next->title }}</td>
                    </tr>
                @endforeach        
                </tbody>
            </table>

也许换个方式做会更容易些?按频道循环,然后执行
$channel->next->…
$channel->now->…

在您的
频道
模式中,您可以执行以下操作:

public function next()
{
    // Change this for your "get next" query
    return $this->hasOne('Show')->where('start', '<=', DB::raw('(NOW() - INTERVAL 15 SECOND)')) ->where('end', '>', DB::raw('(NOW() + INTERVAL 15 SECOND)')) ->orderBy('start');
}

public function now()
{
    return $this->hasOne('Show')->where('start', '<=', DB::raw('(NOW() - INTERVAL 15 SECOND)')) ->where('end', '>', DB::raw('(NOW() + INTERVAL 15 SECOND)')) ->orderBy('start');
}

@foreach($channels作为$channel)
现在:{{$channel->Now->title}}
下一步:{{$channel->Next->title} @endforeach

我根本没有测试这个,这只是一个想法/建议。

关于您的数据库结构的更多信息,甚至您试图生成的原始查询,肯定会有所帮助。哇,杰斯珀!那很有魅力!不知道你能在模特身上做到!谢谢你的帮助!
public function next()
{
    // Change this for your "get next" query
    return $this->hasOne('Show')->where('start', '<=', DB::raw('(NOW() - INTERVAL 15 SECOND)')) ->where('end', '>', DB::raw('(NOW() + INTERVAL 15 SECOND)')) ->orderBy('start');
}

public function now()
{
    return $this->hasOne('Show')->where('start', '<=', DB::raw('(NOW() - INTERVAL 15 SECOND)')) ->where('end', '>', DB::raw('(NOW() + INTERVAL 15 SECOND)')) ->orderBy('start');
}
$channels = Channel::with(array('next', 'now'))->get();
@foreach($channels as $channel)
    Now: {{ $channel->now->title }} <br>
    Next: {{ $channel->next->title }}
@endforeach