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.
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();
}
}
?>
In the model, write the below code,
namespace App\models;
This would work , only missing namespace in your code, rest all code is perfect
you have imported your model wrongly
'm' of models is small
use App\models\Test;
this is the correct way:
use App\Models\Test;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With