php - Facing problem to show properly sub-categories in the Categories Dropdown list
问题描述
Troubling problem to show sub-categories in the Categories Dropdown list
in my code,i want to show category and sub catagory under the Category in the Products table.
Here is my categories 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();
});
}
Here is my products table
2.
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id');
$table->string('product_name');
$table->timestamps();
});
}
Here is my Category Model
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');
}
}
Here is my Product Model
4.Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function category(){
return $this->hasone('App\Category');
}
}
Now here is my 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');
}
}
Here is my product.blade.php file
<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>
thats why i use this code 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
I think the result would be something like this.
If not has parent render category $cat->parent->name
.
If has parent render sub-category '--' . $cat->name
.
Shoes
-- Casual Shoes
I hope it helps you
推荐阅读
- python - 将 float 转换为 str 正在删除过程中的字符
- android - 如何在 EditText 中放置单位?
- python-3.x - 如何模拟构造函数pytest
- php - 连接被拒绝 [2002] symfony docker
- javascript - 如何从 blob:chrome-extension 获取 blob 数据?
- performance - 并行文件下载
- c# - 仅通过一张照片检测活体(人脸反欺骗)。C#
- python - 更高分辨率与更低分辨率的 Canny 边缘检测
- flutter - Flutter 如何在单击时在 SVG 图像上添加墨水效果
- react-native - 将 3D 张量转换回图像 (jpg/png)