コード例 #1
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     $countries = Country::orderBy('name', 'asc')->get();
     $cities = array();
     $languages = array();
     if (count($countries) > 0) {
         $cities = $countries[0]->cities()->orderBy('name', 'asc')->get();
         if (count($cities) > 0) {
             $languages = $cities[0]->languages()->orderBy('name', 'asc')->get();
         }
     }
     $data = ['active_menu' => 'index', 'countries' => $countries, 'cities' => $cities, 'languages' => $languages];
     return view('index', $data);
 }
コード例 #2
0
 public function destroy(Request $request)
 {
     //Validation data
     $customMessages = ['exists' => 'Страна не найдена.'];
     $customAttributes = ['id' => 'Страна'];
     $this->validate($request, ['id' => 'required|exists:countries,id'], $customMessages, $customAttributes);
     //Remove from DB
     $country_rec = Country::find($request->input('id'));
     $cities_rec = $country_rec->cities()->get();
     foreach ($cities_rec as $city_rec) {
         $city_rec->languages()->detach();
         $city_rec->delete();
     }
     $country_rec->delete();
     //Redirect to countries page
     return redirect()->route('countries');
 }
コード例 #3
0
 public function store(Request $request)
 {
     //Validation data
     $customMessages = ['exists' => 'Страна не найдена.'];
     $customAttributes = ['name' => 'Город', 'country_id' => 'Страна'];
     $this->validate($request, ['country_id' => 'required|exists:countries,id', 'name' => 'required|min:2|max:50|unique:cities,name'], $customMessages, $customAttributes);
     //Insert data to DB
     $city_rec = new City();
     $city_rec->name = $request->input('name');
     $country = Country::find($request->input('country_id'));
     $country->cities()->save($city_rec);
     //Redirect to cities page
     return redirect()->route('cities');
 }