Deekshith's avatar

add condition along with pagination for one to many relationsip.

i have a query like below,

$cat = Taxonomy::category()->slug('current-affairs-2')->with(['posts'=>function($q) {
                $q->where('status', 'publish')
					->whereDate('post_date','2021-03-03');
            }])->first();

    return $cat->posts()->paginate(10);

But here "where" condition is not working. how to add pagination along with conditions for one to many relationships?

can I add conditions here only?

 $cat->posts()->paginate(10);
0 likes
9 replies
MichalOravec's avatar
Level 75
 return $cat->posts()
    ->where('status', 'publish')
    ->whereDate('post_date','2021-03-03')
    ->paginate(10);

You don't need to have eager loading for posts on category.

Deekshith's avatar

status is an unknown column here because I should put this condition in posts relationship as this one to many relationship

MichalOravec's avatar

So why do you have it here?

>with(['posts'=>function($q) {
                $q->where('status', 'publish')
					->whereDate('post_date','2021-03-03');
            }])-
Deekshith's avatar

Sorry, it worked as there was a typo in the column name. :-)

Deekshith's avatar

Also, i stuck in corcel package where I have a query like below to fetch posts from wordpres DB

Taxonomy::category()->slug('current-affairs-2')->with('posts')->first();   

return $cat->posts()
             ->where('post_status', 'publish')
             ->whereYear('post_date','2019')
             ->whereMonth('post_date', '10')
             ->newest()
             ->paginate(10);

But here the problem if I want to add multiple categories in slug method how to add?

Here I want to add

['current-affairs-2','current-events-2']

So I want to merge all the posts which has these two categories and I will display those posts in frontend.

MichalOravec's avatar
$categorySlugs = [
    'current-affairs-2', 'current-events-2'
];

return Post::whereHas('categories', function ($query) use ($categorySlugs) {
        $query->whereIn('slug', $categorySlugs);
    })
     ->where('post_status', 'publish')
     ->whereYear('post_date','2019')
     ->whereMonth('post_date', '10')
     ->newest()
     ->paginate(10);
Deekshith's avatar

The categories method is undefined. as I am using Corel package for this and in documentation, they mentioned like below,

Categories and Taxonomies
Get a category or taxonomy or load posts from a certain category. There are multiple ways to achieve it.

// all categories
$cat = Taxonomy::category()->slug('uncategorized')->posts->first();
echo "<pre>"; print_r($cat->name); echo "</pre>";

// only all categories and posts connected with it
$cat = Taxonomy::where('taxonomy', 'category')->with('posts')->get();
$cat->each(function($category) {
    echo $category->name;
});

// clean and simple all posts from a category
$cat = Category::slug('uncategorized')->posts->first();
$cat->posts->each(function($post) {
    echo $post->post_title;
});

I am using first method but now i want to pass multiple categories and fetch all posts linked with those categories.

chaudigv's avatar

Try with whereHas()

->whereHas('posts', function($q) {
	$q->where('status', 'publish')
		->whereDate('post_date','2021-03-03');
	})
->with('posts')
1 like

Please or to participate in this conversation.