我正在尝试更新我的产品,但为此我必须从数据库中获取所有数据并且一切正常,除了类别和子类别,它没有显示在选择框中。
这是我的 Blade 文件代码
<div class="form-group">
<label>Brand</label>
<select class="form-control" name="brand_id" id="brand_id">
<option value=""></option>
@foreach($brands as $brand)
<option value="{{ $brand->id }}" {{ $product->brand_id == $brand->id ? "selected" : "" }}>{{ $brand->brand_name }}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label>Parent Category</label>
<select class="form-control" name="category" id="category" data-url="{{ url('api/dropdown')}}" >
@foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->category}}</option>
@endforeach
</select>
<br>
</div>
<div class="form-group">
<label>Sub-Category Category</label>
<select class="form-control" name="cat_id" id="sub_category">
<option value=""></option>
</select>
@if($errors->has('cat_id'))
<span class="help-block">{{ $errors->first('cat_id') }}</span>
@endif
</div>
<br>
这是我的产品 Controller
public function categoryAPI() {
// Get the "option" value from the drop-down.
$input = Input::get('option');
// Find the category name associated with the "option" parameter.
$category = Category::find($input);
// Find all the children (sub-categories) from the parent category
// so we can display then in the sub-category drop-down list.
$subcategory = $category->children();
// Return a Response, and make a request to get the id and category (name)
return \Response::make($subcategory->get(['id', 'category']));
}
public function editProducts($id) {
$product = Product::where('id', '=' , $id)->find($id);
$categories = Category::whereNull('parent_id')->get();
$brands = $this->brandsAll();
return view('admins.products.editproducts',compact('product','categories','brands'));
}
这是我的产品模型
<?php
namespace App;
use App\ProductPhoto;
use App\Brand;
use App\Category;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
protected $table = 'products';
protected $fillable = [
'product_name',
'product_qty',
'product_sku',
'price',
'reduced_price',
'cat_id',
'featured',
'brand_id',
'description',
'product_spec',
];
//protected $gaurded = ['id'];
/**
* One Product can have one Category.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function category() {
return $this->hasOne('App\Category', 'id');
}
// do same thing above for category() if you want to show what category a certain product is under in products page.
/**
* A Product Belongs To a Brand
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function brand() {
return $this->belongsTo('App\Brand');
}
/**
* Save a Product to the ProductPhoto instance.
*
* @param ProductPhoto $ProductPhoto
* @return Model
*/
public function addPhoto(ProductPhoto $ProductPhoto) {
return $this->photos()->save($ProductPhoto);
}
/**
* One Product can have many photos.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function photos() {
return $this->hasMany('App\ProductPhoto');
}
/**
* Return a product can have one featured photo where "featured" column = true (or 1)
*
* @return mixed
*/
public function featuredPhoto() {
return $this->hasOne('App\ProductPhoto')->whereFeatured(true);
}
/**
* Show a product when clicked on (Admin side).
*
* @param $id
* @return mixed
*/
public static function LocatedAt($id) {
return static::where(compact('id'))->firstOrFail();
}
/**
* Show a Product when clicked on.
*
* @param $product_name
* @return mixed
*/
public static function ProductLocatedAt($product_name) {
return static::where(compact('product_name'))->firstOrFail();
}
}
这是我的类别模型
<?php
namespace App;
use App\Product;
use Illuminate\Database\Eloquent\Model;
class Category extends Model {
protected $table = 'categories';
protected $fillable = ['category'];
//protected $guarded = ['id'];
/**
* One sub category, belongs to a Main Category ( Or Parent Category ).
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent() {
return $this->belongsTo('App\Category', 'parent_id');
}
/**
* A Parent Category has many sub categories
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function children() {
return $this->hasMany('App\Category', 'parent_id');
}
/**
* One Category can have many Products.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function product() {
return $this->hasMany('App\Product', 'id');
}
/**
* Delete all sub categories when Main (Parent) category is deleted.
*/
public static function boot() {
// Reference the parent::boot() class.
parent::boot();
// Delete the parent and all of its children on delete.
//static::deleted(function($category) {
// $category->parent()->delete();
// $category->children()->delete();
//});
Category::deleting(function($category) {
foreach($category->children as $subcategory){
$subcategory->delete();
}
});
}
}
这是我在数据库中的类别表
这是我在数据库中的产品表
这是我的网站外观,没有选择类别和子类别。
最佳答案
我做到了,我只需要这样做
<div class="form-group">
<label>Parent Category</label>
<select class="form-control" name="category" id="category" data-url="{{ url('api/dropdown')}}" >
<option value=""></option>
@foreach($categories as $category)
<option value="{{ $category->id }}" {{$product->Category->parent->id == $category->id ? "selected" : "" }}>{{ $category->category}}</option>
@endforeach
</select>
<br>
</div>
关于php - 在 Laravel 5.3 中编辑产品时无法获取类别和子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129731/