title: Get Category posts in laravel 5.4
body: <p>I want to get posts under their category name. I tried lots of tutorials on the web none of them works for me!</p>
  
  <p>This is my Controller:</p>
  
  <pre><code>public function showcoursecat($slug) {
      $coursecateories = course::with('coursecateory')
          -&gt;where('slug', $slug)
          -&gt;get();
  
      return view ('frontend.coursecats', compact('coursecateories'));
  }
  </code></pre>
  
  <p>This is my route:</p>
  
  <pre><code>Route::get('/courses/cat/{slug}', 'FrontendController@showcoursecat')
      -&gt;name('coursecat')
      -&gt;where('slug', '[\w\d\-\_]+');
  </code></pre>
  
  <p>My Course model:</p>
  
  <pre><code>class Course extends Model
  {
      use Notifiable;
  
      protected $table = "Courses";
  
      protected $fillable = [
          'course_name', 
          'course_description', 
          'course_price', 
          'course_disscount',  
          'slug', 
          'image', 
          'category_id', 
          'user_id', 
          'approve',
      ];
  
      protected $events = [
          'created' =&gt; Events\CourseApproved::class,
      ];
  
      public function user() {
          return $this-&gt;belongsTo(User::class);
      }
  
      public function coursecateories() {
          return $this-&gt;belongsTo(Coursecateory::class, 'category_id');
      }
  }
  </code></pre>
  
  <p>My Coursecateory model:</p>
  
  <pre><code>class Coursecateory extends Model
  {
      protected $fillable = [
          'name', 
          'image', 
          'slug',
      ];
  
      public function courses() {
          return $this-&gt;hasMany(Course::class, 'category_id');
      }
  }
  </code></pre>
  
  <p>And here is my view:</p>
  
  <pre><code>@foreach($coursecateories as $coursecateory)
  
      &lt;h3&gt;{{ $coursecateory-&gt;course_name }}&lt;/h3&gt;
  
      @foreach($coursecateory-&gt;courses as $course)
  
          &lt;h4&gt;&lt;a href="#"&gt;{{ $course-&gt;course_name }}&lt;/a&gt;&lt;/h4&gt;
  
          &lt;p&gt;{{ $course-&gt;course_description }}&lt;/p&gt;
  
      @endforeach
  
  @endforeach
  </code></pre>