/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, ['city' => 'required|unique', 'country' => 'required|exists:cities,country_id']); $city = new City(); $city->city = $request->city; $city->country_id = $request->country; $city->save(); foreach ($request->language as $language_id) { $city->language()->attach($language_id); } $statusCode = 200; $response = ["success" => "City successfully created"]; return response($response, $statusCode); }