ive made a categorytrait just not sure how its supposed to be used in the category component? or if i even need to do something else? as in my postscontroller and category model all i did was to use CategoryTrait; at the top. not sure if im doing it correctly. This is the trait
<?php
namespace App\Traits;
use App\Models\Post;
use App\Models\Category;
use Illuminate\Support\Carbon;
trait CategoryTrait {
public function byCategory(Category $category)
{
$categories = Category::query();
$posts = Post::query()
->join('category_post', 'posts.id', '=', 'category_post.post_id')
->where('category_post.category_id', '=', $category->id)
->where('active', '=', true)
->whereDate('published_at', '<=', Carbon::now())
->orderBy('published_at', 'desc')
->paginate(10);
return view('categories', compact('posts', 'categories', 'category'));
}
public function index()
{
$posts = Post::query()
->where('active', '=', 1)
->where('published_at', '<', Carbon::now())
->orderBy('published_at', 'desc')
->with('categories')
->take(4)
->get();
$latestPost = $posts->shift();
return view('home', compact('posts', 'latestPost'));
}
}
everything works as per usual, just that im still getting the same syntax error.