samedi 24 décembre 2016

Calling a list of posts based on category

Im trying to call a list os posts based on a category, but it isnt working, is giving me the error:Non-static method App\Category::posts() should not be called statically, assuming $this from incompatible context. I already tried calling from both sides, going threw the post model methdos or categories, but same error message. Can figure out what im doing wrong.

Database:

Posts:
-id;
-title;
-text;

Categories:
-id;
-name;
-internal_name;

category_post:
-id;
-post_id;
-category_id

Controller:

 public function categoryPostList($category)
    {
       $category = Category::posts()->where('internal_name',$category)->first(); 
       $posts = Post::categories()->where('category_id',$category->id)->get(); 
dd($posts)
    }

Model Post:

 class Post extends Model
    {
        public function categories(){
            return $this->belongsToMany(Category::class);
        }       
    }

Model Category:

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire