hello world
hello world

Reputation: 1755

Class Not Found Error in Laravel 5.1

Hey guys I'm trying to learn PHP frameworks as well as OOP and I'm using Laravel 5.1 LTS.

I have the following code in my AuthController

<?php

namespace App\Http\Controllers\Auth;

use App\Verification;
use Mail;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    private $redirectTo = '/home';
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    protected function create(array $data){
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        // generate our UUID confirmation_code
        mt_srand((double)microtime()*15000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $uuid = substr($charid, 0, 8)
        .substr($charid, 8, 4)
        .substr($charid,12, 4)
        .substr($charid,16, 4)
        .substr($charid,20,12);

        $data['confirmation_code'] = $uuid;

        // pass everything to the model here 
        $setVerification = new Verification();
        $setVerification->setVerificationCode($data['email'], $data['confirmation_code']);

        // send email for confirmation
        Mail::send('email.test', $data, function ($m) use ($data){
            $m->from('[email protected]', 'Your Application');
            $m->to($data['email'])->subject('Thanks for register! Dont forget to confirm your email address');
        });

        return $user;

    }

}

my error message Class 'Models\Verification' not found is coming from this piece of code here

// pass everything to the model here 
$setVerification = new Verification();
$setVerification->setVerificationCode($data['email'], $data['confirmation_code']);

which looks right to my beginner's eyes, but it's clearly wrong.

Here is my Verification class that has the setVerificationCode method

<?php 

namespace App\Http\Controllers;

use App\User;
use DB;
use App\Http\Controllers\Controller;

class Verification {

    /**
     * This method will update the confirmation_code column with the UUID
     * return boolean
     **/
    protected function setVerificationCode($email, $uuid) { 
        $this->email = $email;
        $this->uuid = $uuid;

        // check to see if $email & $uuid is set    
        if (isset($email) && isset($uuid)) {
            DB::table('users')
                    ->where('email', $email)
                    ->update(['confirmation_code' => $uuid]);

            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * This method will validate if the UUID sent in the email matches with the one stored in the DB
     * return boolean
     **/
    protected function verifyConfirmationCode() {

    }


}

Upvotes: 2

Views: 808

Answers (2)

Qazi
Qazi

Reputation: 5135

its seems that, you are missing something, which, Extend your Model with eloquent model

use Illuminate\Database\Eloquent\Model;

class Verification extends Model
{

and the rest is seems fine.

also share your verification model code


Updated


instead of your this line

use App\Verification;

do this

use App\Models\Verification;

as you created custom directory for your Models then its better to auto load it in your composer.json file. add this line "app/Models" in your "autoload" section. follow this

"autoload": {
    "classmap": [
        "database",
        "app/Models"

    ],

and after that, run this command in your project repo composer dump-autoload

Upvotes: 1

Haseena P A
Haseena P A

Reputation: 17376

Please give the following in AuthController

use App\Http\Controllers\Verification;

instead of

use App\Verification;

If we give use App\Verification , it will check if there is any model named Verification.

Upvotes: 6

Related Questions