Sidart's avatar
Level 12

How to display Posts via the Category

This is my first laravel Application, and my first database based application so please be patient with me !! I will try to be specific!!

Categories Table: Id Name Timestamps

Posts table: Id title body slug Category_id timestamps

Lets say i have 4 catergories. Laptops, computers,phones,tablets

I want when i go to /computers to be able to get all the posts that are specific to that category.

Category Model: <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
        protected $table ='categories';

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

Post Model:

0 likes
11 replies
vipin93's avatar
$category = Category::with('posts)->get();

and in view use

@foreach($category->posts as $post)

{{ $post->name }}, {{ $post->id }}// etc..

@endforeach 
Sidart's avatar
Level 12

After trying out what you said i am getting Undefined variable: category

vipin93's avatar

ohhhhhh bro then u dont pass your variable to your view then u must first watch "laravel-5.4 from scratch-2017" in this case u should pass variable like

return view('view', compact('category'));
Sidart's avatar
Level 12

Property [posts] does not exist on this collection instance. (View: C:\xampp\htdocs\blog\resources\views\pages\computer-id.blade.php)

This is what i am getting now.

I will and watch what u said. Been struggling to get this to work for the past day!!

vipin93's avatar
class Category extends Model
{
        protected $table ='categories';

        public function posts()
    {   
         return $this->hasMany('App\Post');
        }
}
1 like
vipin93's avatar

show your controller and model Post model also

Sidart's avatar
Level 12

Post Model <?php

  namespace App;

  use Illuminate\Database\Eloquent\Model;

  class Post extends Model
  {
    public function category()
    {
       return $this->belongsTo('App\Category','Category_id');
  }

}

  CatController
 <?php

   namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use App\Post;
  use App\Category;

 class CatController extends Controller
{
  public function show()
  {
     $category = Category::with('posts')->get();
    return view('pages.computer-id',compact('category'));
  }

}
vipin93's avatar
vipin93
Best Answer
Level 13

sorry my mistake use like

  public function show(Category $category)
  {
     $category= $category->posts;
    return view('pages.computer-id',compact('category'));
  }

and in your Category model add this

public function getRouteKeyName()
{
   return 'name'
}

and your route like

Route::get('all/{category}','CatController@show');

and also now your view like

@foreach($posts as $post)
//continue
@endforeach
Sidart's avatar
Level 12

Property [posts] does not exist on this collection instance. (View: C:\xampp\htdocs\blog\resources\views\pages\computer-id.blade.php)

i found the solution

<?php

namespace App\Http\Controllers;

 use Illuminate\Http\Request;
 use App\Post;
 use App\Category;

 class CatController extends Controller
 {
      public function show(Category $category)
    { 
     $posts = Category::findOrFail($category)->posts()->get();                                                                                                                           return view('pages.computer-id',compact('category'))
         ->with('posts',$posts);
  }
}

You are amazing !!! Thanks for all your input @vipin93!!!

vipin93's avatar
class CatController extends Controller
 {
      public function show($category =null)
    { 
     $category = Category::where('name',$category)->first();                                                                                                                               return view('pages.computer-id',compact('category');
  }
helpmyworld's avatar

where does the below code go?

  {
     $category= $category->posts;
    return view('pages.computer-id',compact('category'));
  }```

I am trying to display my post per category also. i need help. i also want to know as per above code example, can i return view to two pages(front and admin?

Please or to participate in this conversation.