parent 类别和子类别的 ID

parent id for category and subcategory

我使用 L8 我有一个 category table ,我的子类别有 parent_id

categories table

Category model

categoryController

SubCategoryController

categories.blade

sub_categories.blade

在我的 subcategory-index.blade.php 中,我想显示类别,但我只能显示它们的 ID (parent id)

我不知道如何显示类别标题而不是它们的 ID。

我对类别 table 进行了此迁移:

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,你可以使用它,但我认为它不能,因为你的索引函数不接受任何路由参数。所以我假设您正在尝试获取所有类别和子类别。但是在这种情况下,index函数的第二行就完全没有意义了

如果要所有类别:

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