Error Illuminate Database Eloquent Not Found - php

In the Controller when i tried to call a function from a model it Through Exception
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Class 'Illuminate\Database\Eloquent' not found
the controller is simple and i used to create name space to manage sub directories controllers and model
<?php
namespace Manage ;
use Illuminate\Support\Facades\View;
use Illuminate\Routing\Controller;
class BaseController extends Controller {
/**
* Setup the layout used by the controller.
*
* #return void
*/
protected $layout = 'manage.layouts.master';
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout)->with(Dashboard::all());
}
}
}
and model
<?php
namespace Manage ;
use Illuminate\Database\Eloquent;
class Dashboard extends Eloquent{
protected $table = 'admin_dashboard_sidebar';
//put your code here
}

The class is Model:
use Illuminate\Database\Eloquent\Model as Eloquent;
or just
use Eloquent;
This last one is an alias to the class you can find in your app/config/app.php.

use Illuminate\Database\Eloquent\Model;
class Dashboard extends Model{
}

Related

Laravel Model Can not Find Factory

I create model to another path
namespace Core\Entity;
use Core\Base\BaseEntity;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class News extends BaseEntity
{
use HasFactory;
And This is My seeder
use Core\Entity\News;
class NewsSeeder extends Seeder
{
public function run()
{
News::factory(500)->create();
}
And This is my factory
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* #extends \Illuminate\Database\Eloquent\Factories\Factory<\Core\Entity\News>
*/
class NewsFactory extends Factory
{
/**
* Define the model's default state.
*
* #return array<string, mixed>
*/
public function definition()
{
return [
];
}
}
Problem Is it Can not Find path of Factory , gives me like this ERROR
Class "Database\Factories\Core\Entity\NewsFactory" not found
How I can solve this problem?
Update your namespace in Factory class:
namespace Database\Factories;
to
namespace Database\Factories\Core\Entity;
And run composer dump-autoload

Load multiple models from single controller

need help laravel. I have 2 model in different folder
app\User
app\model\Role
there is no problem when i used at UsersController -> call app\User, or RolesController -> call app\model\Role
but, when i used both models on UsersController , the app\model\Role didnt work
==================== UsersController ======================
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use app\User;
use app\model\Role as Role;
use DataTables;
class UserController extends Controller
{
public function index(Request $request){
$data['title_page'] = 'User';
$data['roles'] = Role::all(); // this line show error
return view('admin/user', $data);
}
}
======================== app\model\Role ===================
namespace App\model;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $fillable = ['id','name'];
protected $tables = 'roles';
public function users(){
return $this->belongsTo('App\User','role');
}
}
======================== app\User =======================
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = "users";
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password','role', 'status'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function roles(){
return $this->hasOne('App\model\Role','id');
}
}
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class 'app\model\Role' not found
Try with capital 'A' on 'App'. Some systems are case sensitive. So use App\Model\Role depending on your folder name for model. IE if your model folder is lower case, match it in the use statement.
Also, you don't need the as keyword here unless there are conflicts - you may be fine with just use App\Model\Role without the as Role.
One more item, make sure the Role class is actually in the model folder and that your namespace is at the top of the php file correctly referencing the App\Model namespace. So in your Role class make sure the caps match your use call -
<?php
namespace App\Model

Laravel 5.5 - Class Custom BaseController not found but exists

Laravel 5.5 Custom BaseController not found even though it exists. Have checked the other questions on StackOverflow regarding the BaseController not found but they are referring to the default BaseController which isn't the same in mine case.
Here is my implementation
Controller.php
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as CheckController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends CheckController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
Custom BaseController (BaseController.php)
use App\Http\Controllers\Controller;
class BaseController extends Controller
{
/**
* Setup the layout used by the controller.
*
* #return void
*/
public $data = array();
public function __construct()
{
if (Sentinel::check()) {
// User is not logged in, or is not activated
$this->data['admin'] = Sentinel::getUser();
}
}
protected function setupLayout()
{
if (!is_null($this->layout)) {
$this->layout = View::make($this->layout);
}
}
}
Extending a class named HomeCtontroller to Custom BaseController
class HomeController extends BaseController {
protected $layout = 'master';
public function main()
{
...
}
}
And then it gives the following error
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Class 'BaseController' not found
Would appreciate any sort of pointers.
I believe you haven't included full namespaces.
Make sure in BaseController you have:
namespace App\Http\Controllers;
and in HomeController make sure you are using:
use App\Http\Controllers\BaseController;
all those controllers should be located in app/Http/Controllers directory.
If you are sure you have valid directories and namespaces run composer dump-autoload in console

Laravel 5 SQLSTATE[42S02]: Base table or view not found

I'm studing about Repository Design Pattern in Laravel and I'm using https://github.com/andersao/l5-repository to do it.
I think i install success in my project . But when i run code with repository i have some problem
SQLSTATE[42S02]: Base table or view not found: 1146 Table
'test.nhanviens' doesn't exist (SQL: select * from nhanviens)
Table in my database is Nhanvien not Nhanviens
Here in my code
NhanvienRepository.php
<?php
namespace App\Repositories;
use Prettus\Repository\Contracts\RepositoryInterface;
/**
* Interface NhanvienRepository
* #package namespace App\Repositories;
*/
interface NhanvienRepository extends RepositoryInterface
{
//
}
NhanvienRepositoryEloquent.php
<?php
namespace App\Repositories;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Repositories\NhanvienRepository;
use App\Entities\Nhanvien;
use App\Validators\NhanvienValidator;
/**
* Class NhanvienRepositoryEloquent
* #package namespace App\Repositories;
*/
class NhanvienRepositoryEloquent extends BaseRepository implements NhanvienRepository
{
/**
* Specify Model class name
*
* #return string
*/
public function model()
{
return Nhanvien::class;
}
/**
* Boot up the repository, pushing criteria
*/
public function boot()
{
$this->pushCriteria(app(RequestCriteria::class));
}
}
DataController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\nhanvien;
use App\Repositories\NhanvienRepository;
class DataController extends Controller
{
protected $repository;
public function __construct(NhanvienRepository $repository){
$this->repository = $repository;
}
public function DanhSach(){
var_dump($this->repository->all());
}
}
from App\Nhanvien.php Add this variable to the class:
protected $table = 'nhanvien';
Explanation: The "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the nhanvien model stores records in the nhanviens table.
As stated in the official Eloquent documentation you need to specifically set the table name in your Model definition. That is, in your App\Nhanvien.php file set the following:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Nhanvien extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'Nhanvien';
}
or use
protected $table = 'nhanvien';
instead if your table name is full lowercase.
Check your migration file, maybe you are using Schema::table, like this:
Schema::table('table_name', function ($table) {
// ... });
If you want to create a new table you must use Schema::create:
Schema::create('table_name', function ($table) {
// ... });
See the Laravel migration documentation for more information.
If you are using Schema::create then please provide the contents of your migration file.
In my case, I've got rid of similar error by executing the command
php artisan config:cache

Namespacing for seeders

I'm trying to see what I'm doing wrong for my seed file to not work with my namespacing properly and wanted to see if someone can spot what's not working in this code.
Error: PHP Fatal error: Class 'App\Models\Eloquent' not found in
/Users/me/Repositories/personal/project/app/models/Event.php
<?php
namespace App\Models;
class Event extends Eloquent {
protected $fillable = [];
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'events';
}
<?php
use App\Models\Event;
// Composer: "fzaninotto/faker": "v1.3.0"
use Faker\Factory as Faker;
class EventsTableSeeder extends Seeder {
public function run()
{
$faker = Faker::create();
foreach(range(1, 100) as $index)
{
Event::create([
]);
}
}
}
You need to point to Eloquent in the root namespace instead of Eloquent inside App\Models (which doesn't exist)
To do that either reference id with a backslash:
class Event extends \Eloquent {
or add a use statement:
use Eloquent
class Event extends Eloquent {

Categories