Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何按$category进行分页->;拉维尔的公司_Php_Laravel_Pagination - Fatal编程技术网

Php 如何按$category进行分页->;拉维尔的公司

Php 如何按$category进行分页->;拉维尔的公司,php,laravel,pagination,Php,Laravel,Pagination,我列出了我的分类公司,我想按它们分类。我怎么做 我的看法是: @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">

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

我的看法是:

@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
我的类别架构:

        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']);
        });
    }
公司类别的透视表

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->companys对我的公司进行分页吗?谢谢你的帮助


$category->companys
将返回所有公司的有说服力的集合,而不是
LengthAwarePaginator
的实例

$companys
将返回
LengthAwarePaginator
的实例,因此请对其进行迭代

在评论中,您说过要加载指定类别的公司。如果已经定义了
belongToMany()
关系,请使用
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() }}

如果要对类别进行分页,则必须对类别使用paginate()

尝试通过以下方式更改get category查询

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

然后您可以在需要分页的地方使用
{{$category->render()}
对不起,我不知道在这种情况下是否可以使用
$category->companys

您可能需要尝试此代码

$companies = Company::leftJoin('category_company', 'companies.id', 
                                '=', 'category_company.company_id')
                        ->where('category_id', $id)
                        ->paginate(9);
  • 此代码将左键加入表格公司和类别公司
  • 它还将能够获得具有所选类别id的公司
  • 结果也可以在刀片服务器中分页
但我需要由选定类别的公司进行渲染。这就是为什么我有$category=category::find($id);选择类别后,我需要显示所选类别公司。这就是为什么$category->companys是$singlecompany,我想澄清一下。您想在所选类别下对公司进行分页,对吗?如果是这样,您可以编辑您的问题,并将表格公司和类别的表格模式放入。感谢列
$table->string('categories')->nullable()外键?公司与类别表的关系是什么?它有额外的透视表。填写表格时必须添加类别。我将把它添加到公共函数categories(){return$this->belongstomy(Category::class,'Category_company')->withTrashed();公共函数companys(){return$this->belongstomy(company::class,'Category_company')->withTrashed();}你需要在哪一列中找到$id?是的,你是对的!只需解决公司之间的问题例如,它显示10个对象。但它应该只显示9对象中的数据结果
$companys
将基于页码。如果它解决了你的问题,你可以将你的问题标记为已回答。谢谢!我会的,bu首先,我想确定它是否真的正确
$companies = Company::leftJoin('category_company', 'companies.id', 
                                '=', 'category_company.company_id')
                        ->where('category_id', $id)
                        ->paginate(9);