Reputation: 154
So I've been trying to find a way to get/take the number in between two dates in fields "from" and "to"(leave table) then take that number(ex. it's 7) and subtract it with another number from the user table a from a field called leaveBalance it has a default number of 20 so I want that ( 20 -7) and the result to be saved in that specific user who requested the leave, in that leaveBalance field after that is changed, also would it be possible to add an if statement to check if the number between dates is bigger than the number allowed that we have on the leaveBalance to just return an error message
This is the leave table
The user table has the leaveBalance field and the two tables don't have a foreign key relation the user_id on the leave only stores the id of that authenticated user when a leave is created and then it only displays the leaves of that id created on the user's view
This is the Leave Controller
public function create()
{
$leaves = Leave::latest()->where('user_id',auth()->user()->id)->paginate(5);
return view('leave.create',compact('leaves'));
}
public function store(Request $request)
{
$this->validate($request,[
'from'=>'required',
'to'=>'required',
'description'=>'required',
'type'=>'required'
]);
$data=$request->all();
$data['user_id']=auth()->user()->id;
$data['message']='';
$data['status']=0;
$leave =Leave::create($data);
$admins = Admin::all();
$users = User::where('role_id', 2)->get();
foreach ($admins as $admins) {
foreach($users as $users){
$admins->notify(new LeaveSent($leave));
$users->notify((new LeaveSent($leave)));
}
}
return redirect()->back()->with('message','Leave Created');
}
This is the Leave Model:
{
use Notifiable;
protected $guarded=[];
public function user(){
return $this->belongsTo(User::class,'user_id','id');
}
}
This is the view of the Leave
<div class="card-body">
<form method="POST" action="{{route('leaves.store')}}">
@csrf
<div class="form-group">
<label>From Date</label>
<div class="col-md-6">
<input class="datepicker" type="text" class="form-control @error('from') is-invalid @enderror" name="from" required="">
@error('from')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group">
<label>To Date</label>
<div class="col-md-6">
<input class="datepicker1" type="text" class="form-control @error('to') is-invalid @enderror" name="to" required="">
I'm open to using carbon in this I don't really know much on carbon but I am aware that it's used for dates and such but since I use date picker is that possible?
Upvotes: 0
Views: 161
Reputation: 580
For example
public function up()
{
Schema::create('types', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('days')->unsigned();
$table->timestamps();
});
}
in table leaves add new column name type_id
$table->foreignId('type_id')->nullable()->constrained()->onDelete('cascade');
Upvotes: 1
Reputation: 580
This is an idea that can be developed so that the ajax about choosing the type of leave is done and it returns the available days for the vacation and then you can add this number through the datetimepicker functions, I hope that I have succeeded in bringing my idea closer. Thank you.
public function getUserLeave(Request $request)
{
if ($request->ajax()) {
$type = $request->type;
$user_id = $request->user_id;
$leaves = Leave::where('user_id', $user_id)->where('type', $type)->where(function ($q) {
$date = Carbon::now();
$q->where('from', '<=', date('Y-01-01', strtotime($date))); // 2020-01-01
$q->where('to', '>=', date('Y-m-d', strtotime($date))); // 2020-08-27
})->get();
if (count($leaves) > 0) {
$dateLeave = array();
foreach ($leaves as $leave) {
$leave_from = $leave->from;
$leave_to = $leave->to;
$diff = $leave_to->diff($leave_from)->format("%a");
$dateLeave[] = $diff;
}
$dateLeaveSum = array_sum($dateLeave);
// add type of leave
$annual_leave = 30;
$sick_leave = 5;
if ($type === 'annual_leave'){
$value = ($annual_leave - $dateLeaveSum);
return response()->json($value);
}
if ($type === 'sick_leave'){
$value = ($sick_leave - $dateLeaveSum);
return response()->json($value);
}
}
}
}
Upvotes: 1
Reputation: 580
you can use this to checking if To find out how many days he took a vacation and you can build function to do that and back results
$from = new DateTime("2020-08-20");
$to = new DateTime("2020-08-28");
$diff = $to->diff($from)->format("%a"); // 8
Then pass it on to the result, and the jaguar can help you with that
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.0.0-alpha14/css/tempusdominus-bootstrap-4.min.css" />
<div class="container">
<div class='col-md-5'>
<div class="form-group">
<div class="input-group date" id="datetimepicker7" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker7"/>
<div class="input-group-append" data-target="#datetimepicker7" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
</div>
<div class='col-md-5'>
<div class="form-group">
<div class="input-group date" id="datetimepicker8" data-target-input="nearest">
<input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker8"/>
<div class="input-group-append" data-target="#datetimepicker8" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js" integrity="sha512-rmZcZsyhe0/MAjquhTgiUcb4d9knaFc7b5xAfju483gbEXTkeJRUMIPk6s3ySZMYUHEcjKbjLjyddGWMrNEvZg==" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/tempusdominus-bootstrap-4/5.0.0-alpha14/js/tempusdominus-bootstrap-4.min.js"></script>
<script type="text/javascript">
$(function () {
$('#datetimepicker7').datetimepicker({
format: 'DD-MM-YYYY',
minDate: moment.now(),
});
$('#datetimepicker8').datetimepicker({
useCurrent: false,
format: 'DD-MM-YYYY'
});
$("#datetimepicker7").on("change.datetimepicker", function (e) {
var addNewDay = moment(e.date, "DD-MM-YYYY").add(1, 'd').format('DD-MM-YYYY');
console.log(addNewDay);
$('#datetimepicker8').datetimepicker('minDate', addNewDay);
});
$("#datetimepicker8").on("change.datetimepicker", function (e) {
$('#datetimepicker7').datetimepicker('maxDate', e.date);
});
});
</script>
Upvotes: 1