简体   繁体   中英

Laravel form select option

I have a landing page with Create, Edit and Delete buttons, The Edit button is linked to a select (dropdown) menu and my route for the URL is /events/{number}/edit.

How will I get my URL to write to this format as the page will already be loaded and then the selection is made.

<div class="col-sm-4">
    <form method="get" action="{{ route('events.edit', ['event_id' => '2']) }}">
        <label>Event Name</label>
        <select name="event_edit" class="form-control form-control-lg">
            @foreach($events as $event)
                <option>{{$event->event_name}}</option>
            @endforeach  
        </select>  
        <button type="submit" class="btn btn-primary btn-round">Edit</button>
    </form>
</div>

The above works but I have to put the event_id manually which is set to 2, will javascript be needed for this one?

Route::resource('events', 'EventController');

GET|HEAD  | events/{event}/edit    | events.edit      | App\Http\Controllers\EventController@edit                              | web    

I do not have a larvel instance I can test on but simply put PHP is static so once a DOM is render you cannot update the output with PHP ie putting the $event->id in the action when a dropdown (or any element) is changed. If you wanted to you would need JavaScript. However, I would opt for a more straight forward approach.

You will need to create a generic edit route on your controller and put the id on the option as the value.

<div class="col-sm-4">
    <form method="get" action="{{ route('events.edit') }}">
        <label>Event Name</label>
        <select name="event_edit" class="form-control form-control-lg">
            @foreach($events as $event)
                <option value="{{$event->id}}">{{$event->name}}</option>
            @endforeach  
        </select>  
    </form>
</div>

Now when the form is submitted you will have the id of the event the user wanted to edit. Simply use that to get the event in the controller and render the edit screen for that event. If you want the url to be displayed nicely as site.com/edit/event/3 then you could use something like

return redirect()->route('route.name', [$param]);

which should redirect to that routes url with the param being the id then do all the logic for getting and displaying there.

hm. Do we need the form-action here? How about this:

<div class="col-sm-4">
    <form method="get" action="">
        <label>Event Name</label>
        <select name="event_edit" class="form-control form-control-lg" 
            onChange="window.location.href='/events/' + this.options[this.selectedIndex].value + '/edit'">
            @foreach($events as $event)
                <option value="{{$event->id}}">{{$event->name}}</option>
            @endforeach  
        </select>  
    </form>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM