类别和子类别的父id



我使用L8我有一个category表,它有parent_id子类别

categories table

Category model

categoryController

SubCategoryController

categories.blade

sub_categories.blade

subcategory-index.blade.php中,我想显示类别但我只能显示它们的id (parent 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>

谢谢你告诉我该怎么做:>

获取子类别

$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);

下面这行是不正确的。因为with()是用来获取关系数据的,而parent_id不是关系名称。

$parent_id = Category::with('parent_id')->get();

如果你的路由包含类别的id或段号,你可以使用它,但我认为它不应该,因为你的index函数不接受任何路由参数。我假设你要获取所有类别和子类别。但是在这种情况下,索引函数的第二行根本没有意义。

如果你想要所有的类别:

$categories = Category::where('parent_id', null)->with('children')->get();

我看到您使用123作为顶级类别,它看起来足够高。但是对于这个目的,nullable是一个更好的实践。

如果您需要一个特定的类别及其子类别:

// web.php
Route::get('category/{slug}', [CategoryController::class, 'index']);
// CategoryConteroller.php
public function index($slug)
{
$category = Category::where('slug', $slug)->with('children')->get();
}

最新更新