Home > OS >  Class "\App\Models\Admin" not found getting error when trying to login or register admi
Class "\App\Models\Admin" not found getting error when trying to login or register admi

Time:06-05

I am deploying a laravel9 project on hostinger cpanel,everying was fine till deployment and lending page,but when trying to register new user or login the user, facing below shared error, thankfully during registration process data getting save in database but dashboard loading with below shared error:

Class "\App\Models\Admin" not found in app / Http / Controllers /Admin / AdminController.php : 109

Below is the code for your reference:

AdminController.php

Folder path : app\Http\Controllers\Admin\AdminController.php

<?php


namespace App\Http\Controllers\Admin;

use App\Providers\RouteServiceProvider;
//use Closure;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Admin;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{
    //
    function log(Request $request)
    {
        $user= Admin::where('email', $request->email)->first();
        // print_r($data);
            if (!$user || !Hash::check($request->password, $user->password)) {
                return response([
                    'message' => ['These credentials do not match our records.']
                ], 404);
            }
             $token = $user->createToken('my-app-token')->plainTextToken;
        
            $response = [
                'user' => $user,
                'token' => $token
            ];
        
             return response($response, 201);
    }
    public function index()
    {
        return Admin::all();
    }
    public function store(Request $request)
    {   
        $request->validate([
            'name'=> 'required',
            'type'=> 'required',
            'vendor_id'=>'required',
            'email'=> 'required'
        ]);

        return Admin::create($request->all());
    }
    
    public function show($id)
    {
        //return Admin::find($type);
        return Admin::where('name',$id)->get();
    }
    
    public function update(Request $request, $id)
    {
        $product= Admin::find($id);
        $product->update($request->all());
        $result=$product->save();
        if($result)
        {
            return ['result'=>'Data has been updated'];
        }
        else
        {
            return ['result'=>'update operation has been failed'];
        }
        return $product;
    }

    public function delete($id)
    {
        $product= Admin::find($id);
        $result=$product->delete();
        //$result=$product->save();
        if($result)
        {
            return ['result'=>'Data has been deleted'];
        }
        else
        {
            return ['result'=>'update operation has been failed'];
        }
        return $product;
    }

    public function dashboard(){
        return view('admin/dashboard');
    }
    public function addadminuser(){
        return view('admin/users/add_admin_user');
    }
    public function updateadminpassword(){
        //echo"<pre>"; print_r(Auth::guard('admin')->user());die;
        $adminDetails = Admin::where('email',Auth::guard('admin')->user()->email)->first()->toArray();
        return view('admin.settings.update_admin_password')->with(compact('adminDetails'));
    }
    public function login(Request $request){
        if($request->isMethod('post')){
            $data = $request->all();
            echo "<pre>"; print_r($data); die;

            $validated = $request->validate([
                'email' => 'required|email|max:255',
                'password' => 'required',
            ]);
            if(Auth::guard('admin')->attempt(['email'=>$data['email'],'password'=>$data['password'],'status'=>1])){
                return redirect('admin/dashboard');
            }else{
                return redirect()->back()->with('error_message','Invalid Email or Password');
            }
        }
        return view('admin/login');
    }
    public function logout (){
        Auth::guard('admin')->logout();
        return redirect('admin/login');
    }
}

admin.php Folder Path: app\Models\admin.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class Admin extends Authenticatable
{
    use HasApiTokens,HasFactory,Notifiable;
    protected $guard = 'admin';
    protected $fillable=[
        'name',
        'type',
        'vendor_id',
        'email',
        'password',
        'mobile',
        'image',
        'status'
    ];
};

Error Screenshot-1

Error Screenshot of Line No.Highlighted

Note: Same Cond is working fine on localhost, error coming Hostinger Shared Hosting deployment Only, Any help will be highly appreciated.

Route Added:

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');

require __DIR__.'/auth.php';

Route::prefix('/admin')->namespace('App\Http\Controllers\Admin')->group(function(){
    // Admin Login Route
    Route:: match(['get','post'],'login','AdminController@login');
    Route::group(['middleware'=>['admin']], function(){
        // Admin Dasboard Route
        Route:: get('dashboard','AdminController@dashboard');
        // Update Admin Password
        Route::match(['get','post'],'update-admin-password','AdminController@updateadminpassword');
        // Add Admin User
        Route::get('add-admin-user','AdminController@addadminuser');
        //Admin Logout
        Route::get('logout','AdminController@logout');
    });
    

});

CodePudding user response:

Rename admin.php as Admin.php, as Psr4 (Autoloader) dictate

All class names MUST be referenced in a case-sensitive fashion.

Laravel expect to load all classes in PascalCase

https://www.php-fig.org/psr/psr-4/

https://dev.to/dalelantowork/php-laravel-8-best-naming-conventions-for-apis-23i0

  • Related