【发布时间】: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->string('categories')->nullable();列是外键吗?公司和类别表的关系是什么? -
它有额外的数据透视表。填写表格时必须添加类别。我也加一下
-
公共函数 categories() { return $this->belongsToMany(Category::class, 'category_company')->withTrashed();
-
公共函数公司() { return $this->belongsToMany(Company::class, 'category_company')->withTrashed(); }
标签: php laravel pagination