Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Laravel 4 Basics Blade & Forms Form Builder

Error message when submitted form for the first time

Hi there,

I have made the 'create.blade.php' file as shown by Hampton and replaced the contents of the 'store' function to say: 'return "Creating a new list"'; However I keep on receiving errors when submitting the form. I get an error:

Whoops, looks like something went wrong. TokenMismatchException in compiled.php line 2440.

I tried removing the line 'App\Http\Middleware\VerifyCsrfToken' from Kernal.php, but I then got another error -

Sorry, the page you are looking for could not be found. NotFoundHttpException in compiled.php line 7693:

Now I am well and truly in a pickle! Anyone have any expert advice on what might be going wrong?

Thanks!

Robert London, UK

P.s.

Here is my create.blade.php code:

"@extends('layouts.main') @section('content') <!--Anything in here, in this section, is what is going to print into our content yield-->

<form method="POST" action="http://larvael.dev:8000/todos" accept-charset="UTF-8">
    <label for="title" type="text" id="title"> List Title </label>
    <input name="title" type="text" id="title">
    <input class="button" type="submit" value="submit">
</form>

@stop"

Thanks.

Benjamin Payne
Benjamin Payne
8,142 Points

Hey Robert, Can you post your entire TodoListController.php?

Thanks, Ben

Sure, here goes - my todoListController.php file:

<?php namespace App\Http\Controllers;

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

use Illuminate\Http\Request;

use View;

use app\Model;

use app\User;

use app\TodoList;

class TodoListController extends Controller { // extends the controller from Laravel

/**
 * Display a listing of the resource.
 *
 * @return Response
 *Will list out all of our ToDo lists
 */
public function index()
{
    $todo_lists = \App\TodoList::all();
    return View::make('todos.index')->with('todo_lists', $todo_lists);
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    return View::make('todos.create');

}

/**
 * Store a newly created resource in a database.
 *
 * @return Response
 */
public function store()
{
    return "Creating a new list";

}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    $list = \App\TodoList::findOrFail($id);
    return View::make('todos.show')->withList($list);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    // Updates the specified resource in the database.
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    // destroys the resource in the database.
}

}

This is my routes.php file:

<?php

*/

/* / = home /todos - all lists /todos/1 - show /todos/1/edit - edit and update /todos/create - create new list

*/ Route::get('/todos/create', 'TodoListController@create');

Route::get('/', 'TodoListController@index');

Route::get('/db', function(){ $names = DB::table('todo_lists')->select('id', 'name as user_name')->get();

    foreach ($names as $name){
        echo $name->user_name . '<br/><br/>';
    }

});

Route::get('/todos/{id}', 'TodoListController@show');

I have kind of got around the problem though by creating a new route in my routes.php file, to get it to display the message "Creating a new list":

"Route::post('todos.store', array('as' => 'todos.store', function() {
return "<p>Creating a new list</p>"; }));"

Not sure this was the totally correct approach, though!!

9 Answers

Excellent so you now have everything working. That's exactly what was supposed to happen. Now you just need to return a view upon successful creation. Something like below would work.

return View::make('todos.show')->withList($list);

Because you're opening the form yourself and not using Laravel's tool to open the form you're missing the XSRF token and it's blocking the form submission for security reasons.

Try adding this to your form:

<input type="hidden" name="_token" value="{{ csrf_token() }}">

Alternately you can use use the following:

{!! Form::open(array('route' => ['todos.store'])) !!}
    <label for="title" type="text" id="title"> List Title </label>
    <input name="title" type="text" id="title">
    <input class="button" type="submit" value="submit">
{!! Form::close() !!}

Thanks Corey, now I get the following message:

ErrorException in compiled.php line 8545:

Route [todos.store] not defined. (View: /home/vagrant/Sites/laravel-basics/resources/views/todos/create.blade.php)

Just wondered how I might set this up? I'm not sure Hampton mentioned specifically how to create a route in routes.php for the store method. I have attempted to create my own:

Route::post('todos.store', array('as' => 'todos.store', function() // {
// return "<p>Creating a new list</p>"; // }));

But the video mentioned that the 'Creating a new list' return statement should be in the controller file? :)

Thanks,

Rob

Yes, this is precisely my problem. I don't know how to create a route to todos.store in routes.php.

The easiest way is:

Route::resource('todos', 'TodoListController');

That gets you the standard RESful routing for your standard things. You get index, show, create, update, insert, and destroy. Adding additional functions becomes a pain though.

The other way to go that is more granular is route groups and I'll post a functional example below.

Route::group(['prefix' => 'products'], function()
{
    Route::get('create', [
        'as'    => 'products.create',
        'uses'  => 'ProductsController@create'
    ]);

    Route::post('store', [
        'as'    => 'products.store',
        'uses'  => 'ProductsController@store'
    ]);

    Route::get('american-heart', [
        'as'    => 'products.american-heart',
        'uses'  => 'ProductsController@AmericanHeart'
    ]);

    Route::get('{id}', [
        'as'    => 'products.show',
        'uses'  => 'ProductsController@show'
    ])->where('id', '[0-9]+');

    Route::get('{id}/edit', [
        'as'    => 'products.edit',
        'uses'  => 'ProductsController@edit'
    ])->where('id', '[0-9]+');

    Route::put('{id}', [
        'as'    => 'products.update',
        'uses'  => 'ProductsController@update'
    ])->where('id', '[0-9]+');

    Route::delete('{id}', [
        'as'    => 'products.destroy',
        'uses'  => 'ProductsController@destroy'
    ])->where('id', '[0-9]+');

    Route::get('/', [
        'as'    => 'products.index',
        'uses'  => 'ProductsController@index'
    ]);
});

This allows you to implement the standard CRUD as well as extend your application with some different routes. In my example I have http://example.com/products/american-heart which points to the AmericanHeart method in my Products controller.

More information here: http://laravel.com/docs/5.0/routing#route-groups

Hi Corey,

Thanks for your reply. Apologies that this is taking so long to sort out.

I have used the first piece of code (as you mentioned - Route::resource('todos', 'TodoListController');), and placed this in my routes.php file. That cleared the error message I was getting, but now I am getting the message:

FatalErrorException in TodoListController.php line 52:

Class 'app\TodoList' not found

This is even though I have the code at the top of my TodoListController.php file saying: use app\TodoList, and I have a file name 'TodoList.php' under my app directory.

My TodoListController.php file:

<?php namespace App\Http\Controllers;

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

use Illuminate\Http\Request;

use View;

use app\Model;

use app\User;

use app\TodoList;

use Input;

class TodoListController extends Controller { // extends the controller from Laravel

/**
 * Display a listing of the resource.
 *
 * @return Response
 *Will list out all of our ToDo lists
 */
public function index()
{
    $todo_lists = \App\TodoList::all();
    return View::make('todos.index')->with('todo_lists', $todo_lists);
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    return View::make('todos.create');

}

/**
 * Store a newly created resource in a database.
 *
 * @return Response
 */
public function store()
{
    $name = Input::get('title');
    $list = new TodoList();
    $list->name = $name;
    $list->save();

}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    $list = \App\TodoList::findOrFail($id);
    return View::make('todos.show')->withList($list);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    // Updates the specified resource in the database.
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    // destroys the resource in the database.
}

}

My TodoList.php file:

<?php namespace App;

use Eloquent;

class TodoList extends Eloquent { public $timestamps = false; }

?>

Well, we're making huge progress. The last thing I see is in your controller when you're putting your use statements you need to make a couple small changes.

Everywhere you have:

use app\[something]

change app to App.

Also about how long it's taking.. No worries on my part, I'll come back and help until you're sorted =)

Hi Corey,

Yes...this works!! Just. Now when I submit my form, I get directed to a blank page (url: http://laravel.dev/todos) - but when I check my todo_lists database, the input seems to have been added to a new row! So quite ok then. Maybe I should rewatch the video to see if this was what was supposed to happen? :)

Thanks, Rob

Brill - I have added this to the end of my 'store' method in my controller file - I now have a regular page when I hit submit showing the name of my newly added list item.

Thanks.

This line actually returns all the list items which is better:

return Redirect::route('todos.index');

and I have imported 'use Illuminate\Support\Facades\Redirect'