[英]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.