How can I count the nested replies? - laravel

I have a table structure like this.
--comments
id article_id comment_parent
1 9 0
2 0 1
3 0 1
4 0 2
5 0 4
Basically, the first comment is on the article_id, and replies to comments are on the comment_parent. The database above creates a nested comments such as this:
- Comment 1
- Comment 2
- Comment 4
- Comment 5
- Comment 3
The problem is, I couldn't find how to determine how many comments are on the article. Right now, article 9 has 5 comments.
I believe a recursive function would solve this issue, but my Eloquent experience is pretty basic.
How can I do something like this?
Article::find(9)->getAllCommentsAmount(); //5

I would suggest adding the article_id to the child comments as well, if that is allowed. It will make it easier to count the comments for a certain article.

You may want to look at implementing a Nested Set pattern in your database.
There is a fairly popular Laravel/Eloquent implementation available here:
https://github.com/etrepat/baum
Nested Sets are specifically designed for data that is heavily nested like yours, and allows you to quickly and easily (i.e. without heavy recursion) query your data.

Related

Best way to call functions while iterating through a foreach loop in Laravel Blade Template

I apologize if this is a stupid question – but I haven't found any great answers from searching online.
I have a database table of test scores, that looks something like this:
user | q1 | q2 | q3 | q4 | q5
-----------------------------------
user1 3 3 2 1 5
user2 4 2 1 4 5
user1 4 3 3 2 5
Any given user can have multiple entries in the table.
In my blade file, I am iterating through all of the rows in the table:
#foreach($scores as $score)
<tr>
<td>{{$score->user}}</td>
<td>{{$score->q1}}</td>
<td>{{$score->q2}}</td>
<td>{{$score->q3}}</td>
<td>{{$score->q4}}</td>
<td>{{$score->q5}}</td>
<td>{{$score->getTotalScore()}}</td>
</tr>
#endforeach
It is the function "getTotalScore()" in the last table cell that is causing the problems. I want to perform a slightly complex calculation based on all of the scores of the table – but I prefer not to do it inside the blade file.
I DO have a working version where I make use of #php / #endphp inside the blade file, and do the calculations that way – but it tends to go against my aim to have as little "calculating" in my view as possible.
Trying to put a function in the Model doesn't work, because I am not really returning a relationship. So really, I'm just not sure how to call a function, while in the middle of a #foreach() loop, iterating through returned results.
Can someone please point me in the right direction here?
Maybe you can use += in order to sum?

Get matching records in Laravel

I'm not sure if this is obvious and I don't see it because it is late here, but right now I'm struggling with the following:
I'm trying to find out if there is a match somewhere. So, profile 2 liked profile 1 and also, profile 1 liked profile 2. That would be a match.
I tried combining arrays but that that ran nowhere. ._. How could I archive this in Laravel queries?
$likey = DB::table('likes AS liker')
->join('likes AS liked', 'liker.liked_id', '=', 'liked.liker_id')
->select('liked.liker_id', 'liked.liked_id')
->where('liker.liker_id', '=', 'liked.liked_id')
->get();
Something along those lines.
EDIT: Just to clarify this solution so you don't get into temptation of copy pasting this and never figuring out what just happened here;
we are joining (using INNER JOIN, very important) this table to itself simpy because (just like you've said it) we have to check it twice. First for the liker (the one who liked someones profile first), than for the liked (the one who responded with a like in return) user. Having that in mind, we join this table checking liked_id from the first table on liker_id on the second table.
Which should give us joined result looking like:
liker.liker_id | liker.liked_id | liked.liker_id | liked.liked_id
-----------------------------------------------------------------
2 | 1 | 1 | 2
1 | 2 | 2 | 1
Mind you this will give us duplicates! (VERY IMPORTANT).
Having that in mind I would think about redisigning your table. For example adding boolean column named "liked_back" will give you much cheaper and cleaner queries rather than doing whatever this is...

Mahout recommender returns no results for a user

I'm curious why in the example below the Mahout recommender isn't returning a recommendation for user 1.
My input file is below. I added blank lines to enhance readability. This file will need the blank lines removed before it's run through Mahout.
The columns in this file are:
User ID | item number | item rating
1 101 0
1 102 0
1 103 5
1 104 0
2 101 4
2 102 5
2 103 4
2 104 0
3 101 0
3 102 5
3 103 5
3 104 3
You'll note that item 103 is the only common item that all 3 users rated.
I ran:
hadoop jar C:\hdp\mahout-0.9.0.2.1.3.0-1981\core\target\mahout-core-0.9.0.2.1.3.0-1981-job.jar org.apache.mahout.cf.taste.hadoop.item.RecommenderJob -s SIMILARITY_COOCCURRENCE --input small_data_set.txt --output small_data_set_output
The Mahout recommendation output file shows:
2 [104:4.5]
3 [101:5.0]
Which I believe means:
User 2 would be recommended item 104. Since user 3 rated item 104 a 3 this may account for the 4.5 recommendation score vs. the result below…
User 3 would be recommended item 101. Since user 2 rated item 101 a "4" this may account for a slightly higher recommendation score of 5.
Is this correct?
Why isn't user 1 included in the recommendation output file? User 1 could have received a recommendation for Item 102 because user 2 and user 3 rated it. Is the data set too small?
Thanks in advance.
Several mistakes may be present in your data, the first two here will cause undefined behavior:
IDs must be contiguous non-zero integers starting at 0 so you need to map your IDs above somehow. So your-user-ID = 1 will be a Mahout-user-ID = 0. The same for items, your-item-ID = 101 will be Mahout-user-ID = 0.
You should omit the 0 values from the input altogether if you mean that the user has expressed no preference, this makes the preference "undefined" in a sense. To do this omit the lines entirely.
Always use SIMILARITY_LOGLIKELIHOOD, it is widely measured as doing significantly better than the other methods unless you are trying to predict ratings, in that case use cosine.
If you use LLR similarity you should omit the values since they will be ignored.
There are very few uses for preference values unless you are trying to predict a user's rating for an item. The preference weights are useless in determining recommendation ranking, which is the typical thing to optimize. If you want to recommend the right things in the right order toss the values and use LLR.
The other thing that people sometimes do with values is show some weight of preference so 1 = a view of a product page and 5 = a product purchase. This will not work! I tried this with a large ecommerce dataset and found the recommendations were worse when adding in product views, even though there was 100 times more data. They are fundamentally different user actions with different user intent and so can't be mixed in this way.
If you really do want to mix different actions use the new multimodal recommender based on Mahout, Spark, and Solr described on the Mahout site here: It allows cross-cooccurrence type indicator calculations so you can use user location, likes and dislikes, view and purchase. Virtually the entire user clickstream can be used. But only with cross-cooccurrence correlating one action to the canonical "best" action, the one you want to recommend.

Real time data processing

I am parsing keywords several times per second. Every second i have 1000 - 5000 keywords. So i want to find outlier, growing and other stuff which called technical analysis. One of the problem is how to store data.
I will be able to do someting like:
20-01 20-02 20-03
brother 0 3 4
table 1 0 0
cup 34 54 78
But it might be a lot of keywords. For every new part of data i need to look is this word exists? If donnt then i must to add new words and add new rows for them. What is right way to organize store? Should i use key\value database, NoSQL or something else?

SSRS Tablix Sorting w/Row & Column Group

My matrix has a row group (QuestionText) and a Column Group (AnswerText).
It looks like this:
[AnswerText]
[QuestionText] [AnswerCount]
My Data looks like this:
QuestionText AnswerText AnswerCount
Question 1 Yes 2
Question 1 No 1
Question 2 Yes 3
Question 2 No 4
Which renders as follows:
Yes No
Question 1 2 1
Question 2 3 4
What I'd like to do is to sort by the Yes or No. Please note that both the questions and answers are just examples, they answers could be flag colors & red, white, blue...
Is this possible? I've seen someone say "it's hard". I'm ok with hard, I just need to know how.
http://social.msdn.microsoft.com/Forums/en/sqlreportingservices/thread/b4a242ce-e2de-463e-8732-a9f8abe493e4
Thanks for looking!
did you try just adding an order by in your sql? so you would order by QuestionText then AnswerText.
I might not be grasping this correctly, but do you just want to sort by ascending/descending # of Yes's & No's?
Try this... Go to the answertext header, and right click, then in the properties, go to the interactive sorting tab.
Select "Groups" instead of Details (radial button).
In the sort by: box, select your answer text group.
In the last section, where it says: apply this sorting to all groups and data regions in:
Select your entire matrix/tablix. Not positive, but I had a matrix and that last step was the key to sorting it...in drilldowns or groupings, etc..., sorting only works if you apply it to the whole tablix.

Resources