Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class 'App\models\Test' not found when try to access model

Tags:

php

laravel

I am try to learn laravel framework.

when i am try to use model at that time i got some error.

In my test controller

 <?php 
        namespace App\Http\Controllers;

        use App\models\Test;

        class TestController extends Controller {

            public function __construct()
            {
                $this->middleware('guest');
            }

            public function editData($id) {
                $result = DB::select('select * from users where id = ?', array($id));
                $data['data'] = $result[0];
                return view('myview', $data);
            }

       }
?>

and in my model

model path is app/models/Test.php

model name is Test.php

<?php
use Illuminate\Database\Eloquent\Model;
class Test extends Model {
    protected $table = 'users';
    public static function getMyData(){

         $user = Test::find();

    }
}
?>

Output :

FatalErrorException in TestController.php line 56:
Class 'App\models\Test' not found

I am also try this command.

composer dump-autoload

But is not working.

like image 383
Hardik Ranpariya Avatar asked Feb 10 '15 12:02

Hardik Ranpariya


3 Answers

Your Test model isn't in any namespace at all. You would need to reference it by:

use Test;

or:

\Test::getMyData();

Or just put it in the namespace you try to reference it now:

<?php
namespace App\models;

use Illuminate\Database\Eloquent\Model;
class Test extends Model {
    protected $table = 'users';
    public static function getMyData(){
        $user = Test::find();
    }
}
?>
like image 175
lukasgeiter Avatar answered Nov 20 '22 00:11

lukasgeiter


In the model, write the below code,

namespace App\models;

This would work , only missing namespace in your code, rest all code is perfect

like image 3
Udhav Sarvaiya Avatar answered Nov 20 '22 00:11

Udhav Sarvaiya


you have imported your model wrongly
'm' of models is small

use App\models\Test;

this is the correct way:

use App\Models\Test;
like image 2
Janakthegamer Avatar answered Nov 20 '22 01:11

Janakthegamer