Laravel - Adding date validation on edit view

Laura :

I have two tables - 'Trips' and 'Events'. One trip can have many events. I have a form which allows users to add events to their trip. I also have a form which allows users to edit event.

I have added a check when adding and event. The event can only be added if the start date and end date of the event is within the trip start date and the trip end date.

However, I can't seem to get the same check to happen on edit view. Using the same code as my add in my edit, if a user changes the tite of the event but does not change the event start/end times, the date error message appears - even though the dates are within the trip.

EventController - add funtion which is working

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

}

Event Controller edit function not working

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>

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

}
Darryl E. Clarke :

The update method is looking for $request['trip_id'], does it exist? It's not in the required validation like it is in the create method.

Is it in your update form?

Also a extra tip, you can simplifiy a lot of your code by using create and fill on model objects.

ie: $trip = Trip::create($request->input()); assuming the input names match the model and the input is clean.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=32957&siteId=1