计算培训时间 [英] Calculate hours for training
问题描述
我对3张桌子type_training , training & payment
有些犹豫.
I have some hesitations about 3 tables which are type_training , training & payment
.
在表type_training
中,我有一个名为price
的字段,其中包含4个数量:例如:
In the table type_training
, I have a field named price
with 4 amounts: for example:
1小时00 = 100 欧元
1 hour 00 = 100 euros
1小时30 = 150 欧元
1 hour 30 = 150 euros
2小时00 = 200 欧元
2 hour 00 = 200 euros
2小时30 = 250 欧元
2 hour 30 = 250 euros
在我的页面培训中,我为同一位学生录制了2张录音.
In my page Training , I encode 2 recordings for the same student.
学生Dujardin
为300 euros
预订了3 hours
.
在我的表单Payment
中,是否可以检索300
的金额?
In my form Payment
, is it possible to retrieve the amount of 300
?
那么,在我的模型Payment
中?我必须计算hour start
和hour end
之间的差吗?
So, in my Model Payment
? I must to calculate the difference between the hour start
and the hour end
?
我不知道该怎么办?
然后,在取回我的示例中的时差之后,我们得到了3 hours
.
如何对我的字段Total
中的2个记录求和?我试过了吗?
Then, after having retrieved the difference of hours in my example we have 3 hours
.
How to I sum my 2 recordings in my field Total
? I have tried this?
$typetraining = Typetraining::find($request->fk_typetraining);
$data = $request->all();
$data['total'] = $typetraining->price + $request->????;
Payment::create($data);
总结:
1)如何获取hour start
& hour end
,
1) How to retrieve the difference between hour start
& hour end
,
2)如何通过培训持续时间来计算金额?
2) How to calculate the amounts via the duration of my training?
有关信息,这是我的体系结构.
For information, here is my architecture.
感谢您的帮助和解释.
沃特卡姆扬19/09/2019
Watercamyan 19/09/2019
我自适应了吗?
createFromFormat('H:i', $request->get('hour_start'))
每位:
<div class="form-group{{ $errors->has('hour_start') ? 'has-error' : '' }}">
<label for="form-group-input-1">Hour start</label>
<input type="text" name="hour_start" id="hour_start" class="form-control" required="required" value="{{ old('hour_start')}}"/>
{!! $errors->first('hour_start', '<span class="help-block">:message</span>') !!}
</div>
然后,在我的模型培训中,我收到了以下错误消息:未定义的变量:typeseances
Then, in my model Training I have like error message: Undefined variable: typeseances
$start = Carbon::parse($request->get('hour_start'));
$end= Carbon::parse($request->get('hour_end'));
$mins = $end->diffInMinutes($start, true);
$hoursTraining = $mins/60;
$total = $**typeTraining**->price * $hoursTraining;
我有类似的错误消息:未定义的变量:typeseances
I have like error message: Undefined variable: typeseances
推荐答案
我认为,就像JoeGalind所说的那样,您应该认真考虑将其重新架构起来更为简单.实际上,必须调用除了价格以外就没有任何东西的 TypeTraining 对象,实际上应该将其移至 Training 对象.但是,让我来介绍一种使用您现有的代码来解决它的方法.
I think, as JoeGalind said, you should seriously consider re-archetecting this to be simpler. Having to call a TypeTraining object that has nothing but a price, should indeed be moved up to the Training object. However, let me go through a way to solve it with your existing code.
首先,正如您所说,您需要获取所请求培训的小时数.不幸的是,您需要部分小时而不是整个小时来更改价格.如果您需要整个小时,这很容易,您可以使用Carbon方法diffInHours()
.但是我们可以使用diffInMinutes()
进行计算,然后计算出不足的小时数.
First, as you said, you need to get the number of hours of the requested training. Unfortunately, you need part hours instead of whole hours to change the price. If you needed whole hours, this would be easy, you could use the Carbon method diffInHours()
. But we can do it with diffInMinutes()
, and then calculate out the partial hour.
首先,我们需要将表单中的小时数解析为Carbon对象:
First, we need to parse the hours coming in from the form into a Carbon object:
$start = Carbon::parse($request->get('hour_start'));
$end= Carbon::parse($request->get('hour_end'));
注意,我不知道它是如何从您的表单中传入的.如果上述方法不起作用,您可能需要以不同的方式解析它.像这样:
Note, I don't know how it is coming in from your form. You might need to parse it differently if the above doesn't work. Something like:
createFromFormat('H:i', $request->get('hour_start'))
或
createFromFormat('H:i:s', $request->get('hour_start'))
现在您有了一个碳物体,我们需要计算出差异,包括工时.同样,我们将使用分钟数并计算部分小时数:
Now that you've got a carbon object, we need to calculate out the difference, including the part hours. Again, we'll use the minutes and calculate for part hours:
$mins = $end->diffInMinutes($start, true);
$hoursTraining = $mins/60;
这将产生乘数(训练的小时数),例如2.0或2.5或2.25,等等.从这里开始,如果您有一个小时的基本价格(这就是我期望的 TypeTraining 模型的price
字段),很容易:
This will yield your multiplier (the number of hours training), something like 2.0 or 2.5 or 2.25, etc. From here, if you have a base price for one hour (which is what I expect is in that TypeTraining model's price
field), it is easy:
$total = $typeTraining->price * $hoursTraining;
根据您的代码设置方式,最困难的部分是您必须拉 TypeTraining 和 Training ,以便了解价格(再次-只需在培训上付出代价,使生活更轻松).
The hard part, based on the way you have your code set up, is that you must pull the TypeTraining along with the Training, in order to know the price (again - just stick the price on the training to make life easier).
要获取价格,类似这样:
To get the price, something like this:
$training = Training::with('typeTraining')->where('fk_student', $request->get('fk_student)->first();
$price = $training->typeTraining->price;
现在您已经可以插入上面的公式了.
Now you have the price to plug into the formula above.
这肯定是不正确的.而用FK对学生进行培训可能不是您想要的.如果是常规培训,或者有其他一些标识符,请使用该标识符来拉动培训以获取价格.但是您可以稍后再决定.我只能猜测其中的一些,因为我不知道会发生什么,或者您的查询需要什么,或者您的人际关系,但这应该可以给您一个思路.最重要的是,您要询问如何计算总数,以上已作进一步回答.
This is surely not exact. And pulling the training with the FK on student is probably not what you want. If it is generic training, or there is some other identifier, use that to pull the training to get the price. But you can decide that later. I can only guess at some of this, as I don't know what's coming in, or what your query needs to be, or your relationships, but this should give you an idea. Most importantly, you were asking for how to calculate total, which is answered farther above.
这篇关于计算培训时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!