关系:产品,类别和子类别

时间:2016-11-06 09:42:12

标签: laravel-5.2

我是laravel 5.2的新手。

有没有人会帮我一个例子来定义类别之间的模型关系 - 子类别 - 产品以及如何通过相关ID获取子类别下的产品?

    // Category  model
public function subcategories(){ return $this->hasMany('App\Subcategory', 'category_id'); }    
public function products() {return $this->hasMany('App\Product', 'subcategory_id');}

// Subcategory model 
public function categories() {return $this->belongsTo('Category');}
public function products() {return $this->hasManyThrough('App\Product'
,'App\Category','subcategory_id','category_id','id');}

// Product model
public function categories() {   return $this->belongsTo('App\Category');}
public function subcategories() {return $this->belongsTo('App\Subcategory');}
// my controller
// class to fetch subcategories under category
public function show_subcategory($name){  
$categories=Category::whereName($name)->get(); 
return view('project.show_subcategory')->with('categories',$categories); 
} 

// class to fetch product under subcategories    
public function show_product($id){ 
$subcategories = Subcategory::whereId($id)->get();
return view('project.show_product')->with('subcategories',$subcategories);}
 //  views - show_subcategory.blade.php
@foreach( $categories as $category)
@foreach( $category->subcategories as $subcategory )    
{{$subcategory->name}}
@endforeach
@endforeach 

//  views - show_product.blade.php
@foreach( $subcategories as $subcategory) 
@foreach( $subcategory->products as $product ) 
{{$product->title}}
@endforeach 
@endforeach
Route::get('/show_subcategory/{name}','Project\MainController@show_subcategory');
Route::get('/show_product/{id}','Project\MainController@show_product');
// migrations - create_subcategories_table.php
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('image');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade'); 
$table->timestamps();

// migrations - create_products_table.php
$table->engine = 'InnoDB';            
$table->increments('id');
$table->integer('subcategory_id')->unsigned();
$table->foreign('subcategory_id')->references('id')->on('subcategories')->onDelete('cascade'); 
$table->string('title');
$table->text('description');
$table->integer('price');
$table->boolean('availability')->default(1);
$table->string('image');
$table->timestamps();
  

注意:类show_subcategory运行良好,我可以在类别

下显示子类别      

但是show_product类不工作,我无法显示产品   子类别..视图不显示任何错误,不显示   产品

     

我不知道模型关系或控制器类或视图中的错误在哪里

2 个答案:

答案 0 :(得分:0)

你的错误:

  1. 您试图获取产品INTO子类别表:/,您的子类别表没有产品信息。
  2. 您尝试过滤子类别WHERE id = $ id,这是获取子类别信息而非产品信息。尝试做:
  3. url:

    /show_product/1
    

    刀片:

    @foreach( $subcategories as $subcategory) 
    
    {{$subcategory->name}}
    
    @endforeach
    

    它将起作用,并将显示子类别ID为1的子类别名称。

    使用SUBCATEGORY获取产品

    您需要在产品表中包含子类别ID,以便执行以下操作:

    <强> ROUTE

    Route::get('/show_product/{subcategory_id}','Project\MainController@show_product');
    

    <强> CONTROLLER

    // get products with a specific subcategory id
    
    public function show_product($subcategory_id){ 
    $subcategories = Subcategory::where('subcategory_id', $subcategory_id)->get();
    return view('project.show_product', compact('subcategories '));
    }
    

    通过这种方式,您将获得具有特定id子类别

    的所有产品

    你可能想要这样:

    // class to fetch product under subcategories    
    public function show_product($id){ 
    $subcategories = Subcategory::where('product_id', $id)->get();
    return view('project.show_product')->with('subcategories',$subcategories);}
    

    这可以获得具有特定产品ID的所有子类别。但是您需要更改迁移子类别和ADD product_id关系。

    // migrations - create_subcategories_table.php
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('name');
        $table->string('image');
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade'); 
        $table->integer('product_id')->unsigned();
        $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); 
        $table->timestamps();
    

    您还需要在模型产品和子类别中添加关系,但我不能很好地理解您的结构分类。请举个例子:

    1. 第一类
      • firs subcategory
      • 第二个子类别
    2. 第二类
      • 其他子类别
    3. 每个产品都有一个类别,类别有更多子类别的孩子,但我不确定你是否想要。屏幕截图可以帮助我们。

答案 1 :(得分:0)

// Category  model
public function subcategories(){ return $this->hasMany('App\Subcategory', 'category_id'); }    
public function products() {return $this->hasMany('App\Product', 'subcategory_id');}

// Subcategory model 
public function categories() {return $this->belongsTo('Category');}
public function products() {return $this->hasManyThrough('App\Product'
,'App\Category','subcategory_id','category_id','id');}

// Product model
public function categories() {   return $this->belongsTo('App\Category');}
public function subcategories() {return $this->belongsTo('App\Subcategory');}
// my controller
// class to fetch subcategories under category
public function show_subcategory($name){  
$categories=Category::whereName($name)->get(); 
return view('project.show_subcategory')->with('categories',$categories); 
} 

// class to fetch product under subcategories    
public function show_product($id){ 
$subcategories = Subcategory::whereId($id)->get();
return view('project.show_product')->with('subcategories',$subcategories);}
 //  views - show_subcategory.blade.php
@foreach( $categories as $category)
@foreach( $category->subcategories as $subcategory )    
{{$subcategory->name}}
@endforeach
@endforeach 

//  views - show_product.blade.php
@foreach( $subcategories as $subcategory) 
@foreach( $subcategory->products as $product ) 
{{$product->title}}
@endforeach 
@endforeach
Route::get('/show_subcategory/{name}','Project\MainController@show_subcategory');
Route::get('/show_product/{id}','Project\MainController@show_product');
// migrations - create_subcategories_table.php
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('image');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade'); 
$table->timestamps();

// migrations - create_products_table.php
$table->engine = 'InnoDB';            
$table->increments('id');
$table->integer('subcategory_id')->unsigned();
$table->foreign('subcategory_id')->references('id')->on('subcategories')->onDelete('cascade'); 
$table->string('title');
$table->text('description');
$table->integer('price');
$table->boolean('availability')->default(1);
$table->string('image');
$table->timestamps();
  

注意:类show_subcategory运行良好,我可以在类别

下显示子类别      

但是show_product类不工作,我无法显示产品   子类别..视图不显示任何错误,不显示   产品

     

我不知道模型关系或控制器类或视图中的错误在哪里