public function postEdit(Request $request) { $this->validate($request, ['id' => 'required|numeric|exists:series', 'name' => "required|unique:series,name,{$request->input('id')},id|max:255", 'characters' => 'array', 'characters.*' => 'string']); $series = Series::find($request->input('id')); $series->name = $request->input('name'); $series->save(); if (!empty($request->input('characters'))) { Series_Characters::where("series_id", '=', $series->id)->whereNotIn("name", $request->input('characters'))->delete(); foreach ($request->input('characters') as $c) { $character = Series_Characters::where("series_id", '=', $series->id)->where("name", '=', $c)->first(); if (empty($character)) { //As of now if the character exists, there is nothing to update. This may change over time $character = new Series_Characters(); $character->series_id = $series->id; $character->name = $c; $character->save(); } } } return redirect()->action('SeriesController@getList'); }
public function getNcharactersAttribute() { return Series_Characters::where('series_id', '=', $this->id)->count(); }