Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hollow-Serenity's avatar

Updating your departure date in laravel

At the moment I'm only able to create the departure date as a user. How should I write my controller in order to update the departure date if I re-select it?

@section('content')
    <div class="container-fluid">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card card-default">
                    <div class="card-header">
                        Make a departure
                    </div>
                    <div class="card-body">
                        @if (session('success'))
                            <div class="alert alert-dismissible alert-success">
                                {{ session('success') }}
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                            </div>
                        @endif
                        @if ($errors->any())
                            <div class="alert alert-danger">
                                <ul>
                                    @foreach ($errors->all() as $error)
                                        <li>{{ $error }}</li>
                                    @endforeach
                                </ul>
                            </div>
                        @endif
                            <form method="post" action="{{route('departure.store')}}">
                                <div class="form-group"> <!-- Date input -->
                                    <label class="control-label" for="departure_date">Date
                                        <input class="form-control" id="departure_date" name="departure_date"
                                               placeholder="DD/MM/YYYY" type="text"/>
                                    </label>
                                </div>
                                {{ csrf_field() }}
                            <div class="form-group">
                                <label class="control-label" for="location">Location
                                <br>
                                    <select class="form-control" name="location" id="LocationUsers">
                                        <option value="empty" selected>Empty</option>
                                        @foreach($LocationUsers as $LocationUser)
                                            <option value="{{$LocationUser->id}}"
                                                    class="form-control">{{$LocationUser->id}}
                                                -{{$LocationUser->city}}</option>
                                        @endforeach
                                    </select>
                                </label>
                            </div>
                            <br>
                            <input type="submit" class="btn btn-primary" value="Create departure">
                                <form method="POST" action="{{route('departure.update')}}">
                                    {!! method_field('patch') !!}
                                    <input type="submit" class="btn btn-warning" value="Update Departure">
                                </form>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection
0 likes
3 replies
tykus's avatar

Generally you would have a separate route (route('departure.edit', $departure->id);), action DepartureController@edit and view resources/views/departure/edit.blade.php to display the edit form.

You would also need to give the update route the id of the resource you are editing:

route('departure.update', $departure->id)
Hollow-Serenity's avatar

Still having some problems... How could I resolve this in my public function update?

Here is my create and store function

public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'departure_date' => 'required|date|after:today',
            'location' => 'required|max:1|integer',
        ]);


        $request->validate([
            'departure_date' => [new CurrentDeparture, 'required', 'date', 'after:yesterday'],
        ]);

        $validator->validate();

        $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();

        $user = LocationUser::where('user_id', \request()->id)->first();

        if ($user)
        {
            session()->flash('fail', 'Uh oh, you have already made a departure date. Delete the previously made departure date to assign a new one!');

            return redirect('departure.create');
        }

        session()->flash('success', 'Departure added succesfully!');
        return redirect('home');
    }
public function create()
    {
        $LocationUsers = Location::all();
        return view('departure.create', compact('LocationUsers'));
    }

Please or to participate in this conversation.