1
我正在嘗試更新我的產品,但因爲我必須從數據庫中提取所有數據,並且所有工作都很好,所以期望的類別和子類別不會顯示在選擇框中。類別和子類別不是在laravel編輯產品時獲取5.3
這裏是我的刀片文件代碼
<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>
這裏是我的產品控制器
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();
}
});
}
}
這裏是數據庫
這裏是我的產品表數據庫
,這裏是我的網站看看,類別和子類別的心不是我的selcted類別表。
可以的print_r($類)開始它的顯示數據? –
[在編輯表單時獲取選擇框下拉列表中的數據 - Laravel 5.2](http://stackoverflow.com/questions/36480158/get-data-in-select-box-dropdown-when-editing-form- laravel-5-2) –
你可以創建一個plnkr或完整的代碼? –