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的公司
- 结果也可以在刀片服务器中分页李>
$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);