【问题标题】:How to make pagination by $category->companies in Laravel如何在 Laravel 中按 $category->companies 进行分页
【发布时间】:2017-12-14 19:15:37
【问题描述】:

我列出了我的类别公司,我想按它们分页。我该怎么做?

这是我的看法:

@foreach ($category->companies as $singleCompany)
<div class="col-sm-12 col-lg-4 col-md-6">

    <!-- product card -->

    <div class="product-item bg-light">
        <div class="card">
            <div class="card-body">
                <h4 class="card-title"><a href="{{ url('/company/' . $singleCompany->id) }}">{{$singleCompany->name}}</a></h4>
                @foreach ($singleCompany->categories as $singleCategories)
                    <ul class="list-inline product-meta">
                        <li class="list-inline-item">
                            <a href=""><i class="fa fa-folder-open-o"></i>{{ $singleCategories->name }}</a>
                        </li>
                    </ul>
                @endforeach
            </div>
        </div>
    </div>
</div>
@endforeach

我的控制器如下所示:

    $companies = Company::filterByRequest($request)->paginate(9);
    $category = Category::find($id);

我的类别架构:

        if(! Schema::hasTable('categories')) {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->nullable();
            $table->string('icon')->nullable();

            $table->timestamps();
            $table->softDeletes();

            $table->index(['deleted_at']);
        });

我的公司架构:

        if(! Schema::hasTable('companies')) {
        Schema::create('companies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->nullable();
            $table->string('city_id')->nullable();
            $table->string('categories')->nullable();
            $table->string('address')->nullable();
            $table->text('description')->nullable();
            $table->string('logo')->nullable();

            $table->timestamps();
            $table->softDeletes();

            $table->index(['deleted_at']);
        });
    }

company_category 的数据透视表

if(! Schema::hasTable('category_company')) {
        Schema::create('category_company', function (Blueprint $table) {
            $table->integer('category_id')->unsigned()->nullable();
            $table->foreign('category_id', 'fk_p_91029_91033_company__5a12afe2d2772')->references('id')->on('categories')->onDelete('cascade');
            $table->integer('company_id')->unsigned()->nullable();
            $table->foreign('company_id', 'fk_p_91033_91029_category_5a12afe2d27f0')->references('id')->on('companies')->onDelete('cascade');

        });

我可以以某种方式使用 $category->companies 对我的公司进行分页吗?谢谢你的帮助。

【问题讨论】:

  • 我想澄清一下。您想在所选类别下对公司进行分页,对吗?如果是这样,您可以编辑您的问题并放置表公司和类别的表模式。谢谢
  • $table-&gt;string('categories')-&gt;nullable(); 列是外键吗?公司和类别表的关系是什么?
  • 它有额外的数据透视表。填写表格时必须添加类别。我也加一下
  • 公共函数 categories() { return $this->belongsToMany(Category::class, 'category_company')->withTrashed();
  • 公共函数公司() { return $this->belongsToMany(Company::class, 'category_company')->withTrashed(); }

标签: php laravel pagination


【解决方案1】:

抱歉,我不知道在这种情况下$category-&gt;companies 是否可行。

你可能想试试这个代码。

$companies = Company::leftJoin('category_company', 'companies.id', 
                                '=', 'category_company.company_id')
                        ->where('category_id', $id)
                        ->paginate(9);
  • 此代码将左连接表公司和类别公司。
  • 它还将能够获取具有所选类别 ID 的公司。
  • 结果也可以在刀片中进行分页了。

【讨论】:

  • 是的,你是对的!只需将对象从公司修复到公司
  • 一个问题,例如显示 10 个对象。但它应该只显示 9
  • 对象$companies中的数据结果将基于编号。分页。
  • 如果它解决了您的问题,您可以将您的问题标记为已回答。谢谢!
  • 我会的,但首先我要确保它真的正确
【解决方案2】:

如果你想对分类进行分页,你必须使用 paginate() 分类。

尝试通过此更改您的获取类别查询

$category = Category::where('unique_id', $id)->paginate(9);

然后你可以在需要分页的地方使用{{ $category-&gt;render() }}

【讨论】:

  • 你需要在哪一栏中找到那个$id?
【解决方案3】:

$category-&gt;companies 将返回包含所有公司的 Eloquent 集合,而不是 LengthAwarePaginator 的实例。

$companies 将返回LengthAwarePaginator 的一个实例,因此对其进行迭代。

在 cmets 中,您曾说过要加载指定类别的公司。如果您已经定义了belongsToMany() 关系,请使用whereHas() 按类别过滤:

$companies = Company::filterByRequest($request)
    ->whereHas('categories', function($q) use($categoryId) {
        $q->where('id', $categoryId);
    })
    ->paginate(9);

然后显示公司:

@foreach ($companies as $singleCompany)
    {{ $singleCompany->name }}
@endforeach

并添加分页链接:

{{ $companies->render() }}

https://laravel.com/docs/5.5/pagination

【讨论】:

  • 但我需要选定类别公司的渲染。这就是为什么我有 $category = Category::find($id);选择类别后,我需要显示选定的类别公司。这就是为什么它是 $category->companies as $singleCompany
猜你喜欢
  • 2014-05-08
  • 2021-09-27
  • 2018-09-04
  • 2020-03-07
  • 2013-08-24
  • 2019-10-02
  • 2014-01-13
  • 1970-01-01
  • 2021-06-14
相关资源
最近更新 更多