I guess you could merge the posts and the videos collection and then use the sort
method on the resulting eloquent collection. That's how I would try it, I haven't used that myself yet though.
The thing is the posts table contains thousands of posts. I can't just merge the collection of all posts with all the videos and then sort them. It has to be done in the database (eloquent) somehow.
I'm stuck right now with a pretty similar problem. I guess what we'd need is the ability to:
Tag::with('taggable')->where('name', 'TagName')
->orderBy('taggable.created_at')
->get();
But I guess that functionality is not easy to implement for the framework. Do you have an idea how you would solve it with plain SQL?
This is no a laravel issue, you would have the same problem writing this in vanilla SQL since they are separate indexes.
This is where something like mongodb is useful since you can just keep all these in the same table as objects.
However in SQL you will need some intermediary table which can be a bit messy. Or consider getting the latest videos and posts separately and display them separately.
This stackoverflow answer describes how we could get results from two different tables sorting by a common column. The sql UNION
operator is used.
I did a quick search in the laravel framework but the UNION
operator is not used to return eloquent models. :-/
websanova said:
This is no a laravel issue, you would have the same problem writing this in vanilla SQL since they are separate indexes.
This is where something like mongodb is useful since you can just keep all these in the same table as objects.
However in SQL you will need some intermediary table which can be a bit messy. Or consider getting the latest videos and posts separately and display them separately.
I would prefer to not move to mongodb to solve this issue as I am a total noob in mongodb and it's not supported by the framework by default.
(Unfortunately) the only solution I see, as you said, is to create an intermediary Listing
model that will contain either a video either an article (post). In this case listing
will have a many to many
relationship to Tag
. Listing
will also have a polymorphic relationship to Post
and Video
of course. I find this solution dirty but I can't think of another way.
That's why I opened a new thread in the forum to hear other opinions.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community