向laravel 5.4添加子类别 [英] Adding subcategories to laravel 5.4
问题描述
我的应用程序中有类别表,它工作得很好,但我想知道如何也为此获得子类别?
I have categories table in my application and it works just fine but i wonder how to get subcategories for that as well?
目前我所拥有的是:
类别模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
public function ads(){
return $this->hasMany(Ad::class);
}
}
类别迁移:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug')->unique();
$table->timestamps();
});
}
我的帖子模型:
public function category(){
return $this->belongsTo(Category::class);
}
PS:我知道我必须创建新表并为子类别调用category_id
的外键,但令我感到困惑的是关系以及如何在前端选择它们,因此类别和子类别都显示在一个中下拉给用户(例如WP)?
PS: I know I have to make new table and call for foreign key of category_id
for subcategories but what makes me confuse is the relationships and also how to select them in front-end so category and subcategories all shows in one drop down to user (like WP)?
推荐答案
我真的不太了解您要执行的操作..但是,如果您要在一个下拉列表中同时添加类别和子类别,我认为这就是你想要的:
i really don't understand well what you're trying to do .. but if you're trying to add both categories and subcategories in one dropdown i assume this is what you wanted:
假设您已经在控制器中建立了关系..
let's say you already have done the relationship .. in your controller
$categories = Category::with('sub_categories')->get();
return view('page',compact('categories'));
现在我们掌握了所有类别及其对应的子类别..您只需要做的
now we got all the categories and it's corresponding sub-categories .. in your blade all you have to do is
<select name="categories">
@foreach($categories as $categ)
<optgroup label="{{ $categ->name }}">
@foreach($categ->sub_categories as $sub)
<option value="{{ $sub->id }}">{{ $sub->name }}</option>
@endforeach
</optgroup>
@endforeach
</select>
这将为您提供类似的输出
and that would give you an output like
<select>
<optgroup label="Fruits">
<option value="1">Apple</option>
<option value="2">Banana</option>
</optgroup>
<optgroup label="Vegetables">
<option value="3">Beans</option>
<option value="4">Cabbage</option>
</optgroup>
</select>
编辑
关系很容易理解英语..实际上您已经在类别广告中做到了..
relationship is easy as understanding english .. actually you've done it already in your category ads ..
类别模型
public function ads()
{
return $this->hasMany('App\Ad');
// return $this->hasMany('App\Ad','theforeignfieldtocomparetomyprimarykey')
}
hasMany
-这意味着广告模型在数据库表中的字段中有category_id
作为字段..
hasMany
- this means Ad model has category_id
in it's database table as field ..
在您的广告模型
public function category()
{
return $this->belongsTo('App\Category');
// return $this->belongsTo('App\Category','myfieldtocompare');
}
belongsTo
-由于函数名称为category
,它将在其字段中查找category_id
,并将其与model category
的primary key
belongsTo
- since the function name is category
it will look up on his fields for category_id
and match it to the primary key
of model category
因此,基本上,您要做的是首先制作具有如下迁移功能的模型:
so basically what you have to do is first make models with migrations like:
类别
- id
- 名称
子类别
- id
- category_id
- 名称
然后在您的模型中,您应该具有上面示例的脚本..
then in your model you should have a script as exampled above ..
这篇关于向laravel 5.4添加子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!