首页 > 解决方案 > 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 Model

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 what i want

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 :- i get data like this

标签: phplaraveleloquente-commerce

解决方案


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

Shoes
-- Casual Shoes

I hope it helps you


推荐阅读