Reputation: 177
I am trying to authenticate my user with the help of Helpers For this purpose i have make Helper folder in app directory. Add the following lines of code to the composer.json
"files": [
"app/Helpers/UserHelper.php"
],
Make HelperServiceProvider.php in App\Provider directory, and use the following code in it.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
}
after this i have add alias in app.php as well as add provide like this
//this is an alias
'UserHelper' => App\Helpers\UserHelper::class,
//this is an provider
App\Providers\HelperServiceProvider::class,
My User model is
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $table='users';
protected $fillable =['username', 'password', 'firstname', 'lastname', 'email', 'phone', 'groupname', 'about', 'image'];
public static $login = [
'username' => 'required|',
'email' => 'required|',
'password' => 'required'
];
}
This my UserHelper
<?php namespace App\Helpers;
use Illuminate\Support\Facades\Auth;
class UserHelper {
public static function processLogin($inputs){
if(Auth::attempt($inputs)){
return TRUE;
} else {
return FALSE;
}
}
}
Here is my Login Function
<?php
namespace App\Http\Controllers;
use App\User;
use Input;
use Illuminate\Support\Facades\Validator as Validator;
use App\Helpers\UserHelper;
class LoginController extends Controller
{
public function login() {
$inputs = Input::except('_token');
$validator = Validator::make($inputs, User::$login);
if($validator->fails()){
print_r($validator->errors()->first());
} else {
$respones = \UserHelper::processLogin($inputs);
if($respones){
return 'loginView';
} else {
return 'not a user of our DB';
}
}
}
}
I have also updated my composer and after i login to application following error comes up , i am searching this for last 5 hour any help ?
Reards
Upvotes: 0
Views: 584
Reputation: 369
In your code you are extending the class User extends Model but when you are using auth functionality in laravel you need to extend the auth rather than model..
Keep Illuminate\Foundation\Auth\User and extends the model like this...
class User extends Authenticatable{
//code here
}
Upvotes: 1