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);
It's calling 'likesCount + replyCount' column, which is obviously not there -> use raw for such clauses:
orderBy(DB::raw('likesCount + replyCount'), 'desc')
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.
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
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 ...
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?
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??
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;;
}
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 :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community