Laravel update column for all users based on old value - laravel

I have days column in my users table, I need to run a cron job command using Laravel to decrease 1 from the users days column.
I know that I can use it to update all rows at once:
DB::table('Users')->update(['column' => 'value']);
But how I can set value to each member days - 1 ?

Use
DB::table('Users')->decrement('days', 1);
Or,
Since you want to decrement by 1, you can skip value part.
DB::table('Users')->decrement('days');
Find details here https://laravel.com/docs/5.3/queries#increment-and-decrement

You can update by: DB::table('Users')->update(['column' => DB::raw('value + 7')]);
Of course if you don't want to decrement or increment!

Related

How to subtract one minute from column in mysql database in laravel?

I am Trying to subtract 1 minute from duration column through update query but it is not working. Duration field is of type time.
I try to use minus sign with new value but it is not working.
public function index()
{
$current = Carbon::now('Asia/Karachi');
Sale::where('date','=',$current->toDateString())
->where('time','<=',$current->toTimeString())
->update(['duration' => '- 00:01:00']);
}
I want to subtract one minute with help of this query.
Can you use DATE_SUB?
Something like
$object->update(['duration' => DB::raw('DATE_SUB(duration, INTERVAL 1 MINUTE)')])
I don't have a laravel instance in front of my, but that's how I'd write the SQL query and I think that's right based off my laravel memory.

Update all the values of counts to add 2.822 between dates only using DAX

I have a table with a column of values that I would like to update by adding 2.822 of between dates and need help writing this code.
Notes: ColumnName2 adds 2.822 everything, but I want it to add only between the dates. ColumnName1 is the original values column.
It adds 2.822 to all dates, but I only want it to add to what is in between dates, regardless of blank values.
Code:
AddToColumnsBetweenDates =
CALCULATE(SUM(TableName[ColumnName1)+TableName[ColumnName2],
FILTER(TableName,
DATEDIFF(DATE(2 018,01,01), DATE(2018,12,31),DAY
Another one that I tried:
AddToColumnsBetweenDates =
CALCULATE(SUM(TableName[ColumnName1),
FILTER(TableName,
SUM(TableName[Column1])+TableName
[ColumnName2]&&
DATEDIFF(DATE(2018,01,01) < TableName[ColumnName1]=BLANK(),
DATE(2018,12,31),DAY) && TableName[ColumnName1]>=BLANK()))
I figured it out (Add1030ToEachNodeBefore2019 is the 2.822 variable), hence this adds to all the nodes that exist in 2018 only:
AddToNodesBetween2018StartAnd2018End = IF(Table[DateColumn.[Year]=2018, CALCULATE( SUMX(Table, Table[NodeCount]+Table[Add1030ToEachNodeBefore2019]), (DATESBETWEEN(Table[DateColumn].[Date], DATE(2018,01,01), DATE(2018,12,31)))), Table[NodeCount])

Laravel - Check if value is equal to any record in collection

I have a collection of records.
$pastSessions = Sessions::where('id', $id)->pluck('event_start_time');
This has 12 records.
I'm trying to check if a variable is equal to any of these records, but it's only checking the last one.
if($application->appointment == $pastSessions)
How do I check is it equal to any of the records.
This is all in my controller
You can use contains with name of key and value like this.
$pastSessions->contains('appointment', $application->appointment)

Propel, selecting just one column

I am trying to run a query in propel that runs an aggregate function (SUM).
My Code
$itemQuery = SomeEntity::Create();
$itemQuery->withColumn('SUM(SomeColumn)', someColumn)
->groupBy(SomeForeignKey);
Problem
It should theoretically return the sum of every group of items but the problem is propel tries to fetch all columns, and also appends a bunch of other columns to the group by clause. This results in an unexpected categorisation and therefore the sum is incorrect.
Is there anyway to make propel fetch just the column I am running the aggregation function on so that the group by statement works as well?
You need to add a select statement for the column and the foreign key:
$itemQuery = SomeEntity::Create();
$itemQuery->select(array(SomeColumn, SomeForeignKey));
$itemQuery->withColumn('SUM(SomeColumn)', someColumn);
$itemQuery->groupBy(SomeForeignKey);

SOQL - single row per each group

I have the following SOQL query to display List of ABCs in my Page block table.
Public List<ABC__c> getABC(){
List<ABC__c> ListABC = [Select WB1__c, WB2__c, WB3__c, Number, tentative__c, Actual__c, PrepTime__c, Forecast__c from ABC__c ORDER BY WB3__c];
return ListABC;
}
As you can see in the above image, WB3 has number of records for A, B and C. But I want to display only 1 record for each WB3 group based on Actual__c. Only latest Actual__c must be displayed for each WB3 Group.
i.e., Ideally I want to display only 3 rows(one each for A,B,C) in this example.
For this, I have used GROUPBY and displayed the result using AggregateResults. Here is the result.
I got the Latest Actual Date for each WB3 as shown above. But the Tentative date is not corresponding to it. The Tentative Date is also the MAX in the list.
Here is the code I used
public List<SiteMonitoringOverview> getSPM(){
AggregateResult[] AgR = [Select WB_3__c, MAX(Tentaive_Date__c) dtTentativeDate , MAX(Actual_Date__c) LatestCDate FROM Site_progress_Monitoring__c GROUP BY WBS_3__c];
if(AgR.size()>0){
for(AggregateResult SalesList : AgR){
CustSumList.add(new SiteMonitoringOverview(String.ValueOf(SalesList.ge​t('WB_3__c')), String.valueOf(SalesList.get('dtTentativeDate')), String.valueOF(SalesList.get('LatestCDate')) ));
}
}
return CustSumList;
}
I am forced to use MAX() for tentative date. I want the corresponding Tentative date of the MAX Actual Date. Not the Max Tentative Date.
For group A, the Tentative Date of Max Actual Date is 12/09/2012. But it is displaying the MAX tentative date: 27/02/2013. It should display 12/09/2012. This is because I am using MAX(Tentative_Date__c) in my code. Every column in the SOQL query must be either GROUPED or AGGREGATED. That's weird.
How do I get the required 3 rows in this example?
Any suggestions? Any different approach (looping within in groups)? how?
Just ran into this issue myself. The solution I came up with only works if you want the oldest or newest record from each grouping. Unfortunately it probably won't work in your case. I'll still leave this here incase it does happen to help someone searching for a solution to this issue.
AggregateResult[] groupedResults = [Select Max(Id), WBS_3__c FROM Site_progress_Monitoring__c GROUP BY WBS_3__c];
Calling MAX or MIN on the Id will let you get 1 record per group condition. You can then query other information. I my case I just need 1 record from each group and didn't really care which one it was.

Resources