显示 Laravel 中的类别和子类别
Showing Category And Subcategory In Laravel
我正在使用 Laravel 5.2。
我有 2 个这样的 Eloquent Models-
Category.php-
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories'; //Table Name
public $timestamps = false;
public $incrementing = false; //For Non integer Primary key
protected $primaryKey = 'name';
protected $fillable = [
'name'
];
public function SubCategory()
{
return $this->hasMany('App\SubCategory', 'category_id', 'id');
}
}
和子Category.php-
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SubCategory extends Model
{
protected $table = 'sub_categories'; //Table Name
public $timestamps = false;
protected $fillable = [
'category_id',
'name'
];
}
所以,现在如果我在控制器中调用它-
return Category::with('SubCategory')->get();
我得到这样的东西-
[
{
"id": 3,
"name": "Beahan-Mueller",
"sub_category": [
{
"id": 27,
"category_id": 3,
"name": "Carroll Trail"
},
{
"id": 3,
"category_id": 3,
"name": "Davis Lake"
},
{
"id": 9,
"category_id": 3,
"name": "Lehner Ranch"
}
]
},
{
"id": 10,
"name": "Beahan, Stark and McKenzi",
"sub_category": [
{
"id": 1,
"category_id": 10,
"name": "Dibbert Summit"
},
{
"id": 18,
"category_id": 10,
"name": "Kris Mount"
}
]
}
]
所以,我可以看出 sub-category link 有效,对吧?
但我的问题是如果我想将它与 blade 一起使用来显示这样的值-
控制器-
return view('public.listing.main', [
'current_page' => 'Add Listing',
'categories' => Category::with('SubCategory')->get()
]);
查看-
@foreach ($categories as $category)
<li class="no-border">
<label class="pull-left">
<input type="checkbox" name="cat_{{ $category->id }}" checked>
<strong> {{ $category->name }} (21)</strong>
</label>
<ul>
@foreach($category->sub_category as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="{{ $sub_cat->id }}"> {{ $sub_cat->name }} (7)
</label>
</li>
@endforeach
</ul>
</li>
@endforeach
我发现了类似的错误-
谁能帮忙,为什么我会发现这个错误?
您的子类别关系名称在第二个 foreach 中是错误的。应该是
@foreach($category->subCategory as $sub_cat)
// code here
@endforeach
而不是 sub_category
。
我正在使用 Laravel 5.2。 我有 2 个这样的 Eloquent Models-
Category.php-
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories'; //Table Name
public $timestamps = false;
public $incrementing = false; //For Non integer Primary key
protected $primaryKey = 'name';
protected $fillable = [
'name'
];
public function SubCategory()
{
return $this->hasMany('App\SubCategory', 'category_id', 'id');
}
}
和子Category.php-
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SubCategory extends Model
{
protected $table = 'sub_categories'; //Table Name
public $timestamps = false;
protected $fillable = [
'category_id',
'name'
];
}
所以,现在如果我在控制器中调用它-
return Category::with('SubCategory')->get();
我得到这样的东西-
[
{
"id": 3,
"name": "Beahan-Mueller",
"sub_category": [
{
"id": 27,
"category_id": 3,
"name": "Carroll Trail"
},
{
"id": 3,
"category_id": 3,
"name": "Davis Lake"
},
{
"id": 9,
"category_id": 3,
"name": "Lehner Ranch"
}
]
},
{
"id": 10,
"name": "Beahan, Stark and McKenzi",
"sub_category": [
{
"id": 1,
"category_id": 10,
"name": "Dibbert Summit"
},
{
"id": 18,
"category_id": 10,
"name": "Kris Mount"
}
]
}
]
所以,我可以看出 sub-category link 有效,对吧?
但我的问题是如果我想将它与 blade 一起使用来显示这样的值-
控制器-
return view('public.listing.main', [
'current_page' => 'Add Listing',
'categories' => Category::with('SubCategory')->get()
]);
查看-
@foreach ($categories as $category)
<li class="no-border">
<label class="pull-left">
<input type="checkbox" name="cat_{{ $category->id }}" checked>
<strong> {{ $category->name }} (21)</strong>
</label>
<ul>
@foreach($category->sub_category as $sub_cat)
<li>
<label class="pull-left">
<input type="checkbox" checked value="{{ $sub_cat->id }}"> {{ $sub_cat->name }} (7)
</label>
</li>
@endforeach
</ul>
</li>
@endforeach
我发现了类似的错误-
谁能帮忙,为什么我会发现这个错误?
您的子类别关系名称在第二个 foreach 中是错误的。应该是
@foreach($category->subCategory as $sub_cat)
// code here
@endforeach
而不是 sub_category
。