在我的代码中,我想在产品表中的类别下显示类别和子类别。
此处是我的类别表
1。
public function up() { Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('parent_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->string('product_code');
$table->timestamps();
});
}
此处是我的类别模型
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');
}
此处是我的产品型号
4.Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function category(){
return $this->hasone('class::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 addproduct(Request $request){
$product=New Product();
$product->category_id=$request['category_id'];
$product->product_name=$request['product_name'];
$product->product_code=$request['product_code'];
$product->status = request('status');
$product->save();
return back()->with('success','product Upload Successfully!');
}
}
这是我的 addproduct.blade.php 文件
6.addproduct.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">main Category </label>
<div class="controls">
<select name="category_id" id="category_id" style="width:220px;">
<option value="0">Main Category</option>
@foreach(App\Category::all() as $cat)
<option value="{{$cat->id}}" >{{$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="control-group">
<label class="control-label">Product Code</label>
<div class="controls">
<input type="text" name="product_code" id="product_code">
</div>
</div>
<div class="control-group">
<label class="control-label">Product Color</label>
<div class="controls">
<input type="text" name="product_color" id="product_color">
</div>
</div>
<div class="form-actions">
<input type="submit" value="submit" class="btn btn-success">
</div>
</form>
7。我想要这样的数据
鞋子 -休闲鞋 -正式鞋 移动 --Android手机 --iphone
答案 0 :(得分:0)
在您的类别模型中
var values = [0,1,2,3,6,7,8,9,10,12,14,16,17]; //this can occur as X in 1 << X
function bitNumberToArray(bitNumber) {
return values.filter(bit => bitNumber & 1 << bit );
}
console.log(bitNumberToArray(834));
刀片式服务器
class Category extends Model { protected $guarded=[];
public function products(){
return $this->hasMany('App\Product');
}
public function parent(){
return $this->belongsTo('App\Category');
}
}
Ps:对于您的开发:用于查看代码的刀片,您应该在刀片而不是模型中使用变量。因此,像这样@foreach(App\Category::all() as $cat)
<option value="{{$cat->id}}" >{{ isset($cat->parent->name) ? $cat->parent->name . ' -- ' : '' }}{{$cat->name}}</option>
@endforeach
的板条箱变量将传递到Controller中的索引方法中以在刀片中使用。