面临在类别下拉列表中正确显示子类别的问题
Facing problem to show properly sub-categories in the Categories Dropdown list
在类别下拉列表
中显示子类别时遇到问题
在我的代码中,我想显示类别和类别下的子类别在 产品中 table.
这是我的 类别 table
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();
});
}
这是我的产品table
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');
}
}
这是我的产品型号
4.Product.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
.
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>
在类别下拉列表
中显示子类别时遇到问题在我的代码中,我想显示类别和类别下的子类别在 产品中 table.
这是我的 类别 table
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();
});
}
这是我的产品table
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');
}
}
这是我的产品型号
4.Product.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
.
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>