Calling additional seeders in OctoberCMS fails with class not found error - php

I have seeder classes that I confirmed they could run individually, and I'm trying to run them from another seeder class in the same namespace.
I'm getting an error saying that the class run from another seeder is not found.
I'm using OctboerCMS build 419.
Seeder Class calling subseeder
<?php namespace Cocci\Custom\Updates;
use Seeder;
class SeedTablesCocciPedale extends Seeder
{
public function run()
{
$this->call('Cocci\Custom\Updates\SeedTablesCocciBike005');
$this->call('Cocci\Custom\Updates\SeedTablesCocciColor');
// $this->call(SeedTablesCocciBike005::class);
// $this->call(SeedTablesCocciColors::class);
}
}
Seeder called by another seeder
<?php namespace Cocci\Custom\Updates;
use Seeder;
use Cocci\Custom\Models\Product;
use Cocci\Custom\Models\Part;
use Cocci\Custom\Models\PreviewType;
use Cocci\Custom\Models\PartType;
use Cocci\Custom\Models\PartPreviewPosition;
class SeedTablesCocciBike005 extends Seeder
{
public function run()
{
$partTypeSetAll = PartType::create([
'name' => 'set_all',
'category' => PartType::CAT_ALL_PARTS,
]);
$partTypeNoPaintParts = PartType::create([
'name' => 'no_paint_parts',
'category' => PartType::CAT_NON_CUSTOMIZABLE,
]);
...
}
}
Error
% php artisan plugin:refresh Cocci.Custom
Rolled back: Cocci.Custom
Reinstalling plugin...
PHP Fatal error: Class 'Cocci\Custom\Updates\SeedTablesCocciBike005' not found in /Library/WebServer/Documents/cocci-custom/vendor/laravel/framework/src/Illuminate/Database/Seeder.php on line 62
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Cocci\Custom\Updates\SeedTablesCocciBike005' not found
I tried both ways specifying the class by string of fully qualified name and with class keyword, but neither worked. Probably these seeder classes are not loaded?
What should I do?
Thanks in advance.

When using a seeder class to call additional seeder classes, you need to make sure you're autoloading the seeder classes.
Assuming you're seeder classes are called seed_tables_cocci_bike_005.php and seed_tables_cocci_color.php, you can add these files to you're classmap dev autoloader as follows.
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"tests/UiTestCase.php",
"tests/PluginTestCase.php",
"plugins/cocci/custom/updates/seed_tables_cocci_bike_005.php",
"plugins/cocci/custom/updates/seed_tables_cocci_color.php"
]
}
Remember to composer dumpautoload once your composer.json file has been updated.

Related

Class Not Found in PHP Laravel 6.x when another class extends

I always get the class not found exception in php laravel 6 when i create a class and extend a parent class named A which is located in the same directory.
However, another child class that is located in same directory could extend class A successfully.
In addition, i couldn't also instantiate the class A due to class not found exception in another .php file.
Please help me on this.
Thanks in an advance.
Parent class: myContext
<?php
namespace config\models;
class myContext {
public static $conn;
...
}
Class myUser: extension is fine.
<?php
namespace config\models;
class myUser extends myContext {
private $name;
...
}
Class oauth: extension returns myContext class not found.
<?php
namespace config\models;
class oauth extends myContext {
private $user;
}
Instantiate the class - returns class not found.
<?php
use config\models\myContext as context;
$cont = new context();
Check whether the namespace is added correctly when the parent class is imported.
Refer
https://www.quora.com/What-is-namespaces-and-use-in-Laravel
external classes and PHP files in Laravel - Class not found
Laravel - Model Class not found
Laravel 6: Class 'Form' not found
To get the provided example codes to work you need to use require_once
<?php
require_once('models/myContext.php');
use app\config\models\myContext as context;
$test = new context();
A search on SO brought me to this source
You try to add this line of code in the composer.json file, then execute the composer dumpautoload command on the command line
In composer.json file,
"autoload": {
"psr-4": {
"App\\": "app/",
"config\\models\\": "config/models"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
After that composer dump-autoload.
have you add autoload for config/ folder in composer.json. Laravel only default autoload for app/

Reflection Exception : Class ClassName doesn't exist

I am using Laravel 5.5. I have added a custom directory inside App folder in my workspace. So, the folder structure is:
Inside App\Bishwa\Transformers there are two PHP files:
Transformer.php
LessonTransformer.php
Those files look like follows:
Transformer.php
<?php
namespace Bishwa;
abstract class Transformer {
public function transformCollection(array $items){
return array_map([$this, 'transform'], $items);
}
public abstract function transform($item);
}
LessonTransformer.php
<?php
namespace Bishwa;
class LessonTransformer extends Transformer {
public function transform($lesson){
return [
'title' => $lesson['title'],
'body' => $lesson['body'],
'active' => (boolean)$lesson['some_bool']
];
}
}
Then Inside LessonsController.php I have the following:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use App\Lesson;
use Bishwa\LessonTransformer;
class LessonsController extends Controller
{
protected $lessonTransformer;
function __construct(LessonTransformer $lessonTransformer){
dd('ok');
}
While running action of the controller, It gave me an error message saying:
Reflection Exception: Class Bishwa\LessonTransformer does not exist
I have tried composer dump-autoload, restarting the server again but none of them helped. Am I doing wrong while Namespacing or What?
Change your namespace of the files in your custom directory to App\Bishwa.
Well thanks to Jerodev and Jack. Since, both of them were write I decided to write myself a combined solution to this problem.
1st Solution:
In case of custom namespaces and custom classes I have to include the path to classname in Composer.json file in the following portion:
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Bishwa/Transformers"
],
"psr-4": {
"App\\": "app/"
}
2nd Solution:
Changing NameSpace of my files to custom directory App\Bishwa .
Namespace of transformer.php and LessonTransformer.php now becomes:
namespace App\Bishwa\Transformers;
While using in LessonsController:
use App\Bishwa\Transformers\LessonTransformer;
Once again, big thanks to Jerodev and Jack. Its my silly mistake, that I couldn't figure that out.

Laravel 5 make seed to database not working

I try to seed a user to the database but cannot. I could easely create the migration tables, so the problem is not in the connection to the database, but I cannot create a user, I get the "Class UsersTableSeeder does not exist" error. How can I solve it? (I use laravel 5.3)
Console Error
Here is my code:
DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(UsersTableSeeder::class);
}
}
UsersTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use App/User;
use Illuminate\Support\Facades\DB;
class UsersTableSeeder extends Seeder
{
public function run()
{
\DB::table('users')->delete();
User::create([
'name' => 'lamin',
'email' => 'lamin#laravel.com',
'password' => bcrypt('lamin')
]);
}
}
Try to run composer dumpauto command and then run seeder again.
Run this command.
composer dump-autoload
It just regenerates the list of all classes that need to be included in the project

Lumen (Laravel): Unable to run php artisan db:seed due to missing class

I'm using Lumen and working through a 12 month-old tutorial.
While attempting to run php artisan db:seed, I'm getting the following error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'App\Models\Quote' not found
However I created the Models sub-directory and Quote.php within that directory. Here is that code:
<?php
# app/Models/Quote.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
final class Quote extends Model
{
}
Within database/seeds I have two files: QuoteTableSeeder.php & the default DatabaseSeeder.php. I added $this->call('QuoteTableSeeder'); to the run() method of DatabaseSeeder.php.
Here are the contents of QuoteTableSeeder.php:
<?php
# database/seeds/QuoteTableSeeder.php
use App\Models\Quote;
use Illuminate\Database\Seeder;
class QuoteTableSeeder extends Seeder
{
public function run()
{
Quote::create([
'text' => 'Success is going from failure to failure without losing your enthusiasm',
'author' => 'Winston Churchill',
'background' => '1.jpg'
]);
}
}
I have already ran composer dump-autoload which fixed a previous error but now causes the above-mentioned issue.
What am I doing wrong?

Extend Laravel 4.1 validation class

I try extend Validator class. I need add a few methods, that I'd like extend all class not use Validator::extend();
I added in vendor direcotry structure:
-comjaroapp
-src
-Comjaroapp
-Validation
-Validator.php
-ValidatorServiceProvider.php
In my config/app.php in providers array, I added:
'Comjaroapp\Validation\ValidatorServiceProvider'
Code to test is simple:
Validator:
namespace Comjaroapp\Validation;
class CustomValidator extends \Illuminate\Validation\Validator{
public function validatePesel($attribute,$value,$options=null){
return true;
}
}
ValidatorServiceProvider:
namespace Comjarospp\Validation;
use Illuminate\Support\ServiceProvider;
class ValidatorServiceProvider extends ServiceProvider{
public function register(){}
public function boot(){
$this->app->validator->resolver(function($transator,$data,$rules,$messages){
return new CustomValidator($transator,$data,$rules,$messages);
});
}
}
After run composer update I see error:
> Error Output: PHP Fatal error: Class
> 'Comjaroapp\Validation\ValidatorServiceProvider' not found in
> /vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php
> on line 158
When I looked on other extension all work with same structure.
If someone have idea, what is wrong or when should I search, please help.
Thanks in advance.
You have different names for your namespaces:
Comjarospp\Validation
and
Comjaroapp\Validation
EDIT:
After fixing the namespace name, have you executed
composer dumpautoload
?

Categories