2
votes

I have a form

<form action="{{ action('NotesController@store') }}" method="post">
    //
</form>

The action points to the store method in the NotesController controller in the App\Http\Controllers directory.

The NotesController looks like this:

<?php

namespace App\Http\Controllers;

use App\Note;
use Illuminate\Http\Request;

class NotesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // var_dump($request);
        //
        // $note = new Note();


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

But when I try to render the page that displays my form (so before even submitting it, just trying to render the form itself) I get the following error:

Action App\Http\Controllers\NotesController@store not defined. (View: C:\xampp\htdocs\personal_projects\Active\diary_app\resources\views\diary\entry.blade.php)

3
Have you added routes in the file ?Pratik
Add your route for NotesController@store in question!Hiren Gohel
Which Laravel version is it?lchachurski
It's 5.5, I have added Route::resource('notes', 'NotesController'); and it works...Jethro Hazelhurst

3 Answers

5
votes

you need to define web routes for the controller in routes/web.php

Route::resource('notes', 'NotesController');
3
votes

You can use the action() helper to generate an URL to your route:

<form method="post" action="{{ action('NotesController@store') }}" accept-charset="UTF-8">

You can also use like:

In Route:

Route::post('notes', 
      array('uses'=>'App\Controllers\NotesController@store',
             'as' => 'notes.create'));

In View:

{{ Form::open(array('url'=>route('notes.create'))) }}

And if you are using Laravel resourceController, then use:

Route::resource('notes', 'NotesController');

Hope this helps you!

0
votes

Use this code and see what you get:

routes/web.php

Route::get('create', 'NotesController@Create');
Route::post('create', 'NotesController@Store');

form:

<form method="POST" action="/create">
    {{ csrf_field() }}
    {{-- START - FORM DATA --}}
    
    {{-- END - FORM DATA --}}
</form>