jeudi 7 juin 2018

Laravel limit departures to 1

There is a problem with my part of the web-app that i'm making. The point is, for the user, if he intends to create a departure date (if he already has one) to display an error message like this: "Uh oh, you have already made a departure date. Delete the previously made departure date to assign a new one!"

DepartureController.php

 public function store(Request $request)
{
    $request->validate([
        'departure_date' => 'required|date|after:yesterday',
        'location' => 'required|max:1|integer|',


    ]);

    $date = Carbon::parse($request->departure_date);
    $LocationUsers = new LocationUser();
    $LocationUsers->user_id = request()->user()->id;
    $LocationUsers->departure_date = $date;
    $LocationUsers->location_id = $request->location;
    $LocationUsers->save();

    session()->flash('success', 'Departure added succesfully!');
    return redirect('departure/create');
}

And the User model

 public function daysUntilDeparture()
{
    if ($this->location()->count() < 1) {
        return "No departure date has been given";
    }

    $location = $this->location[0];

    $userLocation = LocationUser::where('user_id', $this->id)
        ->where('location_id', $location->id)
        ->first();

    $departure_date = $userLocation->departure_date;
    return $departure_date->diffInDays(Carbon::now());
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire