如何在这种情况下为每个foreach分页?

时间:2018-04-26 14:05:25

标签: php laravel

我有一个UserController,其index()方法可以让用户使用“$congresses = $user->congresses()->get();”对所有会议进行组织。然后在我展示的视图中,例如draftCongresses,即具有stattus列为“D”的会议,如:

@foreach($congresses->filter(function ($item) { return $item->draftCongresses(); }) as $congress)
    <li class="list-group-item">
        <p class="font-size-xsm"><i class="fa fa-calendar" aria-hidden="true"></i>
            {{$congress->start_date->formatLocalized('%a, %b %d, %Y - %H:%M')}}</p>
        <h5 class="card-title">
            {{$congress->name}}</h5>
        <a href="{{route('congress.manage', ['id' => $congress->id])}}" class="btn btn-outline-primary font-size-sm">Panel</a>
    </li>
@endforeach

我希望对此结果进行分页,以便在draftCongresses之间导航。默认情况下,我只想展示5个国会,然后我想在其他大会之间进行分页。

我不明白如何使用laravel paginate()方法,因为会议使用“$congresses = $user->congresses()->get();”,但在视图中有不同的foreach,一个foreach显示{ {1}},其他用于显示pastCongresses,其他用于显示publishedCongresses

在这种情况下,您知道如何为每个foreach实现分页吗?因为只有一条路线可以返回用户组织到视图的所有会议,然后每个foreach在每个标签中显示必要的结果。

//用户控制器,返回由auth用户组织的所有会议到视图

draftCongresses

国会模特:

class UserController extends Controller
{
    public function index(){

        $user = Auth::user();

        $congresses = $user->congresses()->get();

        return view('users.index', compact('user','registrations', 'congresses', $congresses));
    }
}

转到此视图:

    class Congress extends Model
    {
        public function pastCongresses(): bool
    {
        return $this->end_date < now();
    }

    public function draftCongresses(): bool
    {
        return $this->status == 'D';
    }

    public function publishedCongresses(): bool
    {
        return $this->status == 'P';
    }
    }

//查看包含显示pastCongresses,draftCongresses和publishedCongresses的标签。

 Route::get('/user/profile', [
        'uses' => 'UserController@index',
        'as'   =>'user.index'
    ]);

2 个答案:

答案 0 :(得分:0)

您必须将其设置为三个单独的查询。此外,将逻辑检查放在模型方法内部有点尴尬。我认为查询范围可能更适合你。尝试像这样设置模型:

class Congress extends Model
{
    public function scopePastCongresses($query)
    {
        return $query->where('end_date', '<' now());
    }

    public function scopeDraftCongresses($query)
    {
        return $query->where('status', 'D');
    }

    public function scopePublishedCongresses($query)
    {
        return $query->where('status', 'P');
    }
}

接下来你的控制器:

class UserController extends Controller
{
    protected $defaultPageLimit = 5;

    public function index(Illuminate\Http\Request $request)
    {
        $pageLimit = $request->input('page_limit', $this->defaultPageLimit);

        $user = $request->user();
        $congresses = $user->congresses();
        $past = $congresses->past()->paginate($pageLimit);
        $draft = $congresses->draft()->paginate($pageLimit);
        $published = $congresses->published()->paginate($pageLimit);

        $congresses = compact('past', 'draft', 'published');

        return view('users.index', compact('user', 'registrations', 'congresses'));
    }
}

最后你的观点:

@foreach($congresses['draft'] as $congress)
    <li class="list-group-item">
        <p class="font-size-xsm"><i class="fa fa-calendar" aria-hidden="true"></i>
            {{$congress->start_date->formatLocalized('%a, %b %d, %Y - %H:%M')}}</p>
        <h5 class="card-title">
            {{$congress->name}}</h5>
        <a href="{{route('congress.manage', ['id' => $congress->id])}}" class="btn btn-outline-primary font-size-sm">Panel</a>
    </li>
@endforeach

如果您的意图是,只是抓住5页的块并在之后分离结果,您可以忽略查询范围的想法,并将控制器设置为这样:

class UserController extends Controller
{
    protected $defaultPageLimit = 5;

    public function index(Illuminate\Http\Request $request)
    {
        $pageLimit = $request->input('page_limit', $this->defaultPageLimit);

        $user = $request->user();
        $congresses = $user->congresses()->paginate($pageLimit);
        $past = $congresses->filter(function ($congress) {
            return ($congress->end_date < now());
        });
        //Note, this is NOT an SQL query-where.  The results of the query above come back as an Eloquent\Collection object, a child of Support\Collection, which itself has its own 'where()' method separate from Eloquent, that just acts like a filter.
        $draft = $congresses->where('status', 'D');
        $published = $congresses->where('status', 'P');

        $congresses = compact('past', 'draft', 'published');

        return view('users.index', compact('user', 'registrations', 'congresses'));
    }
}

答案 1 :(得分:0)

首先,如果您想使用分页更改,请查询
从这个:

$congresses = $user->congresses()->get();

进入这个:

$congresses = $user->congresses()->paginate(PAGE_SIZE);

阅读有关分页here的简要文档。

其次,在视图中特别应用另一个过滤器到分页结果不是一个好主意;它反对MVC设计模式,您不能将其视为最佳实践
您可以将它应用于您的控制器并将结果传递给您的视图:

$draftCongresses = $user->congresses()->where('status', 'D')->paginate(PAGE_SIZE)

现在,您可以使用$draftCongress循环中的foreach并访问$draftCongress->links()