Retrieving most liked comment using two tables - php

I have two tables, one for comments and another for likes, each row In the likes table shares the same comment ID in the comment table so I can link them together. However I'm only relating them through a foreach.
#foreach ($getNotesAll as $getnotes)
<?php
$upvoteCount = $getNoteVotes->where('noteUniqueID', $getnotes->noteUnique)-
>where('like-type', 'upvote')->count();
?>
#endforeach
Using
$getNoteVotes = noteLikes::all()->where('type', 'upvote');
$getNotesAll = Notes::orderBy('created_at','asc')->paginate(10);
I'm at a point where I need to get the most liked comments, so I'll have to query the like table to request rows with the most repeating comment ID's in descending order, then querying the Comments tables with the Comment Id's from the like query. I can't quite get my head around this without using a foreach loop, though I think this is achieved using models, but any guidance would be appreciated

Try something like this:
DB::table('commentsTable')
->join('likesTable', 'commentsTable.id', '=', 'likesTable.comment_id')
->select('commentsTable.tittle',DB::raw("count(likesTable.comment_id) as count"))
->groupBy('likesTable.comment_id')
->orderBy('likesTable.comment_id','asc')
->get();

Related

Laravel GroupBy Issue

I am looking for a way to return all fields that match together. For example, I have a player_name column, I'm looking to return all rows where the player names match. One player's name will return a collection of their results, and so on and so on for however many there are.
This is what I have tried so far
$test = DB::table('approved_g_r_p_s')
->select('player_name', 'cost')
->groupBy('player_name')
->get();
However it only returned one result from each.
One of player A's fields
One of player B's fields
Despite there being multiple others it has only returned one. Any suggestions?
It looks like you need to add the fields that interest you to the groupBy, something like this:
$test = DB::table('approved_g_r_p_s')
->select('player_name', 'cost')
->groupBy('player_name', 'cost')
->get();

Eloquent query based on presence of key in relationship group

So this one has kinda given me a mental run around. I have Rows, Users, and RowActivity models. Each time a user interacts with a row, it generates a single line in the row activity table saying what status the user changed. Each row can have 3-8 activity entries with a single user reference per line. So my question is this: I want to analyze the rows that particular users have interacted with. So I'd like to search for user A and get all the rows they've touched. But the only way to do that is to query the RowActivity table.
So the query would essentially be this:
Row::whereHas('rowActivity')->whereWithin(Collection of RowActivity, user_id = requested-user)->get();
I know that I can go the long way and query the RowActivity::where('user_id', $requestedUser) and then get all the other row activities based on the related row_id of that those results, but I feel there's a clean way that I can't figure out.
Just for clarification, the row activities are used to generate reports about which users changed the status of the row and how long the duration between the changes are. So I need to get all the activities associated with a row as well as all the rows that a user has touched so that I can analyze how long their portion of that interaction took.
If I need to do this as a multilevel query, so be it, I don't have an aversion to that, I just want to make sure my queries are pristine. If I did it as multi-level, it would look something like this:
$ra = RowActivity::where('user_id', $requestedUser)->pluck('row_id');
$rows = Row::with('rowActivity')->find($ra); //get all rows and their associated activities based on the plucked row id from query 1
You can do that within whereHas(). I hope that's what you want
Row::whereHas('rowActivity', function($query) use($requestedUser) {
$query->where('user_id', $requestedUser);
})->get();

Symfony Doctrine Querybuilder - Select only first row of ordered join in one-to-many relationship

Let's say I have entities User and Comment in a one-to-many relationship. Assume the relationship is set up correctly in the models and is called comments on the User model. Let's say Comment has a field text that is the text written in the comment.
Each Comment belongs to one User, but each User may have any number of comments (including no comments at all.)
I'm using Doctrine-ORM 2.7. I need to select User data and only the text of their most recent comment if it exists. I'm really struggling to piece together how to do this. My thoughts so far:
The query should leftJoin() the User with Comment table on the user ID. I think I want a LEFT JOIN since that way users without any comments are still selected.
The Comments part needs to be a nested query that does something like ORDER BY id DESC LIMIT 1 on the comments that have that user ID, so that only the most recent one is selected.
I don't think I can use groupBy() since I need to select the text of the most recent comment, but I could probably get the MAX(comment.id) through here and that might come in handy?
My progress on an attempt so far:
$qb = $em->getRepository(User::class)
->createQueryBuilder('u')
->select('u.id, u.username') // ...etc. user data
->addSelect('c.text') // comment text
->leftJoin('u.comments', 'c', 'WITH', ....) // Place nested subquery here somehow?
->getQuery()
->getArrayResult();
I feel like the nested query has to be something like this:
$qb = $em->getRepository(Comment::class)
->createQueryBuilder('c')
->select('c.id')
->where('c.user', ':user')
->orderBy('id', 'desc')
->getQuery()
->getScalarResult();
This is a made-up example; in the real application, optimization is a concern so I'd like to minimize the number of nested queries executed. This answer seems to have a good raw SQL approach, but I'm getting confused on how to translate that to query builder syntax. Any help would be appreciated.

PHP one to many relations

I have to table posts and comments have a relation between them i want to display comments of post I display post but I don't know how to display it's commented PHP native help, please
I would be nice if you provide your table structures and the code you have so far. Without those information it is harder to help. ;)
I assume you store the post_id in your comments table? So after you fetch your post in your php-script you will execute a second query on the comments table having your post_id. Sth. like:
Select * From comments WHERE post_id = $postId
make sure that your $postId is safe against SQL injections. Fetch the results in an php array and test if it's empty. If the array isn't empty, you can loop through it with foreach and list the comments.
Your question is not clear, but here is a approx answer
describe your table, displaying comments of a single post is quite easy,
SELECT * from comments WHERE post_id = 123
then use php to loop through comments
e.g
foreach($comments as $comment) {
...
}
OR if you want to get all posts with all comments, here is i created a fiddle for you
http://sqlfiddle.com/#!9/e0264e/1
Help yourself by editing the schema and playing with it
you can use left join to get this relation with post like
example:
SELECT * FROM `posts` WHERE Id=$postId LEFT JOIN `comments` WHERE id = post_id
where the post id field had put in the comments table.
I think this is best than use to separate query

How would you write a complex query in laravel 5?

Hello guys I wonder if this query I wrote with laravel's syntax is actually make sense.
What I am trying to do: I got two tables. One is users table. Second is the dependent table for users. its called user's_thoughts. I want to pull the top best results of posts from users table. and this top results are based on how many likes that particular post had. This like column is in user's_thoughts table.
Here is what I have put together for this:
$results=DB::table('users')->join('user_thoughts', 'users.id', '=', 'user_thoughts.user_id')->select('users.post_id', 'users.post_title', 'users.posts', 'user_thought.like')-get();
Now, after this query which selects from both tables, I am going to see which post had the highest amount of likes.
$count = DB::table('user_thoughts')->count('likes');
$highest = DB::table('user_thoughts')->max('$count');
Now, here I would loop the results out.
$show = foreach ($highest as $cherrypicked => $topones) {
echo $topones;
}
Have I followed this correctly? What do you think?
Thanks.

Categories