A drill down technique would be easier in my opinion. And do you really need Topic and Category.
There are ways to achieve this and yet keep it simple.
I am creating a small news system and these are the tables I have available:
News Table
Category Table
Topic Table
Related post/category Table
Related post/topic Table
When writing a post, both categories and topics must be assigned. Topics and categories are the same thing but with a difference: you can assign unlimited categories but only 1 topic.
When I search for posts by category I get the list of posts containing that category. What I would like is to put first the posts that have the topic corresponding to the search made and then all the others.
Let me give you an example:
If I run the following query
$search = 'TV series';
$query = Posts::with([
'category_related',
'topic_related'
])
->whereHas('category_related', function($query) use ($search){
$query->where('name', 'like', '%'.$search.'%');
})
->orderBy('created_at', 'desc')
->paginate(20);
I get this list:
I would like this list:
In the second list I would like to obtain, news number 5 is in last position because the topic is different from the search made.
Thanks in advance
Please or to participate in this conversation.