I added a seeder (copied from elsewhere and pasted) to my application and included the call in the Database Seeder run() function. I get the exception above even though the class exists.
I suspected that maybe some files may have been cached so i cleared the application cache but i still get the same error.
DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
// $this->call(CustomersTableSeeder::class);
$this->call(RolesTableSeeder::class);
$this->call(ManagerStatesTableSeeder::class);
$this->call(ManagersTableSeeder::class);
$this->call(CountsTableSeeder::class);
$this->call(CategoriesTableSeeder::class);
}
}
Seeder file CategoriesTableSeeder.php
<?php
use Illuminate\Database\Seeder;
class CategoriesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
\DB::table('categories')->insert([
[
'description' => 'Perfumes and Deo',
'slug' => 'perfumes-and-deo',
'parent' => 0,
'level' => 1,
'cna' => '2|',
'created_at' => \Carbon\Carbon::now(),
'updated_at' => \Carbon\Carbon::now(),
],
[
'description' => 'Perfumes',
'slug' => 'perfumes',
'parent' => 1,
'level' => 2,
'cna' => NULL,
'created_at' => \Carbon\Carbon::now(),
'updated_at' => \Carbon\Carbon::now(),
]
]);
}
}
Error:
ReflectionException : Class CategoriesTableSeeder does not exist
at C:\wamp\www\ma-sales-tracker\vendor\laravel\framework\src\Illuminate\Container\Container.php:788
Exception trace:
1 ReflectionClass::__construct("CategoriesTableSeeder")
C:\wamp\www\ma-sales-tracker\vendor\laravel\framework\src\Illuminate\Container\Container.php:788
2 Illuminate\Container\Container::build("CategoriesTableSeeder")
C:\wamp\www\ma-sales-tracker\vendor\laravel\framework\src\Illuminate\Container\Container.php:667
Any ideas on what might be causing this? Thanks in advance guys
I run Composer dump-autoload and voila! Worked as a charm. Also as suggested by Alex Mac, always generate Seeders with artisan commands.
Whenever you create any new file, always run following commands :-
composer dumpa // composer dump-autoload
php artisan optimize:clear // php artisan optimize:clear
because in bootstrap folder it keeps a track of each files and other configurations, etc.
Related
I am facing an issue with laravel 7 while truncating table, even I have used FOREIGN_KEY_CHECKS enable and disable still is return this type of error "Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint".
Method
DB::statement("SET FOREIGN_KEY_CHECKS=0;");
Artisan::call('db:seed', ["--database" => 'DBNAME', '--force' => true, "--class" => 'StatusTableSeeder']);
DB::statement("SET FOREIGN_KEY_CHECKS=1;");
Seeder File StatusTableSeeder.php
public function run()
{
\DB::table('statuses')->truncate();
\DB::table('statuses')->insert(array (
0 =>
array (
'id' => 1,
'name' => 'Current',
'type' => 'current',
),
1 =>
array (
'id' => 2,
'name' => 'Resolved',
'type' => 'resolved',
),
));
}
I have updated laravel version 6 to 7, this syntax works fine in laravel 6 but when I update it to laravel 7 then after it is working. If anyone Have idea about it what's the actual issues
try this:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class StatusTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Schema::disableForeignKeyConstraints();
DB::table('statuses')->truncate();
Schema::enableForeignKeyConstraints();
// and the rest of your code...
}
}
I'm following the official documentation for Laravel 5.7 on the events registration and generation: https://laravel.com/docs/5.7/events#generating-events-and-listeners
I have an EventServiceProvider with the following events defined:
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event handler mappings for the application.
*
* #var array
*/
protected $listen = [
'App\Events\FormBeforeCreate' => [
'App\Listeners\WebhookBeforeCreate',
],
'App\Events\FormAfterCreate' => [
'App\Listeners\NotifyAfterCreate',
'App\Listeners\WebhookAfterCreate',
],
'App\Events\FormBeforeUpdate' => [
'App\Listeners\WebhookBeforeUpdate',
],
'App\Events\FormAfterUpdate' => [
'App\Listeners\NotifyAfterUpdate',
'App\Listeners\WebhookAfterUpdate',
],
'App\Events\FormBeforeDelete' => [
'App\Listeners\WebhookBeforeDelete',
],
'App\Events\FormAfterDelete' => [
'App\Listeners\NotifyAfterDelete',
'App\Listeners\WebhookAfterDelete',
],
'App\Events\FormBeforeSave' => [
'App\Listeners\WebhookBeforeSave',
],
'App\Events\FormAfterSave' => [
'App\Listeners\NotifyAfterSave',
'App\Listeners\WebhookAfterSave',
],
];
/**
* The subscriber classes to register.
*
* #var array
*/
protected $subscribe = [
'App\Listeners\UserEventSubscriber',
];
/**
* Register any other events for your application.
*
* #return void
*/
public function boot()
{
parent::boot();
}
}
The error:
When I run the command php artisan event:generate I get the following error:
PHP Fatal error: Call to a member function listens() on null in /app/vendor/laravel/framework/src/Illuminate/Foundation/Console/EventGenerateCommand.php on line 35
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to a member function listens() on null
According to the doc, it should do this:
This command will generate any events or listeners that are listed in
your EventServiceProvider. Events and listeners that already exist
will be left untouched
I don't understant what I've missed since I didn't find any similar error by searching the web
This is the line that is returning null:
$providers = $this->laravel->getProviders(EventServiceProvider::class);
therefore, there are some problems with your EventServiceProvider... please, try using this:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'App\Events\FormBeforeCreate' => [
'App\Listeners\WebhookBeforeCreate',
],
'App\Events\FormAfterCreate' => [
'App\Listeners\NotifyAfterCreate',
'App\Listeners\WebhookAfterCreate',
],
'App\Events\FormBeforeUpdate' => [
'App\Listeners\WebhookBeforeUpdate',
],
'App\Events\FormAfterUpdate' => [
'App\Listeners\NotifyAfterUpdate',
'App\Listeners\WebhookAfterUpdate',
],
'App\Events\FormBeforeDelete' => [
'App\Listeners\WebhookBeforeDelete',
],
'App\Events\FormAfterDelete' => [
'App\Listeners\NotifyAfterDelete',
'App\Listeners\WebhookAfterDelete',
],
'App\Events\FormBeforeSave' => [
'App\Listeners\WebhookBeforeSave',
],
'App\Events\FormAfterSave' => [
'App\Listeners\NotifyAfterSave',
'App\Listeners\WebhookAfterSave',
],
];
/**
* Register any events for your application.
*
* #return void
*/
public function boot()
{
parent::boot();
//
}
}
My bad, it seems like I was really tired yesterday night, Our project runs on Docker, I was running the command outside of the docker instead of inside.
I have no idea why it showed this bug in particular but once I ran the command in the docker all files generated correctly.
I have a laravel app with passport installed to manage api auth. I'm trying to write some tests but I'm unable to create a Client as per the docs on laravel. I've googled similar SO answers but they all suggest using setUp and tearDown methods which I am doing. When I run the test I get
InvalidArgumentException
Unable to locate factory for [Laravel\Passport\Client].
How can I get this to work?
Below is my code. I have included the Client model from the passport package and I am using the setUp and tearDown methods as suggested in similar SO answers.
I've tried composer dump-autoload and php artisan config:cache.
use Laravel\Passport\Passport;
use Laravel\Passport\Client;
...
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
}
protected function tearDown(): void
{
parent::tearDown();
}
public function testAPIEndpointFailsWhenNoParamIsSet()
{
Passport::actingAsClient(
factory(Client::class)->create(),
['*']
);
$response = $this->postJson('/api/endpoint', [
'param' => ''
]);
$response->assertStatus(401)
->assertJson(['message' => 'Unauthenticated.']);
}
passport client factory should be existed on publishing ...
if it did not .... make it your self:
from : here
use Laravel\Passport\Passport;
use Laravel\Passport\Client;
$factory->define(Client::class, function (Faker $faker) {
return [
'user_id' => null,
'name' => $faker->company,
'secret' => Str::random(40),
'redirect' => $faker->url,
'personal_access_client' => 0,
'password_client' => 0,
'revoked' => 0,
];
});
I'm having troubles trying to test a plugin using fixtures.
I've created the fixtures files under tests/Fixture with the structure as documentation:
namespace Files\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
/**
* FiledFixture
*
*/
class FiledFixture extends TestFixture {
/**
* Table name
*
* #var string
*/
public $table = 'filed';
/**
* Fields
*
* #var array
*/
public $fields = [
'id' => [
'type' => 'integer',
'length' => 11,
...
],
'foreign_key' => [
'type' => 'integer',
...
],
...
];
/**
* Records
*
* #var array
*/
public $records = [
[
'id' => 1,
'foreign_key' => 1,
'file_id' => 1,
'filedtype_id' => 1,
'model' => 'Lorem ipsum dolor sit amet',
'language' => 'Lore',
'created' => '2014-11-01 19:21:54',
'modified' => '2014-11-01 19:21:54'
],
];
}
And then in the TestCase i load the fixtures with the public variable fixtures:
<?php
namespace Files\Test\TestCase\Model\Table;
use Cake\ORM\TableRegistry;
use Files\Model\Table\FiledTable;
use Cake\TestSuite\TestCase;
/**
* Files\Model\Table\FiledTable Test Case
*/
class FiledTableTest extends TestCase {
/**
* Fixtures
*
* #var array
*/
public $fixtures = [
'plugin.files.filed',
'plugin.files.files',
'plugin.files.filedtypes',
'plugin.files.thumbs'
];
...
When I try to run phpunit I get the next error:
..Exception: Referenced fixture class "Files\Test\Fixture\FiledFixture" not found. Fixture "plugin.files.filed" was referenced in test case "Files\Test\TestCase\Model\Table\FiledTableTest". in [/home/genar/src/metropolitan-web-2015/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureManager.php, line 180]
I tryed to create a custom bootstrap.php for testing but I cannot figure what has to be there. I copyed this one https://github.com/Xety/Cake3-Upload/blob/master/tests/bootstrap.php but it raises a lot of errors.
I've googled as well but I cannot find any information about testing on plugins, nor on the oficial documentation.
Any thoughts on how can I achieve this?
Well, finally I suposed the error was on the way composer autoloaders are generated. I've been looking how my Test\Fixture\ namespace was not added on autoload_psr4.php either running composer dump-autoload.
So the way I fixed this it was adding the right lines on composer.json of the plugin and running composer update to generate the autoloads well.
Note composer dump-autoload does not update your plugin psr4 routes when the plugin is intalled under vendors.
thanks for read.
I was creating a model in Laravel 4 while run dump-autoload with Artisan and everything stop working.
I was searching a lot how to fix this problem but i cant find any solution.
I was uninstall Laravel and install again, the problem solved but when i repeat the process, the problem appears again.
Sorry about my english.
The error:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Call to undefined method Illuminate\Routing\Router::deleted()
The line:
\vendor\laravel\framework\src\Illuminate\Routing\Router.php:281
$this->{$route['verb']}($route['uri'], $action);
My model:
<?php
use LaravelBook\Ardent\Ardent;
class Programacion extends Ardent {
/**
* Ardent validation rules
*/
public static $rules = array(
'autor' => 'required',
'etiquetas' => 'required',
'sala_id' => 'required|integer',
'genero_id' => 'required|integer',
'teatro_id' => 'required|integer',
'img_portada' => 'required',
'img_miniatura' => 'required',
'estado' => 'required|integer',
'referencia' => 'required'
);
}
My method in controller:
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function postCreate()
{
$programacion = Programacion::create(
Input::all()
);
//$post = Input::all();
}
I hit this problem yesterday. My problem was that a Controller class had the same name that a Model class. I renamed the Controller class, ran php artisan dump-autoload and it worked again.