有没有一种方法可以取两个日期之间的数字,然后用Laravel减去它



所以我一直在试图找到一种方法来获取/获取字段中两个日期之间的数字"从";以及";至";(leavetable(然后取这个数字(例如,它是7(,并用用户表中的另一个数字减去一个名为leaveBalance的字段a,它的默认数字是20,所以我希望(20-7(和结果保存在请求休假的特定用户中,在更改后的leaveBalancefield中,还可以添加一个if语句来检查日期之间的数字是否大于我们在leaveBalance上允许的数字,以返回错误消息吗

这是左表

  1. id
  2. user_id
  3. 来自
  4. 类型
  5. 说明
  6. 状态
  7. 消息

用户表有leaveBalance字段,并且这两个表没有外键关系-休假上的user_id只存储创建休假时已验证用户的id,然后它只显示用户视图上创建的id的叶子

这是请假管理员

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');
}

这是休假模式:

{
use Notifiable;
protected $guarded=[];

public function user(){
return $this->belongsTo(User::class,'user_id','id');   
}
}

这是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="">

我对在这方面使用碳持开放态度,我对碳的了解不多,但我知道它是用于日期之类的,但既然我使用了日期选择器,这可能吗?

您可以使用它来检查是否要了解他休假了多少天,您可以构建函数来执行此操作并返回结果

$from = new DateTime("2020-08-20");
$to = new DateTime("2020-08-28");
$diff = $to->diff($from)->format("%a"); // 8

然后把它传递给结果,美洲虎可以帮助你完成

<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>

这是一个可以开发的想法,这样就可以完成关于选择休假类型的ajax,并返回假期的可用天数,然后您可以通过datetimepicker函数添加这个数字,我希望我已经成功地实现了我的想法。非常感谢。

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);
}
}
}
}

例如

public function up()
{
Schema::create('types', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('days')->unsigned();
$table->timestamps();
});
}

在表格左侧添加新列名type_id

$table->foreignId('type_id')->nullable()->constrained()->onDelete('cascade');

相关内容

  • 没有找到相关文章

最新更新