Reputation: 53
I want to grab all the timestamps of a table, compare everything with the current date and all those that are less than the current date, add one day to their respective timestamp and in this way always have the dates updated to the current day or in case the final date will be passed, it will be established tomorrow.
Command:
<?php
namespace App\Console\Commands;
use App\pamatrixinf;
use App\Periodicity;
use App\Mail\SendMailable;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
class Cleanminute extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'E:LD';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Every minute';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$datedi = Periodicity::select('pamatrixinf.dateen')
->where('pamatrixinf.cod_paperiodicity', '=', 1)
->get();
$now = Carbon::now();
if ($datedi<$now) {
pamatrixinf::where('read_at', 1)
->where('cod_paperiodicity', 1)
->update(['read_at' => 0]);
pamatrixinf::where('dateen','<', $now)
->update(['dateen' => 'dateen'->addDay()]);
}
}
}
As you can see in my table I have the read_at fields to know if it was seen or not (for another function) and dateen that serves to tell me the deadline of each job and with which I want to compare my $ now variable, I hope you can help me. Since I run the command without any error but nothing happens. So that more or less they know what I want to happen I leave an example.
I still don't execute the command:
id dateen
1 2020-06-03 14:00:00.000000
2 2020-07-05 14:00:00.000000
3 2020-06-07 14:00:00.000000
I execute the command:
id dateen
1 2020-06-04 14:00:00.000000
2 2020-07-05 14:00:00.000000
3 2020-06-08 14:00:00.000000
Thanks for the help
Upvotes: 0
Views: 77
Reputation: 906
since you're using get() the return value is a collection so you need to loop through the collection with a foreach loop, if you have a single record in the table you can use first() and you no longer need to use the foreach loop:
example:
public function handle()
{
$dates = Periodicity::select('pamatrixinf.dateen')
->where('pamatrixinf.cod_paperiodicity', '=', 1)
->get();
$now = Carbon::now();
foreach($dateas as $datedi){
if ($datedi->dateen < $now) {
pamatrixinf::where('read_at', 1)
->where('cod_paperiodicity', 1)
->update(['read_at' => 0]);
pamatrixinf::where('dateen','<', $now)
->update(['dateen' => 'dateen'->addDay()]);
}
}
}
with first
public function handle()
{
$datedi = Periodicity::select('pamatrixinf.dateen')
->where('pamatrixinf.cod_paperiodicity', '=', 1)
->first();
$now = Carbon::now();
if ($datedi->dateen < $now) {
pamatrixinf::where('read_at', 1)
->where('cod_paperiodicity', 1)
->update(['read_at' => 0]);
pamatrixinf::where('dateen','<', $now)
->update(['dateen' => 'dateen'->addDay()]);
}
}
Upvotes: 2