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 {
Related
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
I get the title error when I run command:
php artisan db:seed
My screenshot:
I have no idea where this problem comes from. I was searching for code examples and solution but I haven't found anything :(
ArticlesTableSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
// use Laracasts\TestDummy\Factory as TestDummy;
class ArticlesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
factory(App\Models\Article::class, 30)->create();
}
}
ArticleFactory.php
<?php
namespace Database\Factories;
use App\Models\Model;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ModelFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = App\Models\Article::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
return [
'title' => $faker->text(50),
'body' => $faker->text(200)
];
}
}
DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
$this->call(ArticlesTableSeeder::class);
}
}
Thank you in advance for your help!
In laravel 8 the default route namespace was removed.
Try to change:
ArticlesTableSeeder.php:
factory(App\Models\Article::class, 30)->create();
to:
\App\Models\Article::factory()->count(30)->create();
ArticleFactory.php:
protected $model = App\Models\Article::class;
to:
protected $model = \App\Models\Article::class;
and you will probably have to change:
'title' => $faker->text(50),
'body' => $faker->text(200)
to:
'title' => $this->faker->text(50),
'body' => $this->faker->text(200)
All suggestions that were mentioned here are correct.
Sometimes running composer require laravel/legacy-factories might fix your problem if you're using Laravel 8.
Also, in case you get an error that says Class 'Database\Factories\ArticleFactory' not found
then make sure you have class ArticleFactory extends Factory and not ModalFactory.
And make sure you're using HasFactory in the Article Model like here.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use HasFactory;
}
For more info: Laravel 8 Modal Factories
Try to change
factory(App\Models\Article::class, 30)->create();
to
App\Models\Article::factory()->count(30)->create();
ArticlesTableSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Article;
use Illuminate\Database\Seeder;
class ArticlesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Article::factory()->times(30)->create();
}
}
ArticleFactory.php
<?php
namespace Database\Factories;
use App\Models\Article;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ArticleFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = Article::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
return [
'title' => $this->faker->text(50),
'body' => $this->faker->text(200)
];
}
}
If you are using version 8 of laravel, look at the upgrade guide.
"Laravel's model factories feature has been totally rewritten to support classes and is not compatible with Laravel 7.x style factories. However, to ease the upgrade process, a new laravel/legacy-factories package has been created to continue using your existing factories with Laravel 8.x. You may install this package via Composer:
composer require laravel/legacy-factories"
https://laravel.com/docs/8.x/upgrade#seeder-factory-namespaces
change
factory(App\Models\Article::class, 30)->create();
to
\App\Models\Article::factory(30)->create();
I'm facing the same issue when I realized my model was not using the HasFactory trait.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'categories';
}
So make sure this trait is being used by your model.
If you are using version 8 of laravel you can directory use:
use App\Models\Article;
Article::factory()->count(30)->create();
composer require laravel/legacy-factories
php artisan db:seed
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
So I am trying to create a global scope in laravel 5.1 but just keep on getting the following error Trait 'Eloquent\Scopes\DependentTypeTrait' not found.
Here is my code:
App\Models\Eloquent\Scopes\DependentTypeTrait.php:
<?php namespace App\Models\Eloquent\Scopes;
trait DependentTypeTrait {
/**
* Boot the Active Events trait for a model.
*
* #return void
*/
public static function bootDependentTypeTrait()
{
static::addGlobalScope(new DependentTypeScope);
}
}
App\Models\Eloquent\Scopes\DependentTypeScope.php:
<?php namespace App\Models\Eloquent\Scopes;
use Illuminate\Database\Eloquent\ScopeInterface;
use Illuminate\Database\Eloquent\Builder;
class DependentTypeScope implements ScopeInterface
{
public function apply(Builder $builder)
{
$builder->where('vip_type_id', 2);
}
public function remove(Builder $builder)
{
$query = $builder->getQuery();
// here you remove the where close to allow developer load
// without your global scope condition
foreach ((array) $query->wheres as $key => $where) {
if ($where['column'] == 'vip_type_id') {
unset($query->wheres[$key]);
$query->wheres = array_values($query->wheres);
}
}
}
}
App\Models\Dependent.php:
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Eloquent\Scopes\DependentTypeTrait;
class Dependent extends Model
{
use DependentTypeTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = '<my table name>';
/**
* The actual primary key for the model.
*
* #var string
*/
protected $primaryKey = '<my primary key>';
}
I feel like I have the namespacing correct but its still saying it can't find the attribute....any ideas?
I think you have the use namespace wrong when trying to import your DependentTypeTrait:
<?php namespace App\Models\Eloquent\Scopes;
trait DependentTypeTrait { //...
And then you use it in App\Models\Dependent:
use Eloquent\Scopes\DependentTypeTrait;
Those namespaces don't match. Try this instead:
use App\Models\Eloquent\Scopes\DependentTypeTrait;
Or you can modify your composer.json to change how the Eloquent namespace is autoloaded:
"autoload": {
"psr-4": {
"Eloquent\\": "app/Models/Eloquent"
}
},
And don't forget to dump your autoloader: $ composer dump-autoload
Though specifically because you're using the name Eloquent which is ubiquitous in Laravel, I wouldn't go this route myself even if you would save yourself some typing. Laravel itself uses the namespace Illuminate\Database\Eloquent so you're probably safe from conflicts for now, but the possibility exists if Taylor changes things around or you use a third party library that stomps on it.
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{
}