Laravel 4多对多关系分页

时间:2014-03-21 10:16:13

标签: pagination laravel-4 relation

坚持了一秒钟,而我的解决方案没有找到任何答案,也许我找错了

我的模特

class MediaCategory extends Eloquent {

    protected $table = "md_categories";


    public function Media()
    {
        return $this->belongsToMany('Media', 'media_categories')->orderBy('id', 'DESC');
    }

}

控制器

public function getCategory($slug)
    {
        $categories = $this->category->has('media')->orderBy('name', 'ASC')->get();

        $medias = $this->category->whereSlug($slug)->get();

        // $medias = $results->media();

        $this->layout->title = 'Média tár';
        $this->layout->content = View::make('front::page/results')
                                ->with('categories', $categories)
                                ->with('medias', $medias);

    }

查看

<div class="content-left">
    @include('front::partials/search', array('categories' => $categories))
    @foreach($medias as $data)
        @foreach($data->media as $media)
            <div class="gallery-thumbs">
                <a class="view-more-image" href="{{ URL::to('media/details/'.$media->id) }}">Kép kiválasztása</a>
                    <a href="{{ URL::to($media->mediaOriginal()) }}" rel="prettyPhoto" title="{{ $media->title }}">
                        {{ HTML::image($media->mediaSmall()) }}
                    </a>
            </div>
        @endforeach
    @endforeach
    </div>

因此,当我将$medias = $this->category->whereSlug($slug)->get();更改为$medias = $this->category->whereSlug($slug)->paginate(2ö);时,我只发现了find()方法的瑕疵

可以请别人给我一个提示吗?

1 个答案:

答案 0 :(得分:3)

好吧,不够睡觉的结果会让你提出愚蠢的问题并导致编码效果不佳

工作正常,而不是

$medias = $this->category->whereSlug($slug)->get();

需要

$data = $this->category->whereSlug($slug)->first();

$medias = $data->media()->paginate(30);