gpt4 book ai didi

php - Laravel - 在编辑 View 上添加日期验证

转载 作者:行者123 更新时间:2023-12-01 15:37:01 25 4
gpt4 key购买 nike

我有两张 table ——“旅行”和“事件”。一次旅行可以有很多事件。我有一个表单,允许用户将事件添加到他们的旅行中。我还有一个允许用户编辑事件的表单。

我在添加和事件时添加了一个检查。仅当事件的开始日期和结束日期在行程开始日期和行程结束日期之间时,才能添加该事件。

但是,我似乎无法在编辑 View 上进行相同的检查。使用与我在编辑中添加的代码相同的代码,如果用户更改了事件的时间但未更改事件的开始/结束时间,则会出现日期错误消息 - 即使日期在行程内。

EventController - 添加正在运行的功能

public function addEvent(Request $request)
{
$this->validate($request, [
'event_name' => 'required',
'start_date' => 'required',
'end_date' => 'required',
'time' => 'required',
'trip_id' => 'required',

]);

$start_date = Carbon::parse($request['start_date'])->format('Y-m-d');
$end_date = Carbon::parse($request['end_date'])->format('Y-m-d');

$tripCheck = Trip::where('id', $request['trip_id'])
->whereDate('startdate', '<=', $start_date)
->whereDate('enddate', '>=', $start_date)

->whereDate('startdate', '<=', $end_date)
->whereDate('enddate', '>=', $end_date)
->first();

if ($tripCheck) {

$events = new Events;
$trips = Trip::all();
$categories = Categories::pluck('category','id');
$events->category_id = $request['category_id'];
$events->colour = $request['colour'];
$events->event_name = $request['event_name'];
$events->start_date = $request['start_date'];
$events->end_date = $request['end_date'];
$events->time = $request['time'];
$events->address = $request['address'];
$events->notes = $request['notes'];
$events->trip_id = $request['trip_id'];
$events->save();

return redirect('trips')->with('success', 'The new event has been added to your trip')->with('trips', $trips)->withCategories($categories);
} else
{
return redirect('trips')->withErrors(['The dates you added are not within Trip start and end date.']);
}

}

事件 Controller 编辑功能不起作用
public function edit($id)
{
$events = Events::find($id);
$categories = Categories::pluck('category','id');
return view ('editEventForm')->with('events', $events)->withCategories($categories);
}

public function update(Request $request, $id)
{
$this->validate($request, [
'category_id' => 'required',
'event_name' => 'required',
'start_date' => 'required',
'end_date' => 'required',
]);

$start_date = Carbon::parse($request['start_date'])->format('Y-m-d');
$end_date = Carbon::parse($request['end_date'])->format('Y-m-d');

$tripCheck = Trip::where('id', $request['trip_id'])
->whereDate('startdate', '<=', $start_date)
->whereDate('enddate', '>=', $start_date)

->whereDate('startdate', '<=', $end_date)
->whereDate('enddate', '>=', $end_date)
->first();

if($tripCheck) {

//Update Event
$events = Events::find($id);
$events->category_id = $request['category_id'];
$events->colour = $request['colour'];
$events->event_name = $request['event_name'];
$events->start_date = $request['start_date'];
$events->end_date = $request['end_date'];
$events->time = $request['time'];
$events->address = $request['address'];
$events->notes = $request['notes'];
$events->save();

return redirect('trips')->with('success', 'Event Updated');
} else {
return redirect('trips')->withErrors(['The dates you added are not within Trip start and end date.']);
}

}

trip.blade.php
<div class="form-group">
<input type="text" name="destination" class="form-control" value="{{$trip->destination}}" placeholder="Destination" />
</div>
<h7>Trip Start Date: </h7>
<div class="form-group">
<input type="date" name="startdate" class="form-control" value="{{$trip->startdate}}" placeholder="Start Date" />
</div>
<h7>Trip End Date: </h7>
<div class="form-group">
<input type="date" name="enddate" class="form-control" value="{{$trip->enddate}}" placeholder="End Date" />
</div>
<div>

行程 Controller
public function submit(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'destination' => 'required',
'startdate' => 'required',
'enddate' => 'required'
]);

//Create new trips
$trip = new Trip;
$trip->name = $request->input('name');
$trip->email = $request->input('email');
$trip->destination = $request->input('destination');
$trip->startdate = $request->input('startdate');
$trip->enddate = $request->input('enddate');
$trip->user_id = auth()->user()->id;

//save trips
$trip->save();

//Rredirect
return redirect('/home')->with('status', 'Trip Created Sucessfully');

}

最佳答案

update方法正在寻找$request['trip_id'] ,它存在吗?它不像 create 那样在必需的验证中。方法。

它在您的更新表格中吗?

还有一个额外的提示,您可以使用 create 来简化很多代码和 fill在模型对象上。

即:$trip = Trip::create($request->input());假设输入名称与模型匹配并且输入是干净的。

关于php - Laravel - 在编辑 View 上添加日期验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60416053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com