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

newbie14's avatar

How to submit the form to controller or route?

Hi I have the following form? So when I need to submit I what do I need to create new controller or route? In normal php form I just do a _POST to get the value and then I run an insert query statement so here in laravel what should be correct next step?

{!! Form::label('Type', 'I Type', ['class' => 'col-md-4 control-label']) !!} @{{ints.IName}}
                                <button class="btn btn-primary btn-block" type="submit">Submit</button>
                                <span v-show="loading" class="btn btn-primary" style="width: 100%;"><i class="fa fa-spinner fa-spin"></i></span>
                            </div>
                        </div>
0 likes
27 replies
zachleigh's avatar

Create a controller and a route. Submit the form to a route and in routes/web.php define how to handle the request. The free introductory series I posted about in one of your other threads shows exactly how to do this.

newbie14's avatar

Ok in my form I did this

So in the controller I will create a new function like this

public function rexam() {

}

What should I do in my route because the current route is this Route::get('stds', 'SController@index'); where it open up my current form? I am not too sure what to adjust for the Route?

newbie14's avatar

I am getting this error App\Http\Controllers\SController@rexam not defined. I have already created this function in the SController?

zachleigh's avatar
Route::get('/route', 'SController@rexam');

That would be for a get request. For a post request:

Route::post('/route', 'SController@rexam');
newbie14's avatar

Is that I must add something to my route also? Without adding into route it wont work is it? How should I add this to the route?

zachleigh's avatar

This is your route:

Route::post('/route', 'SController@rexam');
newbie14's avatar

I already have the get Route::get('stds', 'SController@index'); So I just add the post like this or must I put the slash ? Route::post('stds', 'SController@rexam');

newbie14's avatar

I notice these codes <form @submit.prevent="submitExamRequest()" method="post" action="{{ action('SController@rexam') }}" class="form-horizontal"> Then the vue it has this

submitExamRequest: function() { this.loading = !this.loading; if (this.validateRequestExamForm()) { //var apiURL = "{{ url('/exam/create') }}"+"?_token="+"{{csrf_token()}}"; Vue.http.post(apiURL, this.user).then((response) => { // success callback this.rExam(); this.pane.flagcs = false;

                }, (response) => {
                    // error callback
                    alert("User Profile: Update Error - "+response.status);
                });
            }
            this.loading = !this.loading;
        }

Should I remove this vue function so that then it can post to action="{{ action('SController@rexam') }}"

newbie14's avatar

Ok I remove this @submit.prevent="submitExamRequest()" but I got stucked into this TokenMismatchException in VerifyCsrfToken.php line 68:

zachleigh's avatar

Please post your form, routes file, and controller.

newbie14's avatar

I have added this as per google but yet it does not work

newbie14's avatar

I have added this also but yet it does not work?

newbie14's avatar

The form codes is a bit too long to be posted here? Any place I could upload for you ?

zachleigh's avatar

Just so its all in one place...

<form id="detailUpdateForm" method="POST" action="user/update" class="form-horizontal" v-on:submit.prevent="updateUser()">
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Http\Requests;
//use App\Syllabus;

class StudentController extends Controller
{

  /**
   * Create a new controller instance.
   *
   * @return void
   */
  public function __construct()
  {
      $this->middleware('auth');
  }

  /** Show the Student Portal */
  public function index()
  {
//    dd(url());
//    dd(route('aimeos_shop_list', [], false));
//    dd(route('aimeos_shop_account', [], false));

        $events = [];

        $events[] = \Calendar::event(
            'Event One', //event title
            false, //full day event?
            '2016-11-11T0800', //start time (you can also use Carbon instead of DateTime)
            '2015-11-12T0800', //end time (you can also use Carbon instead of DateTime)
            0 //optionally, you can specify an event ID
        );

        $events[] = \Calendar::event(
            "Valentine's Day", //event title
            true, //full day event?
            new \DateTime('2016-11-14'), //start time (you can also use Carbon instead of DateTime)
            new \DateTime('2016-11-14'), //end time (you can also use Carbon instead of DateTime)
            'stringEventId' //optionally, you can specify an event ID
        );

        $eloquentEvent = $events[0]; //EventModel implements MaddHatter\LaravelFullcalendar\Event

        $calendar = \Calendar::addEvents($events);

      return view('student', compact('calendar'));
  }

  /** Show the Student Portal - schedule new class */
  public function scheduleclass()
  {
      return view('student-scheduleclass');
  }

  /** Show the Student Portal - show my scheduled class */
  public function myschedule()
  {
      return view('student-myschedule');
  }

  /** Request new exam, will also return with a list of instrumentType */
  public function requestexam()
  {
//      $syllabus = new Syllabus();
//      $instrumentType = $syllabus->getListOfInstruments(); //DB::table('syllabus')->select('InstrumentType')->distinct()->get();
//      $instrumentList = array_pluck($instrumentType, 'InstrumentType', 'InstrumentType');

//      foreach ($instrumentType as $instrumentType) {
//          $instrumentList[] = $instrumentType->InstrumentType;
//      }

//      $instrumentList = $instrumentType->InstrumentType;
//      $syllabus = DB::table('syllabus')->select('InstrumentType', 'GradeID', 'MusicName')->get(); // returns an array of stdObjects
//      foreach ($syllabus as $syllabus) {
//          $syllabusList[] = $syllabus->InstrumentType;
//      }

      return view('student-requestexam');/*, 'syllabus'*/ /*, compact('instrumentList')*/
  }
  public function registerexam()
  {
     // return view('sstudent-dashboard');
  }
Route::resource('exams','ExamsController',['only'=>['index','show', 'update']]);
//Route::get('exams/getListOfExamByStudent/{StudentID}', 'ExamsController@getListOfExamByStudent');



//->middleware('auth:api');


/*Route::get('/post-login', function () {
    return view('post-login-home');
});
*/

Route::group(['middleware' => ['web']], function () {
    Auth::routes();

//    Route::get('/studentportal', 'StudentPortalController@index');
    Route::get('home', 'HomeController@index');
    Route::get('student', 'StudentController@index');
    Route::post('/student', 'StudentController@registerexam');
    Route::get('examiner', 'ExaminerController@index');
    //function () {return view('welcome'); });

    Route::get('users/coaches/{gradeID}', 'UsersController@getCoaches');
    Route::post('user/update', 'UsersController@update');

    Route::get('appointments/getStudentAppointments', 'AppointmentsController@getStudentAppointments');
    Route::get('appointments/getStudentAppointmentsCalendar', 'AppointmentsController@getStudentAppointmentsCalendar');
    Route::get('appointments/getCoachAppointments', 'AppointmentsController@getCoachAppointments');
    Route::get('appointments/getCoachAppointmentsCalendar', 'AppointmentsController@getCoachAppointmentsCalendar');
    Route::resource('appointments', 'AppointmentsController', ['only'=>['store','show','update','destroy']]);

    Route::get('syllabi/getListOfMusicsWithinInstrumentGradeMusicType/{InstrumentID}/{GradeID}/{MusicTypeID}', 'SyllabiController@getListOfMusicsWithinInstrumentGradeMusicType');
    Route::get('syllabi/getListOfMusicsWithinInstrumentGrade/{InstrumentID}/{GradeID}', 'SyllabiController@getListOfMusicsWithinInstrumentGrade');
    Route::get('syllabi/getVideoLink/{ID}', 'SyllabiController@getVideoLink');

    Route::get('grades/above/{ID}', 'GradesController@getGradesAbove');

    Route::resource('syllabi','SyllabiController',['only'=>['index','store','show','update','destroy']]);

    Route::resource('musictypes', 'MusictypesController', ['only'=>['index','show']]);
    Route::resource('countries', 'CountriesController', ['only'=>['index','show']]);
    Route::resource('grades', 'GradesController', ['only'=>['index', 'show', 'update', 'store', 'destroy']]);
    Route::resource('instruments', 'InstrumentsController', ['only'=>['index','show']]);
    Route::resource('deliverymethods', 'DeliveryMethodsController', ['only'=>['index','show']]);

    Route::resource('cberoles', 'CBERolesController', ['only'=>['index']]);

    Route::group(['middleware' => 'admin'], function()
    {
        Route::get('admin', 'AdminController@index');
    });
/*
        Route::group(['middleware' => 'is.admin'], function()
        {
            Route::group(['prefix' => 'admin'], function()
            {
                // mysite.com/admin/dashboard
                get('dashboard',['as' => 'admin.dashboard', 'uses' => 'AdminController@dashboard']);

            });
        });
*/
});

//results
//appointments
//exams
//users

/*
Route::get('syllabi', function () {
    return App\Syllabus::all();
});
*/


Route::get('/', function () {
    retu
zachleigh's avatar

Your route should be like this:

Route::post('user/update', 'StudentController@methodName');
newbie14's avatar

Hi, I notice no changes you suggest for the controller. What I working on now is actually

newbie14's avatar

Hi Zacleigh, Good news I managed to resolve it I just added this now {{ csrf_field() }} and its working now ?

newbie14's avatar

Another thing I want to ask you based on the form you saw that I have uploaded on dropbox what is your opinion should I do it this way or should I restructure into individual pages for the each link ?

ElvisMagagula's avatar

@NEWBIE14 Subscribe to Laracasts, Jeff will get you up to speed in no time! That's where most of us fell inlove with this framework.

newbie14's avatar

Hi Elvis, How to subscribe on that is there any payment needed for that?

Gatemark's avatar

sorry i dont know how to write snippets in this forum

hey @newbie14 look take these steps

  1. In your web.php(route) write "Route::post('your url', 'yourControllerName@yourFunctionOrMethod')

NB: make sure the submit button is inside the form tag and your form action is 'your url@step 1'

i hope this helps you, if it havent, Holla me here

jlrdw's avatar

@newbie14 have you taken some of the beginner lessons offered for free or perhaps watch some of the Laracast?

1 like
MalaniDerrick's avatar

Route::get('/getOrgById', 'UserRegistrationController@getOrgById');

Please or to participate in this conversation.