Unable to seed data in Lumen 8 project - php

I am trying to run a seeder in a lumen 8 project but have been unsuccessful for a while now. Here's the folder structure view
When I run php artisan db:seed --class="Database\Seeds\SampleTableSeeder" or php artisan db:seed --class="Database\Seeders\Seeds\SampleTableSeeder" or php artisan db:seed --class=SampleTableSeeder it fails and returns a target class not found like
Target class [Database/Seeds/SampleTableSeeder] does not exist.
I have added the namespace to SampleTableSeeder to look like
<?php
namespace Database\Seeds;
use App\Models\Sample;
class SampleTableSeeder
{
// ....
}
And in DatabaseSeeder run function, I am calling it like this
$this->call('Database/Seeds/SampleTableSeeder');
What is the right way to run this particular seeder?

Database/Seeds will never work, because you have not declare it on the composer.json section...
See the original file, it has this section:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
}
You have to declare that new section:
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/",
"Database\\Seeds\\": "database/seeds/"
}
},
}
Then it will work, but I personally do NOT recommend to do that... you already have Database/Seeders...
Quick update, if running php artisan db:seed --class="Database\Seeds\SampleTableSeeder" does not work, try adding \ at the beginning, like this php artisan db:seed --class="\Database\Seeds\SampleTableSeeder"

Related

Laravel forge after each deploy having to use composer update

So I have a helper file: App\helpers.php, added in my JSON as follows:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
},
"files": [
"app/helpers.php"
]
},
In this helper file I have for example a simple userName() method for displaying the full name of a user:
function userName()
{
return auth()->user() ? auth()->user()->present()->name : '';
}
Everything works fine in my local environment. But everytime I push to my production environment, I get errors:
Call to undefined function userName()
Whenever in Laravel Forge I do a composer update after deploying, the error disappears and my website works flawlessly.
I have never had this issue with any other projects, and I am using the helpers.php file the exact same way.
My question is, why is this happening and of course my second question would be, how to solve this?
if you look at forge deploy script, you'll see the line:
$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader
the --no-dev flag will skip your autoload-dev rules, so you just need to move the helper reference into autoload instead of autoload-dev
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
"files": [
"app/Helpers.php"
]
},

Laravel Newly created seeder does not exist

I have a new Laravel Seeder but is giving a class does not exist error. The said seeder works in my local machine but not in production. The only file that I pushed was the seeder file itself.
File is in database\seeders
Filename: SeederName.php
Here is the code structure:
namespace Database\Seeders;
use App\Models\Model1;
use App\Models\Model2;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class SeederName extends Seeder
{}
When I run the command php artisan db:seed --class=SeederName, it returns a Target class [Database\Seeders\SeederName] does not exist.
One of the top answers given was to run the command composer dump-autoload However, when I run the said command, I am getting a Class Database\Seeders\SeederName located in ./database/seeders/SeederName.php does not comply with psr-4 autoloading standard. Skipping.
My composer.json
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
},
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
I also compared this new seeder with my other seeder file and code structure is the same.
I don't know why, but when I create a new seeder, now it works. Everything is the same except for the File Name and Class Name.

Namespaces not working with Symfony and Composer

I must be missing something ; I am trying to run some tests but the classes are never found due to namespace.
Here is my structure.
-app
-tests
-Unit
-TestInterface.php
-common
-MyTest.php
Here is my TestInterface.php:
namespace App\Tests\Unit;
interface TestInterface
{
}
Here is my MyTest.php:
namespace App\Tests\Unit\common;
use App\Tests\Unit\TestInterface;
class MyTest implements TestInterface
{
}
Here is the relevant part of composer.json:
"autoload": {
"psr-4": {
"App\\": "src/",
"spec\\": "spec/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
Here is the error:
PHP Fatal error: Interface 'App\Tests\Unit\TestInterface' not found
What am I missing here?
Answering myself, the code is fine.
I was loading PHPUnit from another.phar. Installed PHPUnit via composer:
composer require --dev phpunit/phpunit ^8
and used it from ./bin and it worked fine.

Composer load single class with namespace

I'm trying to use symfony flex with a bundle.
I have this directory structure
/
src/
AppBundle/
AppBundle.php
# Many classes
Kernel.php
I want to load Kernel.php class with this namespace App and classes inside AppBundle with the namespace AppBundle.
I've tried many composer configurations but I couldn't load them.
"psr-4": {
"AppBundle\\": "src/AppBundle/",
"App\\": "src/"
}
But I got errors like this:
Expected to find class "App\AppBundle\AppBundle" in file "/var/www/vhosts/flex/src/AppBundle/AppBundle.php"
UPDATE
src/Kernel.php class have a different namespace and I couldn't change it because other classes used it, the namespace is App. Some scripts call that class using use App\Kernel
src/AppBundle/AppBundle.php class has this namespace AppBundle
There is way to do this?
UPDATE 2
I sorted it out:
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle/"
},
"classmap": [
{ "App\\Kernel": "src/Kernel.php" }
]
},
I would suggest using a classmap for Kernel.php instead:
"autoload": {
"psr-4": { "AppBundle\\": "src/AppBundle" },
"classmap": [ "src/Kernel.php" ]
}
See https://getcomposer.org/doc/04-schema.md#classmap
You only need
"psr-4": { "App\\": "src/" }

Composer/Laravel Fatal Error

I added this to my composer.json file
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Claremontdesign\\Cdbase\\": "packages/Claremontdesign/cdbase/src",
"Claremontdesign\\Narbase\\": "packages/Claremontdesign/narbase/src",
"Claremontdesign\\Nhr\\": "packages/Claremontdesign/nhr/src"
},
"files": [
"packages/Claremontdesign/cdbase/src/Helpers/helpers.php",
"packages/Claremontdesign/narbase/src/Helpers/helpers.php",
"packages/Claremontdesign/nhr/src/Helpers/helpers.php"
]
},
then, I ran composer update from the command line, and it gave me this error:
symfony component debug exception fatalerrorexception class "Claremontdesign\Cdbase\ServiceProvider" not found
Has anyone else encountered this?
Also, I added a service provider in add.php
Claremontdesign\Cdbase\ServiceProvider::class
Did you try running just composer dump? composer update runs some scripts before it actually runs - for example php artisan clear-compiled. When artisan runs, it'll probably fail because it tries to register the serviceprovider which isn't autoloaded yet. composer dump only generates the autoload files, which is what you need in this case.
i think you are missing one more slash after every src folder
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Claremontdesign\\Cdbase\\": "packages/Claremontdesign/cdbase/src/",
"Claremontdesign\\Narbase\\": "packages/Claremontdesign/narbase/src/",
"Claremontdesign\\Nhr\\": "packages/Claremontdesign/nhr/src/"
},
"files": [
"packages/Claremontdesign/cdbase/src/Helpers/helpers.php",
"packages/Claremontdesign/narbase/src/Helpers/helpers.php",
"packages/Claremontdesign/nhr/src/Helpers/helpers.php"
]
},

Categories