Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

First off this will not work as you expect (2), and it is going to throw fatal error (1):

$query->orderBy('likesCount + replyCount', 'DESC')->take(20);
  1. It's calling 'likesCount + replyCount' column, which is obviously not there -> use raw for such clauses:

    orderBy(DB::raw('likesCount + replyCount'), 'desc')

  2. take(20) will do LIMIT 20 on the second query that loads related models. That being said, you will get only 20 related models in total, not 20 per StatusModel.

Last updated 2 years ago.
0

Yes I want to take 20 comments and related commentTags where the pageId in the statuses table is what's passed in... I'll try the DB::raw and let you know further

Last updated 2 years ago.
0

I tried it and changed the query to the following

$comments = StatusesModel::with(array(
            'comments' => function($query)
            {
                $query->orderBy(DB::raw('likesCount + replyCount'), 'desc')->take(20);
            },
            'comments.commentTags' => function($query)
            {
                $query->where('commentTags.tag', 'LIKE', "%{$sentiment}%");
            })
        )->where('pageId', $pageData->pageId)->get();

The thing is that the alert in the JS isn't alerting anything ... I don't even see an error in the terminal ...

Last updated 2 years ago.
0

In side the

 'comments.commentTags' => function($query)
        {
            $query->where('commentTags.tag', 'LIKE', "%{$sentiment}%");
        }

The IDE says ... Variable $sentiment seems to be uninitialized ... Im passing it into the function but why is it saying that?

Last updated 2 years ago.
0

OK so i changed

$query->where('commentTags.tag', 'LIKE', "%{$sentiment}%");

To

$query->where('commentTags.tag', 'LIKE', "%pos%");

And It worked ... but the thing is ... how do I use that variable in the query??

Last updated 2 years ago.
0

and FYI this is the function

    public static function getHotComments($pageName, $sentiment)
{
    $pageData = PagesModel::where('pageName', '=', $pageName)->first();
    
    $comments = StatusesModel::with(array(
            'comments' => function($query)
            {
                $query->orderBy(DB::raw('likesCount + replyCount'), 'desc')->take(20);
            },
            'comments.commentTags' => function($query)
            {
                $query->where('commentTags.tag', 'LIKE', "%{$sentiment}%");
            })
        )->where('pageId', $pageData->pageId)->get();
            
    return $comments;;
}
Last updated 2 years ago.
0

Got it ... i had to use the following

'comments.commentTags' => function($query) use ($sentiment)
            {
                $query->where('commentTags.tag', 'LIKE', "%{$sentiment}%");
            })

that enabled me to use the passed parameter in the query :)

Thanks you for the help :)

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Haseeb90 haseeb90 Joined 8 Apr 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.