Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object of class Illuminate\Routing\Route could not be converted to int

Tags:

php

laravel

I've got route like this:

Route::post('login', [
    'uses' => 'AuthController@postLogin',
    'before' => 'guest'
]);

but it doesn't work and I got only error: 'Object of class Illuminate\Routing\Route could not be converted to int'

I don't know exactly what I'm doing wrong.

My routes:

Route::group(['middleware' => ['auth, guest']], function () { 
   Route::get('/', array('as' => 'home', 'uses' => 'HomeController@getIndex')); 
   Route::get('/login', array('as' => 'login', 'uses' => 'AuthController@getLogin')) - 
   Route::post('login', [ 'uses' => 'AuthController@postLogin', 'before' => 'guest', ]); 
}); 

AuthController

public function postLogin() { 

   $rules = array('username' => 'required', 'password' => 'required'); 
   $validator = Validator::make(Input::all(), $rules); 

   if ($validator->fails()) { 

      return Redirect::route('login')->withErrors($validator); 
   } 

   $auth = Auth::attempt(array( 'name' => Input::get('username'), 'password' => Input::get('password'), ), false); 

   if (!$auth) { 
      return Redirect::route('login')->withErrors(array( 'Invalid credentials were provided', )); 

   } 
   return Redirect::route('home'); 

}
like image 507
yakuzafu Avatar asked Oct 31 '22 09:10

yakuzafu


2 Answers

I was able to replicate the error you got.

You've a typo in your routes. (Fix the semicolon on the first line.)

Change this:

Route::get('/login', array('as' => 'login', 'uses' => 'AuthController@getLogin'))-

Route::post('login', [
    'uses' => 'AuthController@postLogin',
    'before' => 'guest',
]);

To:

Route::get('/login', array('as' => 'login', 'uses' => 'AuthController@getLogin'));

Route::post('login', [
    'uses' => 'AuthController@postLogin',
    'before' => 'guest'
]);
like image 63
Jilson Thomas Avatar answered Nov 15 '22 06:11

Jilson Thomas


Also this error can happen sometimes if you use response() if you use it without any parameter, in that case using it with a empty string parameter will save you, like: response('')

like image 42
Sajjad Avatar answered Nov 15 '22 06:11

Sajjad