I'm using Laravel for one of my project. Everything is just fine, but I can't solve a strange error.
I'm sending duration in years from my front end (Vuejs).
If client is on any active plan then plan start should be the current plan end date else set to now().
Add years in start time and set as plan end.
Now when I do this in actual code, I see same start and end date generated according to plan end.
$start = Carbon::parse($client->plan_end) ?: Carbon::now();
$end = $start->addYears($request->planDetails['duration']);
return response([
'start' => $start,
'end' => $end,
]);
This is plan_end timestamp in database -- 2022-05-09 09:15:19
This is response I receive.
{
"start" : "2024-05-09T07:15:19.000000Z",
"end" : "2024-05-09T07:15:19.000000Z"
}
So carbon is mutable which means that when you do ->addYears() it will alter the original start date time. You can use the ->copy() function before adding the years so it would look like this.
$end = $start->copy()->addYears($request->planDetails['duration']);
Using ->copy() is fine, but using CarbonImmutable as default is likely better as it will avoid similar pain to happen again:
$start = CarbonImmutable::parse($client->plan_end) ?: CarbonImmutable::now();
$end = $start->addYears($request->planDetails['duration']);
return response([
'start' => $start,
'end' => $end,
]);
Related
I am trying to draw chart for Last 30 days from Today showing total milk quantity. I get last 30 days using - [0 => "2022-02-07" ----- 30 => "2022-03-09"]
$olddate = Carbon::today()->subDays(30)->toDateString();
$todaydate = Carbon::today()->toDateString();
Also get the Data for Database as below [ date" => "2022-03-01" "totalmilk" => "24.10" "daykey" => "01"................."date" => "2022-02-16" "totalmilk" => "22.90" "daykey" => "16"]
$rahul = Buffalomilkrecord::select(DB::raw('date'), DB::raw('sum(totalmilk) as totalmilk, DATE_FORMAT(date,"%d") as "daykey"'))
->whereBetween('date', [$olddate, $todaydate] )
->groupBy(DB::raw('date'))
->orderBy('date', 'desc')
->get();
BY Below code, trying to put "0" if data is not Available
$ddtest = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
foreach($rahul as $order)
{
$ddtest[$order->daykey-1] = $order->date;
}
The code is working fine except the start date he is taking the database date which is (2022-03-01) where it should start with "2022-02-07".
I think i am making mistake while getting "daykey". It is assigning daykey "1" to first data available in database which start from date ""2022-03-01" (as No data available from 07-02-2022 to 28-02-2022 in the database and it start from 1 March 2022.) where as it should start from "$olddate" that is ""2022-02-07"..
where did i am making the mistake.....Thanks in Advance
I tried delaying each notification (or job) using Laravel queues. Still, instead of the notifications being delayed one after the other, the delay is applied to all the notifications at once. However, the delay is only applied to the first notification, and the rest are sent out simultaneously as the first message. Please, can anyone in this community help me?
This is part of the method that gets only the applicants whose admissions are accepted or rejected and then notify them individually. Unfortunately, the mail server we are using only allows sending a maximum of 30 emails per 1 minute; therefore, I need to use queuing to limit sending only (not more than) 1 email per 2 seconds.
foreach ($applications as $application)
{
$application_id = $application->id;
$admission = Admission::where('application_id', $application_id)->first();
if (empty($admission->admission_no) & $application->status === 5) {
$this->generateAdmissionNumber($application->id, $application->course_id);
$application->update(['done' => 1]);
$admissions = Admission::where('course_id', '=', $id)->get();
foreach ($admissions as $admission) {
$admission->update(['done' => 1]);
}
}
$application->done = 1;
$application->update();
$user = $application->users;
if ($user->notify_if_decision_is_made === 1)
{
$user->notify((new DecisionNotification($user))
->delay(Carbon::now()->addSeconds(2)));
}
}
I followed the guides but still have no solution. I looked at many solutions here but did not see any issues like this one.
I set QUEUE_CONNECTION=database in my .env file, added shouldQueue in the DecisionNotification file, and run php artisan queue:work from the terminal to execute the jobs. I appreciate any help.
The ->delay() only gives a delay to the running job. As your loop is running almost instantly, it won't delay put a delay of 2 seconds between each job.
One way to achieve what you want is:
foreach ($applications as $i => $application) {
// your code chunk
$user->notify((new DecisionNotification($user))->delay($i * 2)));
}
This results in delays as:
Job 1: 0s
Job 2: 2s
Job 3: 4s
Job 4: 6s
Job 5: 8s
...
By the way, ->delay() also accepts integer as second.
You can use sleep() function, which holds the loop for defined period, for example if you want to hold loop for 2 seconds, use sleep(2).
Your code should be:
foreach ($applications as $application) {
$application_id = $application->id;
$admission = Admission::where('application_id', $application_id)->first();
if (empty($admission->admission_no) & $application->status === 5) {
$this->generateAdmissionNumber($application->id, $application->course_id);
$application->update(['done' => 1]);
$admissions = Admission::where('course_id', '=', $id)->get();
foreach ($admissions as $admission) {
$admission->update(['done' => 1]);
}
}
$application->done = 1;
$application->update();
$user = $application->users;
if ($user->notify_if_decision_is_made === 1) {
$user->notify((new DecisionNotification($user))->delay(Carbon::now()->addSeconds(2)));
}
sleep(2);
}
Hope this will be useful.
Read More about sleep()
I have a laravel 5.2 project and i want to run laravel scheduler by specified time from database data, so i create schedule kernel like this :
protected function schedule( Schedule $schedule )
{
// Run proposal schedule to generate albums
$proposals = Proposal::whereStatus( true )->whereGenerated( false )->get();
foreach ( $proposals as $key => $proposal ) {
$time = date( 'i G ', strtotime( $proposal->duedate ) );
$send_date = date( $time . 'j n * Y', strtotime( $proposal->created_at . ' + ' . $proposal->timeout . ' days' ) );
$schedule->call( function() use( $proposal ) {
$proposal->generateAlbums();
} )->cron( $send_date );
}
}
And it's work fine, until i reset my migration and try to migrate from the begining, i got an error like this
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "proposals" does not exist
So i think it's not good if i place my code there, right??
where should i place my code for this??
Or it's already right and what i need just to check if database table are exists or not??
I need best solution to code just the way laravel provide..
You can check if table exists or not by
if (Schema::hasTable('proposals')) {
//Your code which needs to execute
}
I have been trying for a while for saving time in my database in timeformat like 10:00:00.
What I want is simply pick up time from timepicker and save data(time) in timeformat.
Here is what I have done:
I have used timepicker as it gets data in format 10:52 AM.
I have used accessor methods to save time as follows:
public function setRTimeAttribute($value)
{
$this->attributes['r_time'] = date('h:i:A', strtotime($value));
}
My post controller in case needed as follows:
public function postSessionReservation(Request $request,$profile_slug)
{
$restaurant = Restaurant::where('slug_profile', $profile_slug)->select('name','ar_name','slug_profile','id')->firstOrFail();
$this->validate($request,[
'no_of_seats'=>'required',
'r_date'=>'required',
'r_time'=>'required',
'restaurant_id'=>'required',
]);
$data = [
'no_of_seats'=>$request->no_of_seats,
'r_date'=>$request->r_date,
'r_time'=>$request->r_time,
'restaurant_id'=>$request->restaurant_id
];
$minutes = 1;
Session::put('reservation',json_encode($data),$minutes);
return redirect($restaurant->slug_profile.'/complete-reservation');
}
But it saves the time as 12:00:00 each time.
I do not know how 12:00:00 is generated whenever I save the time. I tried a lot but could not figure out.
Hope you guys will help me solve this.
Thanks in advance.
First remove spaces between time "10 : 52 AM" to "10:52 AM" and then change your line to this:
$this->attributes['r_time'] = date('H:i', strtotime( $value ));
This will surely help, I already tested it.
For example you can remove spaces through:
$a = '10 : 52 PM';
$x = preg_replace('/\s*:\s*/', ':', $a);
date("H:i", strtotime($x));
I have some code that uploads the CSV file to the specified folder, but it doesn't update the database.
public function do_upload()
{
$csv_path = realpath(APPPATH . '/../assets/uploads/CSV/');
$config['upload_path'] = $csv_path;
$config['allowed_types'] = '*'; // All types of files allowed
$config['overwrite'] = true; // Overwrites the existing file
$this->upload->initialize($config);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->layout->buffer('content', 'program/upload', $error);
$this->layout->render();
}
else
{
$image_data = $this->upload->data();
$fname = $image_data['file_name'];
$fpath = $image_data['file_path'].$fname;
$fh = fopen($fpath, "r");
$insert_str = 'INSERT INTO wc_program (JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish) VALUES '."\n";
if ($fh) {
// Create each set of values.
while (($csv_row = fgetcsv($fh, 2000, ',')) !== false) {
foreach ($csv_row as &$row) {
$row = strtr($row, array("'" => "\'", '"' => '\"'));
}
$insert_str .= '("'
// Implode the array and fix pesky apostrophes.
.implode('","', $csv_row)
.'"),'."\n";
}
// Remove the trailing comma.
$insert_str = rtrim($insert_str, ",\n");
// Insert all of the values at once.
$this->db->set($insert_str);
echo '<script type="text/javascript">
alert("Document successfully uploaded and saved to the database.");
location = "program/index";
</script>';
}
else {
echo '<script type="text/javascript">
alert("Sorry! Something went wrong please proceed to try again.");
location = "program/upload";
</script>';
}
}
}
When I run var_dump($fh); it shows: resource(89) of type (stream)
When I run var_dump($fpath) it shows: string(66) "/Applications/MAMP/htdocs/site/assets/uploads/CSV/wc_program.csv"
So it all uploads but what is wrong with it not updating the database?
I have tried all kinds of changing the fopen method but still no joy, I really need it to add to the database and the insert query and set query should do the trick but it doesn't.
Any help greatly appreciated!
You are not running any query on the database. You are mixing active record syntax with simple query syntax. The active record insert query will be executed by calling.
$this->db->insert('my_table');
db::set() does not actually query the database. It takes in a key/value pair that will be inserted or updated after db::insert() or db::update() is called. If you build the query yourself you need to use the db::query() function.
Review the active directory documentation.
You can use $this->db->query('put your query here'), but you lose the benefit of CodeIgniter's built in security. Review CodeIgniter's query functions.
I'll give you examples of just a few of the many ways you can insert into a database using CodeIgniter. The examples will generate the query from your comment. You will need to adjust your code accordingly.
EXAMPLE 1:
$result = $this->db
->set('JobRef', 911847)
->set('Area', 'Coastal')
->set('Parish', 'Yapton')
->set('AbbrWorkType', 'Micro')
->set('WorkType', 'Micro-Asphalt Surfacing')
->set('Timing', 'TBC')
->set('TrafficManagement', 'No Positive Traffic Management')
->set('Location', 'Canal Road (added PMI 16/07/12)')
->set('Duration', '2 days')
->set('Start', '0000-00-00')
->set('Finish', '0000-00-00')
->insert('wc_program');
echo $this->db->last_query() . "\n\n";
echo "RESULT: \n\n";
print_r($result);
EXAMPLE 2 (Using an associative array):
$row = array(
'JobRef' => 911847,
'Area' => 'Coastal',
'Parish' => 'Yapton',
'AbbrWorkType' => 'Micro',
'WorkType' => 'Micro-Asphalt Surfacing',
'Timing' => 'TBC',
'TrafficManagement' => 'No Positive Traffic Management',
'Location' => 'Canal Road (added PMI 16/07/12)',
'Duration' => '2 days',
'Start' => '0000-00-00',
'Finish' => '0000-00-00'
);
$this->db->insert('wc_program', $row);
// This will do the same thing
// $this->db->set($row);
// $this->db->insert('wc_program');
echo $this->db->last_query();
Example 1 and 2 are using the Active Record. The information is stored piece by piece and then the query is built when you make the final call. This has several advantages. It allows you to build queries dynamically without worrying about SQL syntax and order of the keywords. It also escapes your data.
EXAMPLE 3 (Simple Query):
$query = 'INSERT INTO
wc_program
(JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish)
VALUES
("911847","Coastal","Yapton","Micro","Micro-Asphalt Surfacing","TBC","No Positive Traffic Management","Canal Road (added PMI 16/07/12)","2 days","0000-00-00","0000-00-00")';
$result = $this->db->query($query);
echo $this->db->last_query() . "\n\n";
echo "RESULT: \n";
print_r($result);
This way leaves all the protection against injection up to you, can lead to more errors, and is harder to change/maintain.
If you are going to do it this way you should use the following syntax, which will protect against injection.
EXAMPLE 4:
$query = 'INSERT INTO
wc_program
(JobRef, Area, Parish, AbbrWorkType, WorkType, Timing, TrafficManagement, Location, Duration, Start, Finish)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
$row = array(
911847,
'Coastal',
'Yapton',
'Micro',
'Micro-Asphalt Surfacing',
'TBC',
'No Positive Traffic Management',
'Canal Road (added PMI 16/07/12)',
'2 days',
'0000-00-00',
'0000-00-00'
);
$result = $this->db->query($query, $row);
echo $this->db->last_query() . "\n\n";
echo "RESULT: \n";
print_r($result);
CodeIgniter will replace each "?" in the query with the corresponding value from the array after it is escaped. You can use this to run many queries that are of the same form, but have different data just by updating the $row array and benefit from CI's built in security.