laravel - FormRequest prevents controller method's exceution -
here form request
class categoryrequest extends formrequest { /** * determine if user authorized make request. * * @return bool */ public function authorize() { return true; } /** * validation rules apply request. * * @return array */ public function rules(request $request) { if ($this->method() === 'post') { $parameters = $this->route()->parameters(); $category = null; if (isset($parameters['category'])) { $category = $parameters['category']; } $id = $category ? $category->id : null; $required = $id ? 'required' : 'nullable'; return [ 'name' => 'required|string|max:256', 'slug' => $required . '|string|max:256|unique:categories,slug,' . $id ]; } return []; }
for "create" method works fine , creates new entry in database.
but "update" method not work reason.
public function update(categoryrequest $request, category $category) { $category->fill($request->all()); $category->save(); return redirect()->back(); } public function create(categoryrequest $request) { if ($request->method() === 'get') { return view('admin.category.create'); } $slug = $request->get('slug'); $name = $request->get('name'); $i = 0; while (!$slug) { $newslug = str::slug($name); if ($i > 0) { $newslug = sprintf('%s-%d', $newslug, $i); } $i++; if (!category::where('slug', $newslug)->exists()) { $slug = $newslug; } } $category = category::create(compact('name', 'slug')); return redirect()->route('admin.category.show', ['id' => $category->id]); }
routes
route::post('category/{category}', 'admin\\categorycontroller@update')->name('update'); route::match(['get','post'], 'category', 'admin\\categorycontroller@create')->name('create');
i've tried dd in "update" not execute , not show errors. template shows validation errors on creation, shows validation errors on update (for example, omit "name" attribute , shows error) valid data not execute "update" method @ all.
Comments
Post a Comment