I've did some changes in my config/app to use multiple databases selected by front-end,
now I have to tell in \Request()->header('database') which database I want access.
It's work perfectly, the problem is: when I try to do any artisan commands my logic dies, because isn't informed the database.
So I need to inform the database in artisan commands, like that:
php artisan migrate --database=sandiego_school
php artisan migrate:rollback --database=newyork_school
How can I observer all commands to get the argument?
In this case I guess you should create your own commands that overrides the commands you want to call, then in the handle method of the command you could specify the connexion you want to work on:
\DB::setDefaultConnection($connexion);
or also you can simply add header to request:
request()->headers->set('database', $dbname)
Related
I need to run this command when user change something in translation file
php artisan export:messages-flat
I need to add it in may controller
so I'm using this code
\Artisan::call('export:messages-flat');
but it return error saying that
The command "export:messages-flat" does not exist.
but when I
php artisan list
it's in the list
I also try to run other command
\Artisan::call('cache:clear');
and it works
this is the package I'm using link
kindly help me, sorry for may poor english
The package only allows you to run the commands from the CLI. You can see from the source code, they only register the Artisan commands if the application is running from the console.
As an alternative, you can call ExportLocalization::export()->toFlat() according to their documentation.
in my Symfony 5.1 project I am trying to declare my own task which I would like to use when deploying. I created it like this:
task('update_database', function () {
if($_ENV['DATABASE_URL'])
{
cd('{{release_path}}');
writeln('<info>Updating database</info>');
run('{{bin/php}} {{bin/console}} d:d:c --if-not-exists');
run('{{bin/php}} {{bin/console}} doctrine:schema:update --force');
}
})->desc('Make database updates');
The "cd" and the "writeln" work. But every time I try to run a command it always fails
I tried with just the command php bin/console instead of using {{bin/php}} {{bin/console}}.
I even tried a silly php bin/console cache:clear command to see, but same error.
Yet I am in the right directory.. I followed this tutorial https://www.codepicky.com/php-automatic-deploy/
And I do the exact same thing though
UPDATE :
I connected to my server, I went to the last release of my project, and when I tried commands, absolutely nothing was happening. No mistake, but they didn't do anything. Even if I try the command to create an entity, it is "ignored".
The -vvv flag didn't give anything either
Here is what I have in the console:
I'm trying to write a command as a quickstart to build a project. The command asks for input for database details and then changes the .env file. The problem is that the command needs to do some Database queries afterwards, but the .env variables are not reloaded.
My question would be, is there a way to reload or override .env variables runtime. And if not, is there a way to call another Artisan command freshly, so framework bootstraps again?
In my command I tried doing $this->call('build:project') in my actual command, but even in the second call the variables are not reloaded.
Is there a way to achieve this without making the user manually call multiple commands?
Thanks!
i had the same problem with reloaded .env variables, and i fixed it with this command line which allow you to clear the configuration :
php artisan config:clear
Hope that helped. Regards.
Laravel uses config files which take data from .env file. So, what you can do is to override configuration values at runtime:
config(['database.default' => 'mysql']);
Try to clear cache it helped me (couldn't ssh into the server)
Is there a {app route}/bootstrap/cache/config.php file on the production server? Delete it.
This helped me
As OP I'm trying to bootstrap a Laravel project build by running console command and asking for the database credentials in the middle of the process.
This is a tricky problem and nothing I read was able to fix it : reset config, cache, Dotenv reload, etc... It seems that once the console command / operation is initialized the initial database connection is kept all over until the end.
The working solution I found is to bypass, after database modification are done, this cached state by using native shell exec command and passing the php artisan command as parameter :
passthru('php artisan migrate');
So, the order will be :
php artisan project:build (or whatever is your console command)
prompt the user for database credentials
replace values in .env file (your search & replace algorythm)
run php artisan config:cache
passthru('php artisan migrate'); ro run your migrations
shell_exec would do the same but in silent mode while passthru will return the output generated by console.
https://www.php.net/manual/en/function.passthru.php
Done successfully on Laravel 8.52
I am new to Laravel.
I am trying to create a command and a command handler in a project of mine. What is the correct way to do it from the command line.
I tried to use the following
php artisan make:command WhenFormSubmited
But that seems to create a self handling command.
I want to put the handler some where else so the handlers are in a separate folder.
Can I just manually create a handler? Is there a naming convention that I have to stick with for it to work? How would the command know the location of the corresponding controller?
Is there a way to specify an non-selfhandling command from the command line?
Use this command :
php artisan make:command MyCommand --handler
--handler Indicates that handler class should be generated.
Original Post
Good evening folks. I have a laravel setup and I'm trying to have a cronjob execute a php function to a file within the laravel project directory.
I am getting class and name space errors when I try to do something like this:
<?php
require_once('../laravel/app/Http/Controllers/NotificationsController.php');
and then calling the processQueuedNotifications() function.
This of course gives me errors, what is the correct way to call my function within the laravel directory? I need to call this function as this function has all the correct namespaces and extended controllers necessary to execute the function properly.
Update 1:
Thanks to #michael, I've been made aware of a component in Laravel called commands.
So I ran this code:
php artisan make:console processQueuedNotifications
and it created some files in the console directory.
Currently exploring on what to do next.
After checking out the Events class which the kernel.php file makes use of, I noticed that this class provides an easy to use interface for me to create cron jobs on the fly. Am I correct in think so?
I notice there is not function to run a cron job every minute, is it safe to edit the Events class file without it being overwritten by future make:console commands, or laravel updates?
I saw this code in the kernel.php file:
$schedule->command('inspire')
->hourly();
So is this the place you wanted me to add my function? as I notice that the inspire function is something automatically created for me to understand what's going on?
So I would write,
$schedule->command('processQueuedNotifications')
->everyMinute();
//Providing it's safe to edit the Event's class or figure out a clean way of doing so without my code being deleted in the future on Laravel updates.
A very convenient way is to use laravels console component. You can create a new command by issuing
php artisan make:console
And find it thereafter in your app/console directory. Make sure to enable the command in the Kernel.php file once created.
Simply call your class or whatever you want to run via cron from inside the command. The console command itself is callable via cli just as you would run one of laravels php artisan ... commands. You can set this in the file created for you. For example, you can then call the file from everywhere you want with
/usr/bin/php /path/to/file/artisan my:command
You can set options and arguments if you need to.
Here's the documentation: http://laravel.com/docs/5.0/commands / http://symfony.com/doc/current/components/console/introduction.html
There's an array in kernel.php you need to register your class (include the namespace) in. After that it is callable via cli. For a start, have a look on arguments and options you can initialize in case you need to make different requests on your controller class. (The filename you have chosen for your console command, is an argument. You can make them required or optional for your own commands. )
Within your file, you can create them by simply creating an array in the appropriate method with these values:
[$name, $mode, $description, $defaultValue]
have a look at the docs or Jeffrey's laracasts, they are very good.
To only call your class from the console command, it's enough to name your command in the above section of the file and call you controller like
(new namespace\controller)->method();
What you can do in your code, after your update, 2 choices :
Dispatching directly the command from your code using the Bus facade
first import it using the
use Illuminate\Support\Facades\Bus;
then in your code
Bus::dispatchNow(new YourCommandClass);
(don't forget to import your command class)
Dispatch it for queue process using the same bus facade:
(still importing the same way)
Bus::dispatch(new YourCommandClass);
(Note that in that case, you'll need to have the following command run by your cron job :
php artisan queue:listen
it can handle several options such as the --tries=X where is is the number of tries etc
Generally speaking, you can get more info from commands typing php artisan my:command -h