What does route binding have to do with calculating the different between 2 dates? what's wrong on using Carbon?
Aug 11, 2022
3
Level 1
Route model Binding, question?
Good afternoon everyone,
I need your help, I have a project I use route model binding but I don't have any idea how able to add a value from a variable and save it with all properties. because I need to calculate the different days between on two dates.
Component Class livewire
<?php
namespace App\Http\Livewire\Logs;
use Illuminate\Support\Str;
use App\Models\Log;
use App\Models\Projectstage;
use App\Models\Sdi;
use Carbon\Carbon;
use Illuminate\Validation\Rule;
use Livewire\Component;
class LogForm extends Component
{
/** public Log $log; **/
public $sdi;
public $jobname;
public $slug;
public $projectstage;
public $takeoffby;
public $biddate;
public $notes;
/** replace this for Log $log **/
public function mount(Log $log)
{
$this->log = $log;
}
public function rules():array
{
return [
'sdi' => ['required'],
'jobname' => ['required',
Rule::unique('logs','job_name')
],
'slug' => ['required','alpha_dash',
Rule::unique('logs','slug')
],
'projectstage' => ['required'],
'takeoffby' => ['required','max:3'],
'biddate' => ['required'],
'notes' => ['nullable']
];
}
protected $validationAttributes = [
'sdi' => 'SDI',
'jobname' => 'Job Name',
'slug' => 'Slug',
'projectstage' => 'Project Stage',
'takeoffby' => 'TakeOff By',
'biddate' => 'Bid Date'
];
public function updated($propertyName)
{
$this->validateOnly($propertyName);
}
public function updatedjobname($jobname)
{
$this->slug = Str::slug($jobname);
}
public function save()
{
$this->validate();
/*** Wiht out Carbon caculate between two or more days***/
$tody =date('Y-m-d H:i'); //2022-08-04 9:55
$datetime1 =strtotime($tody);
$datetime2 = strtotime($this->biddate); //2022-08-06 9:55
$differentInDays = (int)round(($datetime2 - $datetime1)/86400);
/* dd($differentInDays);//result: 2 */
/*** Wiht Carbon using diff method ***/
/*
$to = Carbon::now();//2022-08-04 9:55
$from = Carbon::parse($this->biddate);//2022-08-06 9:55
//$days = $to->diffInDays($from);
$days = Carbon::make($to)->diffInDays($from);
dd($days, $to->toIso8601String(), $from->toIso8601String()); //result: ^ 1 ^ "2022-08-04T13:10:08-07:00" ^ "2022-08-06T13:05:00-07:00"
*/
$log = new Log;
$log->sdi = $this->sdi;
$log->job_name = $this->jobname;
$log->slug = $this->slug;
$log->project_stage = $this->projectstage;
$log->takeoff_by = $this->takeoffby;
$log->bid_date = $this->biddate;
$log->days_left = $differentInDays; //here save the between two dates
$log->notes = $this->notes;
$log->save();
/* $this->log->save(); */
session()->flash('status',__('Project save.'));
$this->redirectRoute('logs');
}
public function render()
{
$sdis = Sdi::all();
$projectstages = Projectstage::all();
return view('livewire.logs.log-form',compact('sdis','projectstages'));
}
}
View Livewire
<div>
<h1>Form New Log</h1>
<a href="{{ route('logs') }}">Regresar</a>
<form wire:submit.prevent='save'>
@csrf
<label for="">SDI:
<select wire:model="sdi" name="" id="">
<option value=""></option>
@foreach ($sdis as $sdi)
<option value="{{ $sdi->short_name }}">{{ $sdi->short_name }}</option>
@endforeach
</select>
@error('sdi')
<div class="">{{ $message }}</div>
@enderror
</label>
<br />
<label for="">Job Name:
<input wire:model="jobname" type="text" placeholder="Job Name">
@error('jobname')
<div class="">{{ $message }}</div>
@enderror
</label>
<br />
<label for="">Slug:
<input wire:model="slug" type="text" placeholder="Freandly url">
@error('slug')
<div class="">{{ $message }}</div>
@enderror
</label>
<br />
<label for="">Project Stage
<select wire:model="projectstage" name="" id="">
<option value=""></option>
@foreach ($projectstages as $projectstage)
<option value="{{ $projectstage->name_stage }}">{{ $projectstage->name_stage }}</option>
@endforeach
</select>
@error('projectstage')
<div class="">{{ $message }}</div>
@enderror
</label>
<br />
<label for="">TakeOff By:
<input wire:model="takeoffby" type="text" name="" id="" placeholder="TakeOff By">
@error('takeoffby')
<div class="">{{ $message }}</div>
@enderror
</label>
{{-- <br />
<label for="">Date Completed:
<input wire:model="datecompleted" type="datetime-local" name="" id="" >
</label>
<br />
<label for="">Estimator:
<input wire:model="estimator" type="text" name="" id="" placeholder="Estimator">
</label>
--}}
<br />
<label for="">Bid Date:
<input wire:model="biddate" type="datetime-local" name="" id="">
@error('biddate')
<div class="">{{ $message }}</div>
@enderror
</label>
<br />
<label for="">Notes:
<textarea wire:model="notes" name="" id="" cols="30" rows="10" placeholder="Notes"></textarea>
@error('notes')
<div class="">{{ $message }}</div>
@enderror
</label>
<br />
<input type="submit" value="Guardar"> <a href="{{ route('logs') }}" class="">Cancelar</a>
</form>
</div>
Level 1
Guys, I fix this with the calling name of the table
public function save()
{
$this->validate();
/*** Without Carbon calculate between two or more days***/
$tody =date('Y-m-d H:i'); //2022-08-04 9:55
$datetime1 =strtotime($tody);
$datetime2 = strtotime($this->biddate); //2022-08-06 9:55
$differentInDays = (int)round(($datetime2 - $datetime1)/86400);
/* dd($differentInDays);//result: 2 */
/*** Wiht Carbon using diff method ***/
/*
$to = Carbon::now();//2022-08-04 9:55
$from = Carbon::parse($this->biddate);//2022-08-06 9:55
//$days = $to->diffInDays($from);
$days = Carbon::make($to)->diffInDays($from);
dd($days, $to->toIso8601String(), $from->toIso8601String()); //result: ^ 1 ^ "2022-08-04T13:10:08-07:00" ^ "2022-08-06T13:05:00-07:00"
*/
$this->log->days_left = $differentInDays;//you need use this syntax, the "days_left" is name of the columm of the DB
$this->log->save();
session()->flash('status',__('Project save.'));
$this->redirectRoute('logs');
}
Please or to participate in this conversation.