user3623348
user3623348

Reputation: 139

Store method not working using resource route

I am having trouble figuring out why my data is not being posted and stored in my database. I have used the resource routes for another form and it works fine, but here for some reason it won't work. Clicking submit just seems to refresh the page, no errors to work from!

So I have a form which gets the workout routines from a database, and on submission I want this to create a new Workout "session" in my database table (called "Workouts"). The form is this:

{{ Form::open(array('url' => '/')) }}

            <div class="form-group">
                {{ Form::text('workout_name', Input::old('workout_name'), array('class' => 'form-control', 'placeholder' => 'Session Name')) }}
            </div>

            <div class="form-group">
                {{ Form::select('routines', $routine_names, null, array('class' => 'form-control')) }}
            </div>

                {{ Form::submit('Select Routine', array('class' => 'btn btn-success pull-right')) }}

            {{ Form::close() }}

In my HomeController I have this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Routine;
use App\Workout;

class HomeController extends Controller
{

public function index()
{
    $routines = Routine::all();
    $routine_names = Routine::lists('routine_name');

    return view('workout')->with(array('routines'=>$routines, 'routine_names'=>$routine_names));
}

public function store()
{

    $workout = new Workout;
    $workout->workout_name = Input::get('workout_name');
    $workout->save();

}

}

I have a model created for the Workout, and the route for this page is the following:

Route::resource('/', 'HomeController');

I can't figure out where I'm going wrong. The index method in my controller is working, as it is returning the correct view with the data I need. The form also looks OK I think, as I'm posting to the same page, but submitting doesn't seem to carry out the code I have in the store method of the HomeController.

Any help would be appreciated!

Thanks :)

Upvotes: 4

Views: 5926

Answers (2)

Mahabub Islam Prio
Mahabub Islam Prio

Reputation: 1085

If you are using the resources controller creator command of php artisan then all the specific routes are created for you. To see all listed routes you can type , php artisan routes. This will show you RESTFUL routes even for your POST method .

And also even you did not created the resources controller and did made the routes with manual way then you can create ,

Route::POST('/workout' , SomeController@post);

I am trying to say , you have to use the different POST method for the form submission .

Hope this will solve your problem . Thanks.

Upvotes: 0

The Alpha
The Alpha

Reputation: 146201

Change your route declaration from:

Route::resource('/', 'HomeController');

To something like this:

Route::resource('/workout', 'WorkoutController');

Upvotes: 3

Related Questions