1

我使用 L8 我有一张category桌子,它有parent_id我的子类别

categories table

Category model

categoryController

SubCategoryController

categories.blade

sub_categories.blade

在我的subcategory-index.blade.php我想显示类别,但我只能用他们的 id(父 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>

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

4

2 回答 2

1

下面的行是不正确的。因为 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();
}
于 2021-03-12T18:41:21.477 回答
1

获取子类别

$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);
于 2021-03-12T19:09:02.140 回答