I'm trying to generate models from an existing database at once without having to do it separately for all tables. I have tried to do this with reliese/laravel. I have executed:
php artisan -v code:models
However, I'm getting the following error.
ErrorException : mkdir(): Invalid path
at C:\xampp\htdocs\schaden\vendor\laravel\framework\src\Illuminate\Filesystem\Filesystem.php:466
462| if ($force) {
463| return #mkdir($path, $mode, $recursive);
464| }
465|
466| return mkdir($path, $mode, $recursive);
467| }
468|
469| /**
470| * Move a directory.
Exception trace:
1 mkdir("")
C:\xampp\htdocs\schaden\vendor\laravel\framework\src\Illuminate\Filesystem\Filesystem.php:466
I'm not posting the full error stack here. Can anyone help?
Probably the best solution is using the package Eloquent Model Generator that you can find on github at https://github.com/krlove/eloquent-model-generator.
Then you can easily use, for example, php artisan krlove:generate:model User --table-name=users or php artisan krlove:generate:model MyModel --table-name=my_models and use some of the package options.
Related
I write php artisan migrate:fresh --seed in the console of the root folder of a project, when I run this command, it takes near to 1 minute then it returns \
In PackageServiceProvider.php line 14:
syntax error, unexpected 'Package' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
PackageServiceProvider.php:
namespace Spatie\LaravelPackageTools;
use Carbon\Carbon;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use ReflectionClass;
use Spatie\LaravelPackageTools\Exceptions\InvalidPackage;
abstract class PackageServiceProvider extends ServiceProvider
{
protected Package $package; /* line 14 */
abstract public function configurePackage(Package $package): void;
public function register()
{
$this->registeringPackage();
$this->package = new Package();
$this->package->setBasePath($this->getPackageBaseDir());
$this->configurePackage($this->package);
if (empty($this->package->name)) {
throw InvalidPackage::nameIsRequired();
}
foreach($this->package->configFileNames as $configFileName) {
$this->mergeConfigFrom($this->package->basePath("/../config/{$configFileName}.php"), $configFileName);
}
$this->packageRegistered();
return $this;
}
.
.
.
.
}
the project's author's PHP version: 7.4.19
my PHP version: 7.3.27
I'm noob in laravel, so if I have to show up with more info about the issue tell me. \
edit
after updating the PHP version to 7.4.21
I wrote the command and it returned
C:\xampp\htdocs\Business-Manager>php artisan migrate:fresh --seed
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command? (yes/no) [no]:
> y
Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user 'forge'#'localhost' (using password: NO) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
at C:\xampp\htdocs\Business-Manager\vendor\laravel\framework\src\Illuminate\Database\Connection.php:692
688▕ // If an exception occurs when attempting to run a query, we'll format the error
689▕ // message to include the bindings with SQL, which will make this exception a
690▕ // lot more helpful to the developer instead of just the database's errors.
691▕ catch (Exception $e) {
➜ 692▕ throw new QueryException(
693▕ $query, $this->prepareBindings($bindings), $e
694▕ );
695▕ }
696▕
1 C:\xampp\htdocs\Business-Manager\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDO\Exception.php:18
Doctrine\DBAL\Driver\PDO\Exception::("SQLSTATE[HY000] [1045] Access denied for user 'forge'#'localhost' (using password: NO)")
2 C:\xampp\htdocs\Business-Manager\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:43
Doctrine\DBAL\Driver\PDO\Exception::new(Object(PDOException))
my MySQL accounts :
enter image description here
Your problem is that protected Package $package; is PHP 7.4, it should be like protected $package;.
As you can see in the source code, it required php ^7.4 or ^8.0, so you have to change your PHP to either of those.
This is another place to see the composer package you are downloading, to see more info about it...
import suitable class in the top.
use Facade\Ignition\Support\Packagist\Package;
Scenario is the following:
I'm building a software where the frontend and the programm that does the work are separated from each other. To have the frontend be able to use the models of the worker-programm, I've build a package that holds them. The package has the following code to it's provider:
public function boot()
{
if($this->app->runningInConsole()){
$this->loadMigrationsFrom(__DIR__ . '/MyPackageMigrations');
}
}
Now my problem is that I want those migrations to be run when the artisan migrate command is run in the worker-programm, but not in the UI-programm, since both databases are separated from each other aswell. Is there a way to supress those migrations in the UI-programm?
Greetings
Rather than using loadMigrationsFrom() in the boot() method of your ServiceProvider, you can use $this->puiblishes() which gives you control over publishing migrations rather than having them load automatically.
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../database/migrations/create_models_table.php.stub'
=> database_path('migrations/' . date('Y_m_d_His', time()) . 'create_models_table.php'),
// you can add any number of migrations here
], 'migrations');
}
}
Then all you need to do is publish the migrations in the projects you want to use the migrations in:
php artisan vendor:publish --provider="Husky110\Husky110Package\Husky110PackageServiceProvider" --tag="migrations"
Update
You can check for the existence of migration by doing something like:
if (!class_exists('CreateModelsTable')) {
// perform migration
}
If you have migrations which alter the structure of a table, you might do:
if (!class_exists('AlterModelsTableAddField')) {
// perform migration
}
Installing Swagger for the First Time in laravel 6. https://github.com/DarkaOnLine/L5-Swagger.
composer require "darkaonline/l5-swagger"
php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider"
On generating command
php artisan l5-swagger:generate
Getting Error like:
php artisan l5-swagger:generate
Regenerating docs
ErrorException : Required #OA\Info() not found
at D:\XMAPP\htdocs\minidmsapi\vendor\zircote\swagger-php\src\Logger.php:39
35| $this->log = function ($entry, $type) {
36| if ($entry instanceof Exception) {
37| $entry = $entry->getMessage();
38| }
> 39| trigger_error($entry, $type);
40| };
41| }
42|
43| /**
Exception trace:
1 trigger_error("Required #OA\Info() not found")
D:\XMAPP\htdocs\minidmsapi\vendor\zircote\swagger-php\src\Logger.php:39
2 OpenApi\Logger::OpenApi\{closure}("Required #OA\Info() not found")
D:\XMAPP\htdocs\minidmsapi\vendor\zircote\swagger-php\src\Logger.php:71
Please use the argument -v to see more details.
please Help me for generating swagger documentation
Required #OA\Info() is required to initialize your swagger documentation and then use proper annotation to parse. Annotations are only parsed inside /** DocBlocks. Here you go for initial annotations swagger-php#usage
I'm in the process of upgrading a Laravel 4.2 app I inherited to Laravel 5.2. The app has multiple roles for logged in users that were handled with a before filter. Each controller has an array of functions and roles allowed for those functions:
public $actionFilter = [
'directories-create'=>['super','tsr'],
'directories-destroy'=>['super','tsr'],
'directories-edit'=>['super','tsr'],
'directories-directoryinfo'=>['super','tsr','admin'],
'directories-index'=>['super','tsr'],
'directories-store'=>['super','tsr'],
'directories-update'=>['super','tsr'],
];
then in the construct function it calls two beforeFilters that were in Controller.php
public function __construct()
{
$this->beforeFilter('#filterAuthorization');
$this->beforeFilter('#rerouteSite');
}
Controller.php had a public function filterAuthorization that checked if user's role had access to the route, and a public function rerouteSite that allowed user to stay on the same page but switch between accounts (for example, for a support rep).
I've spent a fair amount of time reading the manual, Googling and reading various tutorials, but I'm still unclear how to get my route-role array connected to the auth middleware. The Laravel docs provide syntax but not the context and the examples I've read either take a different approach or have a different usecase from mine.
I tried leaving the filter functions in Controller.php and calling them like this in the construct:
public function __construct()
{
$this->middleware('#filterAuthorization');
$this->middleware('#rerouteSite');
}
I get an error message: "Class #filterAuthorization does not exist"
I tried putting those functions in app\Http\Middleware\Authenticate, but I get the same error message: "Class #filterAuthorization does not exist"
I followed the steps on Matt Stauffer's blog here (https://mattstauffer.com/blog/laravel-5.0-middleware-filter-style/) and here (https://mattstauffer.com/blog/passing-parameters-to-middleware-in-laravel-5.1/) and on Nwanze Franklin's post here (https://dev.to/franko4don/deep-dive-into-middlewares-in-laravel-doo) as follows.
Create two new middleware files with Artisan
php artisan make:middleware FilterAuthorization
php artisan make:middleware RerouteSite
Edit the new middleware files with the functions from the old Controller.php
Register the new middleware in App\Http\Kernel
protected $routeMiddleware = [
'filterauth' => \Illuminate\Routing\Middleware\FilterAuthorization::class,
'reroutesite' => \Illuminate\Routing\Middleware\RerouteSite::class,
];
Edit the public function __contstruct() in the Controllers than need filtering
public function __construct()
{
$this->middleware('FilterAuthorization');
$this->middleware('RerouteSite');
}
Run
composer dump-autoload
php artisan clear-compiled
php artisan optimize
and I still get the same error:
Class FilterAuthorization does not exist
I'm sure there's a simple way to put this together without rewriting the whole role authorization system. Can someone point me in the right direction?
The kernel registration needs to reference the correct file locations as follows:
'filterauth' => \App\Http\Middleware\FilterAuthorization::class,
'reroutesite' => \App\Http\Middleware\RerouteSite::class,
And the controller boot should use the aliases rather than the class names:
public function __construct()
{
$this->middleware('filterauth');
$this->middleware('reroutesite');
}
Then Laravel can find the custom middleware.
I am using Laravel 5.4 and JWT Auth Library for user authentication in API development. After installation while i am running php artisan jwt:generate then it throws me error of
Method Tymon\JWTAuth\Commands\JWTGenerateCommand::handle() does not exist
Any idea what i am missing ?
This error generally display when you install jwt package in laravel 5.5 version. then after you set service providers and run following command.
php artisan jwt:generate
then you seen this error message in terminal.
how to resolve it? simple follow this step
Step - 1 Re-install package
composer require tymon/jwt-auth:dev-develop --prefer-source
or the following is a new release package use laravel 6.X
composer require tymon/jwt-auth:1.0.*
in this developement version this errors fixed.
Step - 2 Set Service Provider
'providers' => [
....
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class to
Tymon\JWTAuth\Providers\LaravelServiceProvider::class
],
Step - 3 Generate key
php artisan jwt:secret
i found this solution from here https://laravelcode.com/post/method-tymonjwtauthcommandsjwtgeneratecommandhandle-does-not-exist
Go to JWTGenerateCommand.php file located in vendor/tymon/src/Commands and paste this method
public function handle() { $this->fire(); }
It's never a great idea to change anything in the vendor folder but the there's two ways to deal with this ...
Generate a random string yourself and just change the value in the JWT config file.
Go to Tymon\JWTAuth\Commands\JWTGenerateCommand and change the fire method to handle.
go to given file path
vendor/tymon/jwt-auth/src/Commands/JWTGenerateCommand.php
change function name
public function fire() to public function handle()
run command:
php artisan jwt:generate
I'm publishing this answer because I have crash in this error more than one time.
The only solution I found that it works with Laravel 5.6 is the following:
Add "tymon/jwt-auth": "1.0.0-rc.1" to composer.json and run composer update
Open config/app.php and add the following:
config/app.php:
'providers' => [
/*
* JWT Service Provider...
*/
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
],
'aliases' => [
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
],
Execute:
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
Finally, execute: php artisan jwt:secret
After all that, when I hit my endpoint for login I got the following exception:
Class Tymon\JWTAuth\Providers\JWT\NamshiAdapter does not exist
This was fixed by:
Open config/jwt.php and change the following:
config/jwt.php:
'jwt' => Tymon\JWTAuth\Providers\JWT\Namshi::class,
'auth' => Tymon\JWTAuth\Providers\Auth\Illuminate::class,
'storage' => Tymon\JWTAuth\Providers\Storage\Illuminate::class,
Finally, note that in order to work your User model should be defined as follows:
class User extends Authenticatable implements JWTSubject
{
...
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
...
}
I can advise one solution. Go to JWTGenerateCommand.php file located in vendor/tymon/src/Commands and paste this part of code public function handle() { $this->fire(); }
I know this is not an elegant solution, but it works. I hope this might help until official fix arrive.
see here for more info
Change fire() function to handle() in this path
vendor/tymon/jwt-auth/src/commands/JWTGenerateCommand.php
In the file path: /vendor/tymon/jwt-auth/src/Commands/JWTGenerateCommand.php
Add public function
public function handle()
{
$this->fire();
}