简体   繁体   中英

category and sub-category isnt fetch while edit product in 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM