简体   繁体   中英

Facing problem to show properly sub-categories in the Categories Dropdown list

Troubling problem to show sub-categories in the Categories Dropdown list

in my code,i want to show category and sub catagory under the Category in the Products table .

Here is my categories table

1.

 public function up() { Schema::create('categories', function (Blueprint $table) {

    $table->increments('id');
    $table->integer('parent_id'); //sub category id
    $table->string('name');  
    $table->rememberToken();
    $table->timestamps();

    });
}

Here is my products table

2.

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('category_id');
        $table->string('product_name');
        $table->timestamps();
    });
}

Here is my Category Mode l

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');
    }

    public function parent(){
        return $this->belongsTo('App\Category','parent_id','id');
    }
}

Here is my Product Model

4.Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function category(){
        return $this->hasone('App\Category');
    }

}

Now here is my 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 product(){
         return view('admin.products.product');
     }


}

Here is my product.blade.php file

<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">Under Category </label>
                                    <div class="controls">
                                        <select name="category_id" id="category_id" style="width:220px;"  >
                                            <option value='' selected disabled>Select</option>
                                           @foreach(App\Category::all() as $cat)
                                                <option value="{{$cat->id}}"  >
                                                    {{ $cat->parent ? $cat->parent->name .' -- ' : '' }}
                                                    {{$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="form-actions">
                                    <input type="submit" value="submit" class="btn btn-success">
                                </div>
                            </form>

I want to data like this我想要的是

thats why i use this code product.blade.php

 @foreach(App\Category::all() as $cat)
                                                <option value="{{$cat->id}}"  >
                                                    {{ $cat->parent ? $cat->parent->name .' -- ' : '' }}
                                                    {{$cat->name}}
                                                </option>
                                            @endforeach

but i get data like this, which i donot want:- 我得到这样的数据

@foreach(App\Category::all() as $cat)
 <option value="{{$cat->id}}"  >
  {{ $cat->parent ? '--' . $cat->name : $cat->parent->name }}
 </option>
@endforeach

I think the result would be something like this. If not has parent render category $cat->parent->name . If has parent render sub-category '--'. $cat->name '--'. $cat->name .

Shoes
-- Casual Shoes

I hope it helps you

Category.php - add child relation

public function children(){
    return $this->hasMany('App\Category','id','parent_id');
}

product.blade.php

<select name="category_id" id="category_id" style="width:220px;"  >
  <option value='' selected disabled>Select</option>
      @foreach(App\Category::where('parent_id', 0)->get() as $parent)
          <option value="{{$parent->id}}">
              {{ $parent->name }}
          </option>
          @foreach($parent->children as $child)
              <option value="{{$child->id}}">
                  -- {{$child->name}}
              </option>
          @endforeach
       @endforeach
 </select>

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