Can’t seed data from my Laravel plugin - laravel

I developed a Laravel Plugin, which contains seed data : https://github.com/xoco70/laravel-tournaments
When I try to seed it inside a fresh laravel 5.6 project, with:
php artisan db:seed --class=LaravelTournamentSeeder
I get:
ErrorException : include(/Users/julien/Documents/Proyectos/test/vendor/composer/../../database/seeds/LaravelTournamentSeeder.php): failed to open stream: No such file or directory
When I try to namespace all my seeds file with :
namespace Xoco70\LaravelTournaments\Database\Seeds;
and then ruin it with
php artisan db:seed --class=Xoco70\\LaravelTournaments\\Database\\Seeds\\LaravelTournamentSeeder
I also get an error:
ReflectionException : Class Xoco70\LaravelTournaments\Database\Seeds\LaravelTournamentSeeder does not exist
Inside my plugin, my composer.json is:
…
"autoload": {
"psr-4": {
"Xoco70\\LaravelTournaments\\": "src"
},
"classmap": [
"src/"
]
},
…
What am I doing wrong ?

I can see in the GitHub code that the seeder class is not namespaced neither the other seeding files. The missing bit is to move the database related code under "src" following the psr-4 entry in the composer:
"psr-4": {
"Xoco70\\LaravelTournaments\\": "src"
},
Another alternative is to add another entry in the psr-4 but it might be not clear.

Related

Laravel Autoclosing after clicking one request

I have a problem when declaring autoload in laravel,
i have been using laravel 8 and php 7.4.3 this is the problem
After i get another request, the laravel runner always closing and give me this error
To solve this error, i've always do the
php artisan key:generate
or do another thing like restarting the laravel after dump autoload
composer dump-autoload
php artisan serve
But the problem just solve for one time, and it will be error again until i dump the composer again
How can i solve this?
In your composer.json file you should have the following line
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
And in you artisan file you should have the following
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
The composer.json code should autoload any content coming from the App directory, while the require /vendor/autoloader will load vendor content into your project. Also you must have app.php inside of the bootstrap directory to allow the app to use the Kernal corrently. Hope this helps!

How to create custom helper file in laravel 5.6?

When I create an autoload helper file with composer following error ocures:
Fatal error: composerRequireebefe31fc60fbe2897fba8c156ec310c(): Failed
opening required
'D:\xampp\htdocs\common_module\vendor\composer/../../app/Http/helpers.php'
(include_path='D:\xampp\php\PEAR') in
D:\xampp\htdocs\common_module\vendor\composer\autoload_real.php on
line 66
Following are steps to create a Custom Helper In Laravel 5.5
Step : 1 Create app/helpers.php file
First, create one helper class file in app/helpers.php path and in this file we are write our any custom helper logic into the function.
Step : 2 Add app/helpers.php file in composer.json file
Now, we are add our app/helpers.php file in composer.json file for a autoload section.
"autoload": {
"classmap": [
...
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php" //Add This Line
]
},
After done this then once we are run following command.
composer dump-autoload
for more visit following link
Visit How To Create Custom Helper In Laravel 5.5
I hope this will help you...
In older version of Laravel I do the following:
app folder create a Helpers folder
Create a class YourHelper class file
in that class file set namespace to App\Helpers
Name the class file
in config/app.php in aliases create an alias (so you can call the helper in your view also) 'YourHelper'=>'App\Helpers\YourHelper::class'
If you are new to Laravel or PHP, let’s walk through how you might go about creating your own helper functions that automatically get loaded by Laravel.
Creating a Helpers file in a Laravel App
you can organize the location of your helper file(s) however you want, however, here are a few suggested locations:
app/helpers.php
app/Http/helpers.php
I prefer to keep mine in app/helpers.php in the root of the application namespace.
Autoloading
PHP functions cannot be autoloaded. However, we have a much better solution through Composer than using require or require_once.
So composer has a files key (which is an array of file paths) that you can define inside of autoload:
"autoload": {
"files": [
"app/helpers.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
Once you add a new path to the files array, you need to dump the autoloader:
composer dump-autoload
(Use this command after changing composer.json.)
Now on every request the helpers.php file will be loaded automatically because Laravel requires Composer’s autoloader in public/index.php:
require __DIR__.'/../vendor/autoload.php';
Defining Functions
Defining functions in your helpers class is the easy part, although, there are a few caveats. All of the Laravel helper files are wrapped in a check to avoid function definition collisions:
if (! function_exists('env')) {
function env($key, $default = null) {
// ...
}
}
I prefer to use function_exists checks in my application helpers, but if you are defining helpers within the context of your application, you could forgo the function_exists check.

Laravel5.5 after use php artisan app:name Class not found

I am new developer on Laravel, now I'm using Laravel version 5.5
I got the problem after used php artisan app:name on my project, I got the problem:
In ProviderRepository.php line 208: Class
'App\Providers\AppServiceProvider' not found
as the captured image below:
As this error, I can not use php artisan commands or composer commands anymore can you guys please help me to solve this problem I am really appreciated for time. Thanks you
Best Regards
Siripong Gaewmaneechot
I would suggest looking in your config files, and the main classes which were generated when you started your laravel project (User class, etc) because they are all set to App\User App..... etc.
So for example, in the image you have in the question, it says it can not find App\AppProviders... - This indicates that somewhere you still have a use statement pointed to App\AppProviders.. but you changed the app name, so it's no longer App. something I do if I made that mistake, is I do a global search in my project files for App\ (you may need to put App\\ in the search because \ is an escape character
So if you did not change the app name immediately after starting the project, some of the paths will not be pointing to the correct directories. Let me know if that makes sense.
The command changes the PSR-4 configuration in composer.json.
Assume it was App before, your composer.json looks like this:
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
After running the command with php artisan app:name Foo, it will look like:
"autoload": {
"psr-4": {
"Foo\\": "app/"
}
},
Therefore the whole namespace has changed and your classes can't be found by the Autoloader. To fix this, you have to either go back to the old name or do a global search and replace to change the namespace from App to Foo.

How to get access to directory in Laravel?

I have create a new directory Library in path App\Http
When I try to use this path to load class pagination:
use App\Http\Library\Pagination;
Then IDE says that does not see path Library
You need to autoload this into class map. In composer.json put the class into classmap.
"autoload": {
"classmap": [
"app/Http/Library/Pagination"
]
},
Then you update your autoload file composer-dumpautoload or php artisan optimize

Location of resources and namespacing in packages

My package needs controllers, models and views.
I'm confused as to where these should be placed and how they should be namespaced.
For example, controllers. Do they go in:
/workbench/my-corp/my-app/src/controllers
Or should they go in:
/workbench/my-corp/my-app/src/my-corp/my-app/Controllers
And should they be namespaced? If so, to what?
Laravels workbench works in the same way as the vendor directory, with PSR-0 autoloading you would place your controllers within:
/workbench/Company/Package/src/Company/Package/Controllers
And they should be namespaced as namespace Company\Package\Controllers.
Your composer.json within the /workbench/Company/Package/ directory would then have a psr-o section added to the autoload section like so:
...
"autoload": {
"psr-0": {
"Company\\Package": "src/"
}
},
...
Then running php artisan dump-autoload from the command line will get artisan to run a composer dump autoload for the workbench packages.

Resources