Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

elemkeh's avatar

Resource 'store' route not working.

I'm trying to implement the category creation form on the backend of a site I'm building. The idea is to have it be on the same page as the index of existing categories. At present, though, the creation system does nothing. It doesn't return an error; nothing shows up in the database when queried directly; and nothing new appears on the page as per the redirect. Because there's no feedback, I'm groping around for what's different/wrong about this route versus the very similar route I made for the post creation mechanism earlier. In any case, here's the relevant creation form:

<form method="POST" action="{{ route('categories.store') }}" data-parsley-validate>
        <div class="form-group">
          <label name="name">Category Name:</label>
          <input id="name" name="name" class="form-control" required maxlength="255">
        </div>
        <input type="submit" value="Create" class="btn btn-lg btn-block">
        <input type="hidden" name="_token" value="{{ Session::token() }}">
</form>

Here is the 'store' method from the CategoryController:

  public function store(Request $request)
    {
        $this->validate($request, array(
          'name' => 'required|max:255'
        ));

        $category = new Category;
        $category->name = $request->name;
        $category->save();

        Session::flash('success', 'Category has been created!');

        return redirect()->route('categories.index');
    }

And here is the web.php routes file:

<?php

Route::get('/', 'PageController@getIndex');
Route::get('/contact', 'PageController@getContact');

Route::resource('posts', 'PostController');
Route::resource('categories', 'CategoryController');
Route::get('blog/{slug}', 'PostController@show')->where('slug', '[\w\d\-\_]+');
Route::get('blog', 'PostController@index');

Auth::routes();

Route::get('/home', 'HomeController@index');

?>

Again, I'm not getting any error messages at all, it's just that the submission button does nothing. Thanks in advance!

0 likes
9 replies
elemkeh's avatar

I tried switching the form method to "STORE" but the problem persists. I shut down the test-server and ran php artisan cache:clear, refreshed, then restarted the server, but it still didn't work. I also tried removing the method specification, still nothing.

I tried applying the information in the documentation linked above as well, again no effect.

sherwinmdev's avatar

run php artisan route:list and see what it says for that controller method

elemkeh's avatar

It's listed as a POST method, with the name categories.store. It passes through the web and auth middlewares. I thought there might be a chance I was getting logged out without knowing it, but A) that should disable my access to the page entirely, not just make the form break, and B) I put an if (Auth::check()) statement in the view to verify I was logged in and indeed was.

sherwinmdev's avatar

hmmm, not sure what's up. try this

$validator = Validator::make($request->all(), [
            'name' => 'required|max:255'
        ]);

        if ($validator->fails()) {
            return redirect()->route('categories.index')
                        ->withErrors($validator)
                        ->withInput();
        }

$category = new Category;
        $category->name = $request->name;

        if ($category->save())
    {
            return redirect()->route('categories.index')->with('success', 'Category created');
    }else{
        return redirect()->route('categories.index')->with('error', 'something bad happened');
    }

assuming you have something that displays flash error called 'error'. hopefully that will spit out some sort of message.

elemkeh's avatar

I tried it, then I looked through the docs some more and tried a couple variations on it, but no errors ever displayed. I used a foreach loop to iterate through all the items in the $errors variable and render them to the view, but nothing appeared.

sherwinmdev's avatar

is your view displaying the $errors?

@foreach ($errors->all() as $error)
    <li>{{ $error }}</li>
@endforeach

// the or only works in 5.4, maybe 5.3 can't remember
<p>{{ Session::get('success') or 'no success flash message' }}</p>

<p>{{ Session::get('error') or 'no error flash message' }}</p>

elemkeh's avatar

Found the error myself. At the top of the view in question, there is a table for displaying the existing categories. The closing tag was missing its forward slash which I overlooked. Apparently this entirely interrupted the functionality of the form that came later without throwing any errors.

The moral of the story is: If you are working with dynamic behavior on a web app and not getting any exceptions, there is a chance it's because the error is in your HTML.

2 likes

Please or to participate in this conversation.