I created 3 dropdowns with a loop, but when I change one, it will submit a form that will go to the show function of my controller. Then it returns with new parameters to the index page, but I can’t get it to remember the first dropdown value. my index page:
@section('main') <h1>Apply now!</h1> <h2>Select your preferences</h2> <form action="/applications/{{$record->id}}" method="post" id="submitForm"> @method('put') @csrf @for ($i=0; $i<3; $i++) <p style="font-weight: bold">Choice 1</p> <label for="country{{$i}}">Country</label> <select name="country{{$i}}" id="country{{$i}}"> <option value="%">All countries</option> @foreach($places_countries as $country) <option value="{{$country->country->id}} {{ (request()->country == $country->country->id ? 'selected' : '') }}">{{ $country->country->country }}</option> @endforeach <option value="other">Other</option> </select> <label for="otherOption{{$i}}" hidden>what's your suggestion?</label> <input type="text" id="otherOption{{$i}}" hidden> <label for="city{{$i}}" hidden>City</label> <select name="city{{$i}}" id="city{{$i}}" hidden> <option value="%">All cities</option> @foreach($places_cities as $city) <option value="{{$city->city->id}}">{{ $city->city->city }}</option> @endforeach </select> <label for="organisation{{$i}}" hidden>Organisation</label> <select name="organisation{{$i}}" id="organisation{{$i}}" hidden> <option value="%">All organisations</option> @foreach($places_organisations as $organisation) <option value="{{$organisation->organisation->id}}">{{ $organisation->organisation->organisation }}</option> @endforeach </select> @endfor <br> <button type="submit">Submit your choices</button> </form> @endsection
my controller:
<?php namespace AppHttpControllersStudent; use AppCountry; use AppCity; use AppOrganisation; use AppPlace; use AppRecord; use FacadesAppHelpersJson; use IlluminateHttpRequest; use AppHttpControllersController; use IlluminateSupportFacadesDB; class ApplicationController extends Controller { /** * Display a listing of the resource. * * @return IlluminateHttpResponse */ public function index() { $places_countries = Place::distinct()->get(['country_id']); $places_cities = Place::distinct()->get(['city_id']); $places_organisations = Place::distinct()->get(['organisation_id']); $record = DB::table('records')->where('id', '1')->first(); $result = compact('places_countries', 'places_cities', 'places_organisations', 'record'); Json::dump($result); return view('student.application', $result); } /** * Show the form for creating a new resource. * * @return IlluminateHttpResponse */ public function create() { return redirect('applications/'); } /** * Store a newly created resource in storage. * * @param IlluminateHttpRequest $request * @return IlluminateHttpResponse */ public function store(Request $request) { // } /** * Display the specified resource. * * @param AppRecord $record * @return IlluminateHttpResponse */ public function show(Request $request, $id) { $places_countries = Place::distinct()->get(['country_id']); if ($request->country0 != null || $request->country1 != null || $request->country2 != null){ //if a country is chosen, set all cities of that country in dropdown //$places_cities = DB::table('places')->where('city_id', $id)->distinct(); $places_cities = Place::orderBy('id', 'asc') ->where(function ($query) use ($id) { $query->where('city_id', '=', $id); }); }else{ //if none is chosen, set all distinct cities in dropdown $places_cities = Place::distinct()->get(['city_id']); } if ($request->city0 != null || $request->city1 != null || $request->city2 != null){ //if a city is chosen, set all organisations of that city in dropdown //$places_organisations = DB::table('places')->where('organisation_id', $id)->distinct(); $places_organisations = Place::orderBy('id', 'asc') ->where(function ($query) use ($id) { $query->where('organisation_id', '=', $id); }); }else{//if none is chosen, set all distinct organisations in dropdown $places_organisations = Place::distinct()->get(['organisation_id']); } $record = DB::table('records')->where('id', '1')->first(); $result = compact('places_countries', 'places_cities', 'places_organisations', 'record'); Json::dump($result); return view('student.application', $result); } /** * Show the form for editing the specified resource. * * @param AppRecord $record * @return IlluminateHttpResponse */ public function edit(Record $record) { return redirect('applications/'); } /** * Update the specified resource in storage. * * @param IlluminateHttpRequest $request * @param AppRecord $record * @return IlluminateHttpResponse */ public function update(Request $request, $id) { $record = Record::find($id); $preference1 = $request->country0 . " " . $request->city0 . " " . $request->organisation0; $preference2 = $request->country1 . " " . $request->city1 . " " . $request->organisation1; $preference3 = $request->country2 . " " . $request->city2 . " " . $request->organisation2; $record->preference1 = $preference1; $record->preference2 = $preference2; $record->preference3 = $preference3; $record->save(); return response()->json([ 'type' => 'success', 'text' => "Your preferences are submitted!", ]); } /** * Remove the specified resource from storage. * * @param AppRecord $record * @return IlluminateHttpResponse */ public function destroy(Record $record) { // } }
If you need any more information, please let me know!
Advertisement
Answer
you can achieve that by simply using sessions, learn about Laravel session here
Save data to session
// Via a request instance... $request->session()->put('key', 'value'); // Via the global helper... session(['key' => 'value']);
Retrieve data from session
// Via a request instance... $value = $request->session()->get('key'); // Retrieve a piece of data from the session... $value = session('key');