Run Laravel Seeder From Package - laravel

I'm working on a package for an internal Laravel application and I'm having trouble running a seeder that exists in the package directory.
In my package's composer.json file located in packages/vendor/packagename, I've added the following:
"autoload": {
"psr-4": {
"Vendor\\PackageName\\": "src/",
"Vendor\\PackageName\\Database\\Factories\\": "database/factories/",
"Vendor\\PackageName\\Database\\Seeders\\": "database/seeders/"
}
},
I have the following file located in "packages/vendor/packagename/database/seeders/DepartmentSeeder.php"
<?php
namespace Vendor\PackageName\Database\Seeders;
use Illuminate\Database\Seeder;
use Vendor\PackageName\Models\Department;
class DepartmentSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Department::factory()->count(10)->create();
}
}
I then attempt to run the following command:
$ php artisan db:seed --class="Vendor\\PackageName\\Database\\Seeders\\DepartmentSeeder"
Target class [Vendor\PackageName\Database\Seeders\DepartmentSeeder] does not exist.
If I move the seeders directory into my src directory and run the following, it works, but I'd rather keep my seeders in my database directory.
$ php artisan db:seed --class="Vendor\\PackageName\\seeders\\DepartmentSeeder"
Does anyone have any idea why the class isn't being found? All my Google search results are purple and I even went to page two =/
Solution
In my particular case, composer dump-autoload wasn't doing the trick. What I ended up doing was running composer update vendor/packagename and whatever the issue was, it was resolved.
Hopefully, this helps anyone else who may have similar issues.

In my particular case, composer dump-autoload wasn't doing the trick. What I ended up doing was running composer update vendor/packagename and whatever the issue was, it was resolved.
Hopefully, this helps anyone else who may have similar issues.

Related

Cannot locate class of installed package

I'm writing a laravel package which contains spatie/laravel-sitemap.
I already included several external packages and I didn't encountered any issues, but for some reason I'm not able to integrate this one.
What I did is the usual:
composer require spatie/laravel-sitemap
Then I have created a Console command that have as handle method the following content:
public function handle()
{
SitemapGenerator::create(config('app.url'))
->configureCrawler(function (Crawler $crawler) {
$crawler->ignoreRobots();
})
->writeToFile(public_path('sitemap.xml'));
$this->line('<info>Sitemap generated');
}
when I execute the command registered as:
php artisan myapp:sitemap
I get:
Class "Spatie\Sitemap\SitemapGenerator" not found
The reference imported are:
use Spatie\Crawler\Crawler;
use Spatie\Sitemap\SitemapGenerator;
I also tried composer update and composer dump-autoload, same problem.
Any help?
register package class in providers array in config/app.php
Spatie\Sitemap\SitemapServiceProvider;
in the bottom of app.php file
i hope it was useful.
you can publish package using this.
php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config
then
composer dump-autoload
for more details please check the document https://github.com/spatie/laravel-sitemap under Configuration

Service Provider not booting in Laravel 5.8

I've created a new service provider to observe a model (App\Providers\EloquentEventServiceProvider.php), like so:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Staff;
use App\Oberservers\StaffObserver;
class EloquentEventServiceProvider extends ServiceProvider
{
public function boot()
{
Staff::observe(StaffObserver::class);
}
}
I've also added it to the config file (config\app.php):
return [
...
'providers' => [
...
App\Providers\EloquentEventServiceProvider::class,
...
]
...
]
The observer methods aren't working though. If I move Staff::observe(StaffObserver::class); to the AppServiceProvider class, it works just fine. So clearly this is an issue with getting my service provider to boot. I've tried php artisan config:clear, php artisan clear-compiled, composer update and composer dump but none have work. Any help is greatly aprpeciated.
your Oberservers name is wrong, as mentioned in the laravel doc observers laravel doc it should be Observers which means all of your observers should be within App\Observers instead of App\Oberservers.
so here we have 2 solutions :
1- if you want to keep the namespace App\Oberservers, you should run these 2 commands below because autoloading of the files may not work properly because we created a new folder Oberservers:
# Autoloading of files
composer dump
# Configure the cache
php artisan config:cache
2- the second solution is to just rename your actual Oberservers folder to Observers, in that way the autoloading of files will work well.

Class 'Tests\DuskTestCase' not found in ExampleTest.php

I am trying to run Laravel Dusk tests, but when I run the test, a new Chrome tab pops up with this message.
Fatal error: Class 'Tests\DuskTestCase' not found in path-to-project\tests\Browser\ExampleTest.php on line 9
All I have done so far is run composer require --dev laravel/dusk:^1.0 and php artisan dusk:install.
This is my ExampleTest.php (exactly how Laravel set it up)
<?php
namespace Tests\Browser;
use Laravel\Dusk\Chrome;
use Tests\DuskTestCase;
use Laravel\Dusk\DuskServiceProvider;
class ExampleTest extends DuskTestCase
{
/**
* A basic browser test example.
*
* #return void
*/
public function testBasicExample()
{
$this->browse(function ($browser) {
$browser->visit('/')
->assertSee('Laravel');
});
}
}
DuskTestCase.php is also just as Laravel set it up and has the namespace namespace Tests;.
I am using Laravel 5.4 and Dusk 1.0. I am running the test through PhpStorm, using the work around described here.
Anyone know why DuskTestCase can't seem to be found, even though it appears to be set up correctly? Thanks in advance.
In composer.json:
add "Tests\\": "tests/" in
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Tests\\": "tests/"
}
},
then, run composer dump-autoload to reload your packages.
I had this error due to using out-of-date docs that didn't include this line:
$ php artisan dusk:install
If composer dump-autoload does not solve problem, you can try these steps.
Visit homepage in your browser and check if it renders properly. If not, then you probably have a problem with your webserver configuration (Hint: isn't your project subdirectory of document root?).
You can try Laravel inbuilt server via php artisan serve. If homepage is accessible in your browser now, then you can try dusk again.
In that case, remember to update your .env file to match APP_URL=http://127.0.0.1:8000,
and run php artisan dusk from another cli window, cause php artisan serve needs to be running also.
if you test using a phpstrom then u have set path of phpunit ......
in settings/languages & framework/php/test frameworks and use composer autoloader path and then select a path of your laravel dusk project with autoload.php file.....
set file a vendor/autoload.php file in path to script...

Laravel 5.5 not found class with correct namespace

My ./app folder looks like:
+-- app
+-- Classes
+-- Events
+-- EventBase.php
+-- EventX.php
There's nothing secret with EventX file:
<?
namespace App\Classes\Events;
class EventX {
// ...
}
EventBase.php represents a Facade that inside it I just try to instantiate an EventX:
public function someMethod() {
new \App\Classes\Events\EventX;
// ...
}
After this line, Framework throw an exception telling that class was not found:
Symfony\Component\Debug\Exception\FatalThrowableError (E_ERROR)
Class 'App\Classes\Events\EventX' not found
Even that:
file_exists(__DIR__ . '\EventX.php'); // true
I already had this issue before when trying to create Facades and solved by moving class file from his current directory and after moving back (yeah, I don't why but it worked).
Something tells me that this is an issue of autoload process, so I tried these command (but still not working):
php artisan cache:clear
php artisan clear-compiled
php artisan config:clear
composer dump-autoload
What can I do in order to investigate the problem?
I think the problem is with the php tag <?
<?php
namespace App\Classes\Events;
class EventX {
// ...
}
PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).
Link
You need to add the new namespace to your composer.json to the key "psr-4"
"psr-4": {
"App\\": "app/",
"Classes\\": "app/classes/"
otherwise composer can't detect your new namespace
Another approach can be found here:
https://stackoverflow.com/a/28360186/6111545

Seeding from a package / Fixing ReflectionException

I have created a package which copies migrations and seeds to the respective directories in the base app.
However, unless I call (add) these within the run() function in the DatabaseSeeder class in the base app (app/database/seeds/DatabaseSeeder.php) when I run
php artisan migrate:refresh --seed
Then the seeds are not run. How can I automate this part so the user doesn't have to manually edit this file? Are there any artisan commands to manually run seeds?
Still don't completely understand how I got this to work, but for others;
In your boot method of your PackagenameServiceProvider use $this->publishes() to move the seeds and migrations to the base app
public function boot()
{
if (!$this->app->routesAreCached()) {
require __DIR__.'/../routes.php';
}
$this->publishes([
__DIR__.'/../DatabaseStuff/Migrations' => $this->app->databasePath().'/migrations',
__DIR__.'/../DatabaseStuff/Seeds/Example1Seeder.php' => $this->app->databasePath().'/seeds/Example1Seeder.php',
__DIR__.'/../DatabaseStuff/Seeds/Example2Seeder.php' => $this->app->databasePath().'/seeds/Example2Seeder.php',
]);
}
Have a copy of DatabaseSeeder with call methods pointing to the seeds you wish to run but make sure it's name-spaced correctly to where ever you have the files in your package.
namespace ExampleVendor\ExamplePackage\DatabaseStuff;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
$this->call(\Example1Seeder::class);
$this->call(\Example2Seeder::class);
Model::reguard();
}
}
Now run the following commands. The critical command for me in this case was php artisan optimize
php artisan vendor:publish
composer dump-autoload
php artisan cache:clear
php artisan optimize
php artisan db:seed --class="ExampleVendor\ExamplePackage\DatabaseStuff\DatabaseSeeder"
Without php artisan optimize I kept getting
ReflectionException
Class ExampleVendor\ExamplePackage\DatabaseStuff\DatabaseSeeder does not exist
Hopefully this helps someone in the future.

Resources