在类别下拉列表中显示子类别的问题
在我的代码中,我想产品表中的类别下显示类别和子类别。
这是我的类别表
1。
a = [4, 2, 3, 4, 4, 9, 98, 98, 3, 3, 3, 4, 2, 98, 1, 98, 98, 1, 1, 4, 98, 2, 98, 3, 9, 9, 3, 1, 4, 1, 98, 9, 9, 2, 9, 4, 2, 2, 9, 98, 4, 98, 1, 3, 4, 9, 1, 98, 98, 4, 2, 3]
a.sort(key=lambda x: a.count(x))
这是我的产品表
2。
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();
});
}
这是我的类别模式 l
3.Category.php
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id');
$table->string('product_name');
$table->timestamps();
});
}
这是我的产品型号
4.Product.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');
}
}
现在这是我的 ProductsController.php
5.ProductsController.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function category(){
return $this->hasone('App\Category');
}
}
这是我的 product.blade.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>
答案 0 :(得分:1)
@foreach(App\Category::all() as $cat)
<option value="{{$cat->id}}" >
{{ $cat->parent ? '--' . $cat->name : $cat->parent->name }}
</option>
@endforeach
我认为结果将是这样。
如果不是,则具有父渲染类别$cat->parent->name
。
如果具有父渲染子类别'--' . $cat->name
。
Shoes
-- Casual Shoes
希望对您有帮助
答案 1 :(得分:1)
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>