mehrdad70's avatar

Display products of each category in laravel

Hi everyone When I click on a category I want it to go to the course list page and show the courses related to that category

header.blade.php

            @foreach ($categories as $category)
            <li class="{{count($category->subCategory) > 0  ? 'dropdown' : ''}}">
                <a href="{{route('courses.list' , $category->name)}}">{{$category->name}}</a>
                <ul class="dropdown-menu">
                @foreach ($category->subCategory as $child)
                    <li class="{{count($child->subCategory) > 0  ? 'dropdown' : ''}}">
                        <a href="{{route('courses.list' , $child->name)}}">{{$child->name}}</a>
                        @include('front.categories.child'  , ['child' => $child])
                    </li>
                @endforeach
                </ul>
            </li>
            @endforeach

web.php

    Route::get('/courses/list' , 'FrontController@courseList')->name('courses.list');

    public function courseList()
    {
        $courses = Course::filter()->where('status' , Course::STATUS_ACCEPT)->latest()->paginate(10);
        $categories = Category::all();
        $teachers = User::all();

        return view('front.courses.all-courses.all-course' , compact('courses' , 'categories' , 'teachers'));
    }

course-list.blade.php

                            @forelse ($courses as $course)
                            <div class="col-4 col-md-6 col-12">
                                <div class="box">
                                        <a href="{{$course->path()}}">
                                            <img class="card-img-top" src="{{'/storage/' . $course->banner->files['600']}}" alt="Card image cap">
                                        </a>
                                        <div class="box-body">
                                            <div class="text-left">
                                                <h4 class="box-title"><a href="{{$course->path()}}">{{$course->limitCharTitle()}}</a></h4>
                                                <p>{!! $course->limitCharBody()!!}</p>
                                                <div class="row">
                                                    <p class="mb-10 text-light font-size-12"><i class="fa fa-graduation-cap mr-5"></i>{{$course->teacher->fullName ?? ''}}</p>
                                                    <p class="mb-10 text-light font-size-12" style="margin-left: 30px"><b class="badge badge-info">{{$course->getFreeOrCash()}}</b><b class="text-danger"> : قیمت</b></p>
                                                </div>
                                                <div class="row">
                                                    <a href="{{$course->path()}}" class="btn btn-outline btn-primary btn-sm">جزئیات دوره</a>
                                                    <span class="btn btn-outline btn-info btn-sm" style="margin-left: 10px">{{$course->formatTime()}}  : زمان دوره</span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                @empty
                                <div class="alert alert-danger">
                                    <b>دوره ای یافت نشد</b>
                                </div>
                            @endforelse

I have a button on the main page with a list of all courses Which when I click on it applies the path, method and blade I mentioned above The URL is the same localhost: 8000/courses/list Now I want it to change when I click on each category, but go to the same course list page and show the courses related to that category localhost:8000/courses/list?category = laravel

0 likes
8 replies
CorvS's avatar

@mehrdad70 You are not filtering the courses by category, what do you expect? How are your courses related to categories anyway? What do your tables and relationships look like?

mehrdad70's avatar
    public function courses()
    {
        return $this->belongsToMany(Course::class);
    }

shami003's avatar

The course should have relationship with categories Categories Model:

public function posts()
{
   return $this->hasMany('App\Course');
}

From view pass category id <a href="{{route('courses.list' , $category->id)}}">{{$category->name}}</a>

Your route should have id parameter Route::get('/courses/list/{id}' , 'FrontController@courseList')->name('courses.list');

In your controller

public function courseList($id)
    {
        $courses = Course::filter()->where('status' , Course::STATUS_ACCEPT)->where('category_id', $id)->latest()->paginate(10);
        $categories = Category::all();
        $teachers = User::all();

        return view('front.courses.all-courses.all-course' , compact('courses' , 'categories' , 'teachers'));
    }
mehrdad70's avatar

I use many to many relationships but i get an error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'category_id' in 'where clause' (SQL: select count(*) as aggregate from `courses` where `status` = accept and `category_id` = 10)

    public function courses()
    {
        return $this->belongsToMany(Course::class);
    }
CorvS's avatar

@mehrdad70 You have to query that relationship then:

$courses = Course::where('status' , Course::STATUS_ACCEPT)
    ->whereHas('categories', function ($query) use ($id) {
        $query->where('categories.id', $id);
    })
    ->latest()
    ->paginate(10);

Assuming your relationship inside your Course model is named categories().

mehrdad70's avatar

i use scopFilter in course model for filter but this code not working

mehrdad70's avatar

My problem is that by clicking on any category, I want to see the related products as described above

Please or to participate in this conversation.