user3623348 February 2016

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

Answers


The Alpha February 2016

Change your route declaration from:

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

To something like this:

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


code.prio February 2016

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.


Daenu February 2016

Change your route from:

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

to this:

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

to call the store() method of your controller.

Post Status

Asked in February 2016
Viewed 1,848 times
Voted 12
Answered 3 times

Search




Leave an answer