简体   繁体   中英

Display all products under sub-categories - Laravel 5.2

I need to display all the products under a certain sub-category. I already have a categories menu set up on the home page which shows all the parent and its sub-categories. when I click on the sub-categories, it takes me to the sub-categories page with its id listed in the url. Problem Im having, is I dont know how to display all the products under that sub-category on that page?

Here is my route to display the sub-categories pages:

Route::group(['middleware' => ['web']], function () {

   Route::get('/category/{id}', [
        'uses'  => 'PagesController@categoryDisplay',
        'as'    => 'category.show'
    ]);

}

here is my ProductController.php to display all products under a sub-category:

class PagesController extends Controller {


    public function categoryDisplay($id) {

        $products = Product::all();

        //$cat_id = Product::where('cat_id', '=', 3);

        return view('category.show', compact('products'));
    }


}

Here is my page where im trying to display the products:

@extends('app')


@section('content')

    <div class="container">

        <h3 class="text-center">
            Hi
            @foreach($products as $product)
                {{ $product->product_name }} <br>
            @endforeach
        </h3>

    </div>

@endsection

My Category Model:

class Category extends Model {

    protected $table = 'categories';

    protected $fillable = ['category'];


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

}

My Products Model:

class Product extends Model {

    protected $table = 'products';

    protected $fillable = [
        'product_name',
        'price',
        'cat_id'
    ];


    /**
     * One Product can have one Category.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function category() {
        return $this->hasOne('App\Category', 'id');
    }



}

And this is how my products table looks like:

产品表

"cat_id" is the products sub-category

As you can see in my controller, if I pass in an actual number, like 3, it will show all the products under that cat_id, but obviously i need it to be dynamic.

I got it: I had to change a few things like my Route:

 Route::get('category/{id}','PagesController@displayProducts');

My Controller:

class PagesController extends Controller {


    public function displayProducts($id) {


        $categories = Category::where('id', '=', $id)->get();

        $products = Product::where('cat_id','=', $id)->get();


        return view('category.show', compact('products', 'categories'));
    }


}

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