add new column to existing table and show it into a view blade (LARAVEL,mysql) - mysql

my goal is to display the total of the SUCCESS payments in my project in home blade.
if is it possible to do this without creating new column its ok.
step 1 :
i have a table named (payments) , i want to add a new column named (total_amount) ,
i already have the amount column so i need to put the SUM of this column to the new one (total_amount).
NOTE : i need just the success payments , so the SUM query need a condition where status = 'success'
enter image description here
step 2 :
after getting the total amount , i want to show it into home view blade .
i just want to show the total of payments in < h1 >
please i need step by step and what i should put in the controller and model... ,
i try to get the total without creating a new column but wasn't work so i think that i need to create his column first and get the value of this column, note that its just a one value not array.

You don't need to have a sum column. In general, you should avoid storing aggregates. Do something like this instead:
[Controller]
$sum = Payment::where('status', 'success')->sum('amount');
Then when you construct the view, make sure to compact sum with the other variables:
[Controller]
return view('yourtemplate', compact( 'all', 'your', 'other', 'vars', 'sum' ));
Then in the view itself, just echo out the sum wherever you want it:
[View] <h1>{{ $sum }}</h1>
EDIT: Thought of another way. If you already have the payments as a collection in that view, you can just use the collection helper:
[View] <h1>{{ $payments->where('status', 'success')->sum('amount') }}</h1>

If I understand your question, and you need the total amount of every successfully payment? If the question is yes, here is your query (assuming you have created your Payment model).
Payment::selectRaw('SUM("amount")->where('status', 'success')->first();

Related

Is there a way to sort the whole database table according to a calculated data

I have a very large table called user and it looks like this
id
events (array)
... (extra columns)
1
[]
...
2
[]
...
...
...
...
When I query the table, I will pass two extra parameters, no_per_page and page so that only the data I want will be retrieved.
And before returning the data, an extra column called 'total_event_hours' will be calculated using the 'events' column from the query. However, because it is also a column and will be presented in a table of a front-end app, I want it also to be sortable.
The naive way I could think of will be to query the whole table and sort it after the calculation, but since it is very large, it will slow down the process.
Is there a way to sort the table using the calculated column and keep the pagination at the same time? Any input will be appreciated.
Edit:
$no_per_page = $param['no_per_page']
$start = $param['page'] > 1 ? ($param['page'] - 1) * $no_per_page : 0;
$query = "SELECT * FROM user LIMIT :start, :no_per_page";
$get_query = $this->db->prepare($query);
$get_query->bindParam(':start', $start);
$get_query->bindParam(':no_per_page', $no_per_page);
$get_query->execute();
$data = $get_query->fetchAll(PDO::FETCH_ASSOC);
foreach ($data as $_data) {
$_data['total_event_hours'] = $this->_getTotalEventHours($_data['events'])
// I want to sort by this column but it is not presented in the table
}
return data;
To answer your question
Is there a way to sort the table using the calculated column and keep the pagination at the same time?
The DBMS cannot sort data by something it does not know. To sort by a calculated value, there are several possibilities, e. g.
sometimes the result of the calculation is in the same order as a column. Then you can sort by this column, of course.
you can rewrite your query to include a calculated column. That is, you must perform the calculation with SQL/a user defined function. You can then sort by the calculated column. Note that it's sometimes possible to get the same order with a simpler calculation.
approach #2 most likely prevents the DBMS from using an index for sorting. To speed things up, you could modify your table to contain the calculated column, and then update its value using triggers. If you have this column, you can also index it.
As always, there's a trade-off, so it depends on the specific circumstances/use case which option to choose.

Trouble creating nested SUM IIF expression in SSRS

I am new to SSRS and have a SUM(IIF question.
My data set contains four columns: Date, GroupID, PlanPaid, and NetworkIndicator.
Here is an example of the data set:
I am trying to SUM the [PlanPaid] amount when [NetworkIndicator] = "In Network".
However, I need this amount broken up by the [Date]. I tried accomplishing this by creating the expression:
=Sum(IIf(Fields!NetworkIndicator.Value = "In Network"
, Fields!PlanPaid.Value
, Nothing)
, "Claims_Rolling12")
But this expression returns the same amount (total) across all [Dates]. How do I break it up so that it is grouped by the correct [Date]?
Here is a photo of my Tablix and my current Groups: [Tablix and Groups]
And here is a photo of the output: [Output]
You haven't said where you want this sum to appear, so the answer here might not work. If it doesn't then edit your question to show what you expect the output to look like based on your sample data.
I'm assuming here that you want to add a new column to the report that shows "In Network total" by date.
The easiest way to do this is to add a row group that groups by date, then within this group you can use a simple expression, like the one you tried, but without specifying the scope.
=SUM(IIF(Fields!NetworkIndicator.Value = "In Network", Fields!PaidPlan.Value, Nothing))
This expression will only sum rows that are within the current scope, in this case the scope will be the row group you created to group by dates.
As IO said, if this is not helpful, edit your question and show what you expect your end result to look like, based on the sample data you supplied and then I can look at it again.

How to build a query in order to get not expired records in Yii2

I have a model in my project which has several fields like id, name, due_year, due_quarter, etc. Here is my table
The issue is that I want to get that records where the field "due_year" is greater than 2017 and at the same time, the field "due_quarter" must be less than 4. In simple words, I am trying to get unexpired records. I tried to build a query like this:
$rows = Model::find()
->andFilterWhere(['>=', 'due_year', date('Y')])
->andFilterWhere(['<', 'due_quarter', 4])
->all();
but unfortunately i got expired rows as well.
Thank you in advance

VirtueMart Custom Fields (SKU)

I'm a pretty novice programmer, but here is what I am trying to do, and what I've done so far:
What I want:
I'm trying to get a final SKU in the cart, built from the product's variant selections.
For an example, if the initial product is "Shovel", and it's SKU is "SHO", the cart variants selection SKU number adds to the end.
If one of the variants were:
Handle Color
-- Green Handle (with SKU of SKU1)
-- Yellow Handle (with SKU of SKU2)
The final SKU would be SHO - SKU1 if green were chosen, and SHO - SKU2 if yellow was chosen)
This would then create a session variable to pass onto the cart, and the cart would display this variable and I'd be able to pass it to an email notification, or anything I need after.
What I've done:
I've added a column custom_sku to the cart variant data in the table virtuemart_product_customfields. I've also added this field to the customs.php file in the administrator tables folder (/administrator/components/com_virtuemart/tables)
This field now updates into the database and each time you add a cart variant option, it adds an SKU to the column "custom_sku" for the row of the customfield_id:
Screenshot: http://awesomescreenshot.com/02e1sfsef9
I've also modified the function getProductCustomsFieldCart in customfields.php (administrator/components/com_virtuemart/models) to add this into the option drop down value.
Now a value looks like: "Variant Value - $1.00 - SKU1"
What I'm stuck on:
I need to pass this SKU value, or the value of the option over to the cart, and have it so I can use this for remaining parts of the order.
This should most likely be done with a session variable, which looks like: sessionSKUselect = "20" where 20 is the custom field ID so I can use this in a query on the cart.
This can also be output in a session variable in an array, as long as it works.
There is currently a variable passed through the form that looks like: "customPrice[0][1]" (the numbers pertain to which select list, and its variant group (which select list))
The customPrice[0][1] has a value of the customfield ID (just a number, for the database value. I need this value to be an array, or be able to use this array in the cart, to query a final SKU number.
Does anyone know what I need to do? Thanks!
I know how to utilize the cart variants. Each can be assigned a price. I have also made it so each variant option can be assigned a custom SKU (item number) as well, and the variable is in the same database table.
I've found out how to pull the variables using the function calculateModificators
Pretty much I needed the selected option values on the cart page, and this function makes them available. I've added my own little code into the above function, and now I can output the SKU but need to display it in an array.
Here is the code, I've noted where my additions are using comments.
$query = 'SELECT C.* , field.*
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
WHERE field.`virtuemart_customfield_id`=' . $selected;
$this->_db->setQuery($query);
$productCustomsPrice = $this->_db->loadObject();
$productCustomsSKU = $this->_db->loadObject(); // My Addition
echo $productCustomsSKU->custom_sku. ' + '; // My Addition
So if my two SKU numbers are this, in order of selected options (SKU3, SKU1)
I get an output like SKU3SKU1
Now I need to get this value to look like this in the end:
SKU3 + SKU1
This needs to have a + in the beginning of the string, and no + at the end. I need this to be in a value in the end, so my value will be:
$value = " + SKU3 + SKU1" and do this for every value that comes from the above array.

How to use aggregate function in aggregate function?

In SSRS I have the following code for one of my fields:
(DateDiff("n",FIRST(Fields!Date.Value), LAST(Fields!Date.Value, "grp_Order")))
I now want to take an AVG of all these fields....How would I go about doing this without getting the nested aggregate error?
Seems I remember doing this sort of thing in Crystal Reports....
Can you place the code into a field, then take the avg of that new field?
Field 1 --> code behind : (DateDiff("n",FIRST(Fields!Date.Value), LAST(Fields!Date.Value, "grp_Order")))
Field 2 --> code behind : AVG ( Field 1 ).
I would imagine that Field 2 could only exist # the end of a group in which field 1 exists.