Я создал 3 выпадающих списка с помощью al oop, но когда я его поменяю, он отправит форму, которая будет go для функции show моего контроллера. Затем он возвращается с новыми параметрами на страницу индекса, но я не могу вспомнить первое раскрывающееся значение. моя индексная страница:
@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
мой контроллер:
<?php
namespace App\Http\Controllers\Student;
use App\Country;
use App\City;
use App\Organisation;
use App\Place;
use App\Record;
use Facades\App\Helpers\Json;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class ApplicationController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
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 \Illuminate\Http\Response
*/
public function create()
{
return redirect('applications/');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param \App\Record $record
* @return \Illuminate\Http\Response
*/
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 \App\Record $record
* @return \Illuminate\Http\Response
*/
public function edit(Record $record)
{
return redirect('applications/');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Record $record
* @return \Illuminate\Http\Response
*/
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 \App\Record $record
* @return \Illuminate\Http\Response
*/
public function destroy(Record $record)
{
//
}
}
Если вам нужна дополнительная информация, пожалуйста, дайте мне знать!