问题描述
我是 Laravel 初学者,使用 L8。
我有一个 category
表,它有 parent_id
用于我的子类别。
我使用一个表和模型来 CRUD 它们,但我的控制器和视图是分开的。
意思是:
categories table
Category model
categoryController
SubCategoryController
categories.blade
sub_categories.blade
在我的 subcategory-index.blade.PHP
中,我想显示类别,但我只能显示它们的 ID(父 ID)
我有这个类别表的迁移:
public function up()
{
Schema::dropIfExists('categories');
Schema::create('categories',function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('parent_id')->default(123);
$table->string('title');
$table->longText('description');
$table->tinyinteger('status')->comment('status is 1 when a category is active and it is 0 otherwise.')->nullable();
$table->rememberToken();
$table->softDeletes();
$table->timestamps();
});
}
这是我的类别模型:
class Category extends Model
{
use HasFactory;
protected $fillable = [
'parent_id','title','description','status',];
public function children(){
return $this->hasMany(Category::class,'parent_id');
}
public function post(){
return $this->hasMany(Post::class);
}
}
和我的子类别控制器:
...
public function index()
{
$parent_id = Category::with('parent_id')->get();
$subcategories = Category::where('parent_id','!=',123)->get();
return view('admin.subcategories.subcategories-index',compact('subcategories'));
}
...
以及在 category-index.blade.PHP 中显示子类别标题的部分:
<table class="table table-bordered">
<tr>
<th>#</th>
<th>id</th>
<th>title</th>
<th>category</th>
<th>status</th>
<th>operation</th>
</tr>
@foreach($subcategories as $subcategory )
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $subcategory['id'] }}</td>
<td>{{ $subcategory['title'] }}</td>
<td>{{ $subcategory['parent_id']}}</td>
<td>
@if($subcategory['status']==0 or $subcategory['status']==NULL)
inactive
@else
active
@endif
</td>
<td>
<form method="POST" action="{{ route('subcategory.destroy',$subcategory->id) }}">
<a class="btn btn-info" href="{{ route('subcategory.show',$subcategory->id) }}">show</a>
<a class="btn btn-primary" href="{{ route('subcategory.edit',$subcategory->id) }}">edit</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger"> delete</button>
</form>
</td>
</tr>
@endforeach
</table>
谢谢你告诉我该怎么做:>
解决方法
下面的行不正确。因为with()是用来获取关系数据的,而parent_id不是关系名。
$parent_id = Category::with('parent_id')->get();
如果你的路由包含类别的 id 或 slug,你可以使用它,但我认为它没有,因为你的索引函数不接受任何路由参数。所以我假设您正在尝试获取所有类别和子类别。但在这种情况下,索引函数的第二行完全没有意义。
如果您想要所有类别:
$categories = Category::where('parent_id',null)->with('children')->get();
我看到你使用 123 作为顶级类别,它看起来足够高。但为此目的,可空是更好的做法。
如果您需要特定类别及其子类别:
// web.php
Route::get('category/{slug}',[CategoryController::class,'index']);
// CategoryConteroller.php
public function index($slug)
{
$category = Category::where('slug',$slug)->with('children')->get();
}
,
获取子类别
$sub_categories = Category::whereNotNull('parent_id')->get();
通过父类获取子类
$sub_categories_with_parent = Category::with('parent')->whereNotNull('parent_id')->get();
获取类别
$categories = Category::whereNull('parent_id')->get();
获取带有孩子的类别
$categories_with_childern = Category::with('children')->whereNull('parent_id')->get();
您可能还需要重新定义您的关系:
public function parent()
{
return $this->belongsTo(Category::class);
}
public function children()
{
return $this->hasMany(Category::class,'parent_id');
}
在迁移中也定义关系
$table->foreign('parent_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
使父字段可以为空
$table->unsignedBigInteger('parent_id')->nullable()->default(123);