dsw8292301 2017-02-09 06:29
浏览 70
已采纳

在laravel 5.3中编辑产品时,类别和子类别无法获取

i am trying to update my product but for that i have to fetch all the data from database and everything is working fine expect categories and sub-categories, its not showing in select box.

here is my blade file code

                <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>

here is my product 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'));

}

here is my product model

<?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();
}


}

here is my category model

<?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();
        }
    });
}


}

here is my categories table in database

here is my products table in database

here is my website look, category and sub-category isnt selcted.

  • 写回答

1条回答 默认 最新

  • duan41497 2017-02-09 12:26
    关注

    i made it i just need to do this

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?