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

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?

Related

Creating advanced SUMIF() calculations in Quicksight

I have a couple of joined Athena tables in Quicksight. The data looks something like this:
Ans_Count | ID | Alias
10 | 1 | A
10 | 1 | B
10 | 1 | C
20 | 2 | D
20 | 2 | E
20 | 2 | F
I want to create a calculated field such that it sums the Ans_Count column based on distinct IDs only. i.e., in the example above the result should be 30.
How do I do that?? Thanks!
Are you looking for the sum before or after applying a filter?
Sumif(Ans_Count,ID) may be what your looking for.
If you need to always return the result of the sum, regardless of the filter on the visual, look at the sumOver() function.
You can use distinctCountOver at PRE_AGG level to count unique number of values for a given partition. You could use that count to drive the sumIf condition as well.
Example : distinctCountOver(operand, [partition fields], PRE_AGG)
More details about what will be visual's group by specification and an example where there duplicate IDs will help give a specific solution.
It might even be as simple as minOver(Ans_Count, [ID], PRE_AGG) and using SUM aggregation on top of it in the visual.
If you want another column with the values repeated, use sumOver(Ans_Count, [ID], PRE_AGG). Or, if you want to aggregate via QuickSight, you would use sumOver(sum(Ans_Count), [ID]).
I agree with the above suggestions to use sumOver(sum(Ans_Count), [ID]).
I have yet to understand the use cases for pre_agg, so if anyone has concrete examples please share them!
Another suggestion would be to do a sumover + partition by in your table (if possible) before uploading the dataset, then checking if the results matche with Quicksight's aggregations. I find Quicksight can be tricky with calculated fields, aggregations, and nested ifs so I've been doing calculations in SQL where possible before bringing it in to quicksight to have a better grasp of what the outputs should look like. This obviously is an extra step, but can help in understanding how quicksight pulls off calcs and brings up figures (as the documentation doesn't always give much), and spotting things that don't look right (I've had a few) before you share your analysis with a wider group.

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

Set Order for a Table (Laravel)

I using laravel 5.1 and PostgreSQL.
I have data product, and I want to custom the order by myself.
For example, I have product
{A, B, C, D, E, F}
and I want to show them in a table by what I want
Idx | Item
-----------
1 | A
2 | C
3 | E
4 | B
5 | F
6 | D
To do this I need a page that can setting it right?
My problem is I don't know what page must I develop to create setting order.
Maybe a page that has a table that I can move each row to set the order,
and a database field in item table to save the order setting?
Please someone help me, is there a plugin like that?
You can just create a page that list all items with minimal amount of information. Then you change the order use something like jQuery UI sortable or Dargsort to sort them, and then save the index of the list back to your database as custom index
But keep in mind 100 items is still too many, it would be much better to just provide filter of all attributes to display items in different order.

How can I count the nested replies?

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.

CodeIgniter Active Record - return a non-associative array as result

Is there a way to return a non-associative array as a result of a CI db query?
Say, I have this table:
id | Name
1 | Name1
2 | Name2
3 | Name3
4 | Name4
I want the names to be returned as a non-associative array.
$this->db->select('tablename')->result_array() returns an associative array.
Is this even possible in CI? I don't want to have to add an extra foreach statement if it is.
Please shed some light.
You could just use array_values(), which returns an array containing the values of your associative array.
There is no built in function for this (or at least i have never seen any), I would suggest using another function for this using your foreach loop method.
There may be other ways I would suggest looking on the CI forums, im sure some else has had this problem before.
Sorry i can't be anymore help.

Resources