Invalid Datetime format: 1366 Incorrect integer value LARAVEL - laravel

I'm trying to update or insert multiple data when a post approve. Insert condition is working but update function giving error.
(SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer
value: '[10000,10000]' for column
parijattech_web_app.palika_to_school_kriyakalap.cash at row 3 update
palika_to_school_kriyakalap SET cash = [ 10000, 10000 ],
palika_to_school_kriyakalap.updated_at = 2022 -12 -26 04: 58: 52 WHERE
kriya_name IN (SALARY, POSHAK) AND school_id IN (24, 24))
My code is so messy. So, how can i improve my coding skill?
Here is my Controller
public function approve(Request $request, $biniyojan_id)
{
$upudate = DB::table('biniyojan')
->where('biniyojan_id', $biniyojan_id)
->update(['biniyojan_status' => 1]);
if ($upudate) {
$budget_data = BiniyojanDetails::where('biniyojan_id', $biniyojan_id)->where('biniyojan_id', $biniyojan_id)->get();
$kriya = Palika_ToSchool_Kriyakalap::whereIn('kriya_name', $budget_data->pluck('kriyakalap'))
->whereIn('school_id', $budget_data->pluck('school_id'))
->get();
$budget_cash = BiniyojanDetails::where('biniyojan_id', $biniyojan_id)
->whereIn('school_id', $kriya->pluck('school_id'))
->whereIn('kriyakalap', $kriya->pluck('kriya_name'))
->get();
$updated_bini = $budget_cash->pluck('cash')->toArray();
$updated_kriya_cash = $kriya->pluck('cash')->toArray();
$newArray = array_map(function () {
return array_sum(func_get_args());
}, $updated_bini, $updated_kriya_cash);
if ($kriya->pluck('school_id') == $budget_data->pluck('school_id')) {
Palika_ToSchool_Kriyakalap::whereIn('kriya_name', $budget_data->pluck('kriyakalap'))
->whereIn('school_id', $budget_data->pluck('school_id'))
->update(['cash' => $newArray]);
} else {
foreach ($budget_data as $bud) {
$data = [
'school_id' => $bud->school_id,
'kriya_name' => $bud->kriyakalap,
'cash' => $bud->cash,
];
Palika_ToSchool_Kriyakalap::where('kriya_name' != $bud->kriyakalap)
->insert($data);
}
}
return redirect()->back()->with('status', 'Approved!!!');
}
else{
return redirect()->back()->with('error', 'Error!!!');
}
}

Related

laravel excel import update only if there is a change

i have laravel excel maatwebsite import update function, it works really well but i want to mark record that change by add "correction_flag" variable. the question is, how can i set this "correction_flag" when import.
this is my import function:
public function collection(Collection $rows)
{
foreach ($rows as $row)
{
$tgl_lahir_cell = $this->transformDate($row['tanggal_lahir']);
$tgl_awal_masa_kerja_cell = $this->transformDate($row['tanggal_awal_masa_kerja']);
$tgl_menjadi_permanen_cell = $this->transformDate($row['tanggal_menjadi_permanen']);
$tgl_keluar_cell = $this->transformDate($row['tanggal_keluar']);
if($tgl_lahir_cell == '1970-01-01') {
$formatedDate1 = NULL;
}else{
$formatedDate1 = $tgl_lahir_cell;
}
if($tgl_awal_masa_kerja_cell == '1970-01-01') {
$formatedDate2 = NULL;
}else{
$formatedDate2 = $tgl_awal_masa_kerja_cell;
}
if($tgl_menjadi_permanen_cell == '1970-01-01') {
$formatedDate3 = NULL;
}else{
$formatedDate3 = $tgl_menjadi_permanen_cell;
}
if($tgl_keluar_cell == '1970-01-01') {
$formatedDate4 = NULL;
}else{
$formatedDate4 = $tgl_keluar_cell;
}
Tempdat::where('cc', Auth::user()->ccode)
->where('id_tempdat', $row['id_tempdat'])
->update([
'golongan' => $row['golongan'],
'nama' => $row['nama'],
'jenis_kelamin' => $row['jenis_kelamin'],
'tgl_lahir' => $formatedDate1,
'tgl_awal_masa_kerja' => $formatedDate2,
'tgl_menjadi_permanen' => $formatedDate3,
'status_awal' => $row['status_di_awal_periode'],
'gaji_pokok_awal' => $row['gaji_pokok_di_awal_periode'],
'tunjangan_tetap_awal' => $row['tunjangan_tetap_di_awal_periode'],
'total_upah_awal' => $row['total_upah_di_awal_periode'],
'status_akhir' => $row['status_di_akhir_periode'],
'tgl_keluar' => $formatedDate4,
'status2_akhir' => $row['keterangan_keluar'],
'gaji_pokok_akhir' => $row['gaji_pokok_di_akhir_periode'],
'tunjangan_tetap_akhir' => $row['tunjangan_tetap_di_akhir_periode'],
'total_upah_akhir' => $row['total_upah_di_akhir_periode'],
'jumlah_pesangon_paid_awal' => $row['pesangon_dibayarkan_pada_periode'],
'correction_flag' => 'CORRECTED'
]);
}
}
public function headingRow(): int
{
return 4;
}
public function startRow(): int
{
return 5;
}
i already tried to put correction_flag like the example above but its just make all record uploaded marked CORRECTED eventhou there is no changes happend.
You need to first() the query, because laravel model event listeners won't work if there's mass update/create. So what you need to do is:
$tempdat = Tempdat::where('cc', Auth::user()->ccode)->where('id_tempdat', $row['id_tempdat'])->first();
if($tempdat){
$tempdat->update([
...
]);
}
And add this function to your Temptdat model:
protected static function booted()
{
parent::boot();
self::saving(function ($model) {
if ($model->isDirty()) {
$model->correction_flag = 'CORRECTED';
}
});
}

Laravel 7: Full Calendar not showing events

Please see my code first:
Controller:
public function getHolidays($days, $start_date) {
$date = date('Y-m-d',strtotime($start_date));
$i = 0;
$response = array();
while ($i <= 42) {
$tsDate = strtotime($date. ' ' .'+ '.$i.' days');
$day = date('D', $tsDate);
if(in_array($day, $days)) {
$data = array();
$data['title'] = 'Weekend';
$data['start'] = date('Y-m-d',$tsDate);
$data['end'] = date('Y-m-d',$tsDate);
$response[] = $data;
}
$i++;
}
return $response;
}
public function index(request $request){
if($request->ajax()) {
$weekends = DB::table('working_days')
->where('status', 0)
->get(['day'])
->map(function($item) {
return $item->day;
})->toArray();
$holidays = DB::table('holidays')
->get()
->map(function($item) {
return [
'title' => 'Holiday',
'start' => Carbon::parse($item->date)->format('Y-m-d'),
'end' => Carbon::parse($item->date)->format('Y-m-d'),
];
})->toArray();
$attendances = DB::table('attendances as a')
// ->where('leave_cat_id', '!=', null)
->where('user_id', Auth::user()->id)
->get()
->map(function($item) {
return [
'title' => 'Leave',
'start' => Carbon::parse($item->date)->format('Y-m-d'),
'end' => Carbon::parse($item->date)->format('Y-m-d'),
'color' => 'red',
];
})->toArray();
$data = [...$this->getHolidays($weekends, $request->start), ...$holidays, ...$attendances];
return response()->json($data);
}
return view('admin.dashboard');
}
Blade File script:
<script>
$(document).ready(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var calendar = $('#full-calendar').fullCalendar({
editable: true,
editable: true,
events: '/dashboard',
eventRender: function (event, element, view) {
if (event.allDay === 'true') {
event.allDay = true;
} else {
event.allDay = false;
}
},
});
});
</script>
Tables:
attendances
id
user_id
date
status
leave_cat_id
1
2
2022-07-13
1
null
2
2
2022-07-12
0
1
holidays
id
date
1
2022-07-13
2
2022-07-20
working_days
id
day
working_status
1
Fri
0
2
Sat
1
3
Sun
1
I want to show attendances on Full Calendar [https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.2/fullcalendar.min.js]
First, weekly holiday will show when the working_status = 0 and day is matched with calendar.
Second, holidays will show when it will match the dates with calendar date
Third, attendance will show on other days. If the status is 1 it will show present and if 0 it will show absent. Also, if the leave_cat_id column is not equal to null, then it will show On leave, others it will show the attendance status.
Can you please modify my code for this?
Thanks
public function getHolidays($days, $start_date) {
$date = date('Y-m-d',strtotime($start_date));
$i = 0;
$response = array();
while ($i <= 42) {
$tsDate = strtotime($date. ' ' .'+ '.$i.' days');
$day = date('D', $tsDate);
if(in_array($day, $days)) {
$data = array();
$data['title'] = 'Weekend';
$data['start'] = date('Y-m-d',$tsDate);
$data['end'] = date('Y-m-d',$tsDate);
$response[] = $data;
}
$i++;
}
return $response;
}
public function index(request $request){
if($request->ajax()) {
$weekends = DB::table('working_days')
->where('working_status''status', 0)
->get(['day'])
->map(function($item) {
return $item->day;
})->toArray();
$holiday$holidays = DB::table('holidays')
->get()
->map(function($item) {
return [
'title' => 'Holiday',
'start' => Carbon::parse($item->date)->format('Y-m-d'),
'end' => Carbon::parse($item->date)->format('Y-m-d'),
];
})->toArray();
$attendances = DB::table('attendances''attendances as a')
// ->where('leave_cat_id', '!=', null)
->where('user_id', Auth::user()->id)
->get()
->map(function($item) {
return [
'title' => $item->leave_cat_id ? 'Leave' : ($item->attendance_status == 1 ? 'Present' : 'Absent'),
'start' => Carbon::parse($item->attendance_date>date)->format('Y-m-d'),
'end' => Carbon::parse($item->attendance_date>date)->format('Y-m-d'),
'color' => $item->attendance_status == 1 ? 'green' : 'red',
];
})->toArray();
$data = [...$this->getHolidays($weekends, $request->start), ...$holiday$holidays, ...$attendances];
return response()->json($data);
}
return view('admin.dashboard');
}
<script type="text/javascript"><script>
$(document).ready(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var calendar = $('#full-calendar').fullCalendar({
editable: true,
editable: true,
events: '/dashboard',
eventRender: function (event, element, view) {
if (event.allDay === 'true') {
event.allDay = true;
} else {
event.allDay = false;
}
},
});
});
</script>

Why value is not getting inserted in table in codeigniter

In controller
Login information is added inside an array and passed to model. Why it is not inserted in table.
$loginInfo = [
'agent' => $this->getUserAgentInfo(),
'ip' => $this->request->getIPAddress(),
'logintime' => date('Y-m-d h:i:s'),
];
$data=$model->saveLoginInfo($loginInfo);
...
...
public function getUserAgentInfo()
{
$agent = $this->request->getUserAgent();
if ($agent->isBrowser())
{
$currentAgent = $agent->getBrowser();
}
elseif ($agent->isRobot())
{
$currentAgent = $this->agent->robot();
}
elseif ($agent->isMobile())
{
$currentAgent = $agent->getMobile();
}
else{
$currentAgent = 'Undefined User Agent';
}
return $currentAgent;
}
In Model
public function saveLoginInfo($data){
$this->db->table('login_activity')->set($data)->insert();
}

How to update row which have unique id in Codeigniter

I have a table which has a field of customer_number, some numbers repeat many times but I want to get only that number which is unique in filed.
public function set_unique()
{
$e = $_POST['e'];
if($e == 1)
{
$one = $this->db->get('calls_details');
foreach($one->result() as $data) {
$two = $this->db->where('Customer_Number', $data->Customer_Number)
->get('calls_details');
if($two->num_rows() == 1) {
foreach($two->result() as $data2) {
$three = $this->db->where('Customer_Number', $data2->Customer_Number)
->update('calls_details', ['unique_id'=>1]);
}
}
}
}
}
}
// defining array in which we get form values
$dataDB = ['first_name' => $this->input->post('first_name'),
'second_name' => $this->input->post('second_name'),
'email' => $this->input->post('email'),
];
// load model
$this->load->model('Customer');
//set record id, which we need to be updated
$this->Customer->id = $uniqueCustomerIDNumber;
//save record
$this->Customer->save();

Laravel: How to insert multiple records

I want to insert multiple records in my database using Laravel, however when i insert it, it only gives me one record in the database
Here's my Controller
public function postCreateAttendance()
{
$validate = Validator::make(Input::all(), array(
'status' => 'required'
));
if ($validate->fails())
{
return Redirect::route('viewStudent')->withErrors($validate)->withInput();
}
else
{
foreach(User::all() as $user):
foreach(User::whereRaw('isTeacher = "0" and isAdmin = "0"')->get() as $student)
foreach(User::whereRaw('isTeacher = "1" and isAdmin = "0"')->get() as $teacher)
//$users[$user->id]=$user->firstname;
$attendance = new Attendance();
$attendance->status = Input::get('status');
$attendance->comment = Input::get('comment');
$attendance->student_id = $student->id=$student->id;
$attendance->student_firstname = $student->id=$student->firstname;
$attendance->student_lastname = $student->id=$student->lastname;
$attendance->teacher_id = $teacher->id=$teacher->id;
$attendance->teacher_firstname = $teacher->id=$teacher->firstname;
$attendance->teacher_lastname = $teacher->id=$teacher->lastname;
if($attendance->save())
{
return Redirect::route('viewStudent')->with('success', 'ATTENDANCE HAS BEEN RECORDED!');
}
else
{
return Redirect::route('viewStudent')->with('fail', 'An error occured while creating the attendance.');
}
endforeach;
}
}
How can i save multiple records? Please help Thank You ^_^
The issue is that you are returning during the foreach loop - so only one record is processed. You need to process all the records, then return the route.
Here is some refactoring of your code
public function postCreateAttendance()
{
$validate = Validator::make(Input::all(), array(
'status' => 'required'
));
if ($validate->fails()) {
return Redirect::route('viewStudent')->withErrors($validate)->withInput();
}
foreach(User::where('isTeacher', '0')->where('isAdmin', '0')->get() as $student) {
foreach(User::where('isTeacher', '1')->where('isAdmin', '0')->get() as $teacher) {
$attendance = new Attendance();
$attendance->status = Input::get('status');
$attendance->comment = Input::get('comment');
$attendance->student_id = $student->id;
$attendance->student_firstname = $student->firstname;
$attendance->student_lastname = $student->lastname;
$attendance->teacher_id = $teacher->id;
$attendance->teacher_firstname = $teacher->firstname;
$attendance->teacher_lastname = $teacher->lastname;
$attendance->save();
}
}
return Redirect::route('viewStudent')->with('success', 'ATTENDANCE HAS BEEN RECORDED!');
}
Edit: I've removed the first foreach(User::all() as $user): - because at the moment, in your code, it does nothing...?

Resources