问题描述
我必须在我的TrainingController
中添加两个变量=> start_time
& stop_time
.
I have to add in my TrainingController
two variables => start_time
& stop_time
.
在我的旧代码中,我有这个:
In my old code I had this:
$conflictTraining = Training::where('fk_motorbike', $request->get('fk_motorbike'))
->whereDate('date_seance', "=" , Carbon::parse($date_seance))
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->where('fk_former', $request->get('fk_former'))
->first();
我的问题是我想进行检查.如何避免使用start_time
&重复我的请求$conflictTraining
stop_time
..
My problem is that I would like to do a checking. How Can I avoid a duplicate for my request $conflictTraining
with start_time
& stop_time
..
这是我现在的代码:
public function store(Request $request)
{
$request->validate([
'date_seance' => 'required',
'hour_start' => 'required',
'hour_end' => 'required',
'fk_motorbike' => 'required',
'fk_former' => 'required',
'fk_student' => 'required',
'fk_typeseance' => 'required'
]);
$start_time = Carbon::createFromFormat('d-m-Y H:s', $date_seance . ' ' . $hour_start);
$stop_time = Carbon::createFromFormat('d-m-Y H:s', $date_seance . ' ' . $hour_end);
$conflictTraining = Training::where('fk_motorbike', $request->fk_motorbike)
->where('start_time', "<=", $start_time)
->where('stop_time', ">=", $stop_time)
->first();
if (isset($conflictTraining)) {
return redirect()->route('trainings.index')
->with('error', 'training duplicate');
}
$data = $request->all();
$data['start_time'] = $start_time;
$data['stop_time'] = $stop_time;
Training::create($data);
return redirect()->route('trainings.index')
->with('success', 'Add');
}
在此先感谢您的帮助.
推荐答案
如果您要问如何为几个新的数据库字段start_time
和stop_time
添加新的约束,我想您与您当前对$conflictTraining
的查询.
If you are asking how to add a new constraint for a couple of new database fields, start_time
and stop_time
, I think you are pretty close with your current query for $conflictTraining
.
您没有说明具体问题是什么,但是我猜这些行:
You didn't state what the specific problem was, but I'm guessing that these lines:
$start_time = Carbon::createFromFormat('d-m-Y H:s', $date_seance . ' ' . $hour_start);
$stop_time = Carbon::createFromFormat('d-m-Y H:s', $date_seance . ' ' . $hour_end);
生成的内容与start_time
和stop_time
的数据库格式不匹配.如果您分解了在这些变量中创建的内容,则基本上是在告诉Carbon创建一个类似于以下内容的变量:"15-10-2019 10:00:00"(警告:00是秒,而不是分钟,因为您使用的是s
而不是i
).
are producing something that doesn't match the format in the database for start_time
and stop_time
. If you break down what you have created in those variables, you are basically telling Carbon to make a variable that looks something like this: "15-10-2019 10:00:00" (with the caveat that the 00 is seconds, not minutes since you used s
instead of i
).
因此,您的第一个问题应该是,我的数据库是否以转储$start_time
时显示的确切格式存储开始时间?"如果没有,查询将永远不会产生匹配.您当然可以将开始时间和结束时间作为日期和时间存储在数据库中……但是请记住,如果您这样做,您将受到日期和时间的限制.换句话说,如果您只希望将开始时间设置为每天的10:00,则该操作将无效,因为数据库在特定日期存储的时间为10:00 .
So your first question should be, 'is my database storing the start time in the exact format that shows when I dump $start_time
?' If not, the query will never produce a hit. You can certainly store your start times and stop times in the database as a a date and time... but keep in mind, you will be constrained by the date as well as the time if you do so. In other words, if you just want a start time to be 10:00 on any day, that won't work because the database is storing 10:00 on a specific date.
解决此问题的一种方法是使其尽可能简单.如果这些时间未链接到特定日期,并且总是按小时显示-将其保存为简单数字.例如. 08
或22
.然后,您不需要使用Carbon,您只需要比较简单的整数即可.如果您想使用实际时间(可能需要18:20或其他时间),则并没有那么复杂-您只需要更改存储和创建时间的方式即可.
One way to resolve this is to keep this as simple as possible. If those times are NOT linked to a specific date, and are always going to be on the hour - save them as a simple number. E.g. 08
or 22
. Then, you don't need to use Carbon, you are just comparing simple integers. If you want to use an actual time (you might want 18:20 or something), this is not much more complex - you just need to change the way you store and create the time.
您可以使用此方法,但是通常的想法是以数据库和您从表单创建的内容之间的一致格式存储/创建时间.因此,对于start_time
,您也许可以将小时和分钟保存在不同的列中.或计算午夜后的分钟数并存储为整数.甚至存储为文本,并在两面都使用Carbon制成格式化的对象.
You can play with this, but the general idea would be to just store / create the time in a consistent format between database and what you create from the form. So for start_time
, you could perhaps save the hours and minutes in a different column. Or calculate the number of minutes after midnight and store as an integer. Or even store as text and use Carbon on both sides to make into a formatted object.
就我个人而言,我发现使用整数最简单,因此我将进行一次计算(可能是午夜之后的几分钟),并在整个程序中使用该计算来仅处理整数.有很多方法可以解决此问题-希望这可以向您说明问题.
Personally I find it easiest to work with integers, so I would make a calc (maybe mins past midnight) and use that calc across the program to just stay with integers. There are a lot of ways to solve this - hopefully this explains the issue to you.
这篇关于通过在预订中添加start_time和stop_time来修改我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!