[英]Facing problem to show properly sub-categories in the Categories Dropdown list
在类别下拉列表中显示子类别的麻烦问题
在我的代码中,我想在Products 表中的Category下显示类别和子类别。
这是我的类别表
1.
public function up() { Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('parent_id'); //sub category id
$table->string('name');
$table->rememberToken();
$table->timestamps();
});
}
这是我的产品表
2.
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id');
$table->string('product_name');
$table->timestamps();
});
}
这是我的类别模式l
3.Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model { protected $guarded=[];
public function products(){
return $this->hasMany('App\Product');
}
public function parent(){
return $this->belongsTo('App\Category','parent_id','id');
}
}
这是我的产品 Model
4.产品.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function category(){
return $this->hasone('App\Category');
}
}
现在这是我的ProductsController.php
5.ProductsController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use App\Product;
use Session;
use Illuminate\Http\Request;
class ProductsController extends Controller
{
public function product(){
return view('admin.products.product');
}
}
这是我的product.blade.php文件
<form class="form-horizontal" method="post" action="{{route('add.product')}}" name="add_product" id="add_product" novalidate="novalidate">
@csrf
<div class="control-group">
<label class="control-label">Under Category </label>
<div class="controls">
<select name="category_id" id="category_id" style="width:220px;" >
<option value='' selected disabled>Select</option>
@foreach(App\Category::all() as $cat)
<option value="{{$cat->id}}" >
{{ $cat->parent ? $cat->parent->name .' -- ' : '' }}
{{$cat->name}}
</option>
@endforeach
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">Product Name</label>
<div class="controls">
<input type="text" name="product_name" id="product_name">
</div>
</div>
<div class="form-actions">
<input type="submit" value="submit" class="btn btn-success">
</div>
</form>
这就是为什么我使用这个代码product.blade.php
@foreach(App\Category::all() as $cat)
<option value="{{$cat->id}}" >
{{ $cat->parent ? $cat->parent->name .' -- ' : '' }}
{{$cat->name}}
</option>
@endforeach
@foreach(App\Category::all() as $cat)
<option value="{{$cat->id}}" >
{{ $cat->parent ? '--' . $cat->name : $cat->parent->name }}
</option>
@endforeach
我认为结果会是这样的。 如果没有父渲染类别$cat->parent->name
。 如果有父渲染子类别'--'. $cat->name
'--'. $cat->name
。
Shoes
-- Casual Shoes
希望对你有帮助
Category.php - 添加子关系
public function children(){
return $this->hasMany('App\Category','id','parent_id');
}
product.blade.php
<select name="category_id" id="category_id" style="width:220px;" >
<option value='' selected disabled>Select</option>
@foreach(App\Category::where('parent_id', 0)->get() as $parent)
<option value="{{$parent->id}}">
{{ $parent->name }}
</option>
@foreach($parent->children as $child)
<option value="{{$child->id}}">
-- {{$child->name}}
</option>
@endforeach
@endforeach
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.