根据选择更新表单提交中的url参数

时间:2018-07-28 01:22:46

标签: php laravel routes

我想使用表单中的选定选项更新url,并将其发送到会话,但我一直收到:(未定义变量:国家/地区)。

请问该如何解决?而且我是个初学者,所以请原谅我的愚蠢错误。

我的路线:

Route::post('/{country}/{region}/{city}', [

'as' => 'locationFinder',
'uses' => 'User\UserLocationsController@store']);

我的控制器:

class UserLocationsController extends Controller
{
public function store(Request $request, Country $country, Region $region, City $city)

{

    session()->put($country->name, $region->name, $city->name);

    return redirect()->route('welcome.index', [$country, $region, 
 $city]);

   }

 }

我的观点:

<form action="{{ route('locationFinder',[$country, $region, $city]) }}" method="POST">
      <div class="form-group">
        <select class="form-control" name="country_id" id="country_id">
          <option value="0" disable="true" selected="true">  Select Country </option>
            @foreach ($countries as $key => $value)
              <option value="{{$value->id}}">{{ $value->name }}</option>
            @endforeach
        </select>
      </div>
      <div class="form-group">
        <select class="form-control" name="region_id" id="region_id">
          <option value="0" disable="true" selected="true"> Select Region </option>
        </select>
      </div>
      <div class="form-group">
        <select class="form-control" name="districts" id="districts">
          <option value="0" disable="true" selected="true"> Select City </option>
        </select>
  </div>
  <div class="form-group">
      <button class="btn btn-primary">submit</button>
  </div>
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

我的CountryController:

  class CountryController extends Controller

{

public function countries(){
  $countries = Country::all();
  return view('welcome', compact('countries'));
}

public function regions(){
  $countries_id = Input::get('country_id');
  $regencies = Region::where('country_id', '=', $countries_id)->get();
  return response()->json($regencies);
}

public function cities(){
  $regions_id = Input::get('region_id');
  $districts = City::where('region_id', '=', $regions_id)->get();
  return response()->json($districts);
}


 }

1 个答案:

答案 0 :(得分:1)

在控制器中,您传递了“ $ country” ...但是在您看来,您在foreach中使用了“ $ countries”

控制器

..
return redirect()->route('welcome.index', [$country, $region, 
$city]);
..

视图

@foreach ($countries as $key => $value)
   <option value="{{$value->id}}">{{ $value->name }}</option>
@endforeach