Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent
Last updated 1 year ago.
0

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.

Eloquent Collection API

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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?

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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. :-/

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

It's can be easily implemented by analogy to ordering by polymorph one-to-one relationship answer

0

Sign in to participate in this thread!

Eventy

Your banner here too?

dimsav dimsav Joined 31 Jan 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.