PHP Script - How to use php artisan command in my script? - php

I'd like to create a php script which connects to a mysql server, makes changes on a database and runs a php artisan command.
The first part I have figured out (mysql connection) but is it possible to just put (for example):
php artisan snipeit:ldap-sync --location_id=1
into my script and it will run the command, or am I missing something here?
I'd appreciate it if you could send me into the right direction wtih this. Thank you.

You can use Artisan::call().
Artisan:call('snipeit:ldap-sync', [
'--location_id' => 1
]);
It can also take a second parameter to specify an array of command parameters.
For more information, see Programmatically Executing Commands.

You can call artisan command from code like this:
Artisan::call('cache:clear');

Related

Laravel - Artisan::call() doesn't take argument

I'm using Laravel 5.4 and PHP 7.0.
I have a lot of failed jobs in the table that I want to re-queue. I have written a script to go through a list of IDs that I pulled from the database and I want a foreach to re-queue each one. Pretty simple stuff.
My issue is that when I run
foreach($jobsToRetry as $failedJob) {
Artisan::call('queue:retry '.$failedJob);
}
I receive the following error:
Command "queue:retry 1" is not defined.
Did you mean one of these?
queue:failed
queue:failed-table
queue:flush
queue:forget
queue:listen
queue:restart
queue:retry
queue:table
queue:work
It needs to be using the command "queue:retry" and have the parameter separate but I just can't figure out how to get that to work.
Give the parameter in the arguments
Artisan::call('queue:retry', ['id' => $failedJob]);
You should try this:
Artisan::call('queue:retry', ['--yourparameter' => $failedJob]);

Laravel Artisan - reload .env variables or restart framework

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

How to use composer, artisan ...(Laravel) php commands manually

I have one problem I have no ssh access to the server, so I cannot use php artisan, composer and other commands.
As I can quess they do nothing other than modifying files or just copying php src files to specific directories.
In order to understand that process better and because of no access via ssh to the server I am looking for tutroial, manual or an article how can I perform this commands manually.
For example I need to execute
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
What should I do in this case, it would be grate to find some document describes what should I do manually to get the same result.
Laravel provides a handy Facade for artisan commands.
Just use Artisan::call('your-command') from where you need.
Example:
Route::get('artisan-command/{command}', function($command) {
Artisan::call($command);
});
Your URL looks like this: http://yourhost.com/artisan-command/db:seed
More specific for your use-case:
Route::get('vendor-publish/{provider}', function($provider) {
Artisan::call('vendor:publish', ['provider' => $provider]);
});
And the URL: http://yourhost.com/vendor-publish/Tymon\JWTAuth\Providers\JWTAuthServiceProvider
Reference: Artisan Console in the Laravel Docs
You can execute command lines from your app. You can do something like this:
Route::get('execute/my/command', function(){
exec("php path/to/your/project/artisan your-command",$resultLines);
Foreach($resultLines as $resultLine){
echo $resultLine;
}
});
First exec property is your command and second is variable to save the result.
I hope that help you

Select from table using command prompt (not browser) in Laravel framewok

I want to select all data from 1 table in command prompt (not from browser) using Laravel framework. Is there anyone know what the good references is? please give me link, thankyou
You can create an Artisan Command.
Check the Laravel 5.0 docs:
http://laravel.com/docs/5.0/commands
Then, you can run you command. Example:
php artisan mycommand:list
Laravel 4.2:
http://laravel.com/docs/4.2/commands
You can run artisan commands from "cmd" (Windows). (Edit your environment variables first! Add you PHP path).
Try:
php artisan tinker
Then:
DB::table('users')->get()

How to run a php file from bash script?

I am working in a Windows machine. I need to run a script that will execute a php file. When I run where php to find my php executable location I get the following:
c:\xampp\php\phpe.exe
After that I tried something like this:
#!/bin/bash
#!/c:/xampp/php c:/xampp/htdocs/Bash/testingphp.php
I don't get any error, but I don't know if the script is doing something. Just for reference I am using cygwing to run the scripts. How do I know if my script is really working??
Thank you in advanced
As stated by #Etan, your second line is a comment, but your paths are also incorrect. You need to use /cygdrive/c/, not /c:/. Here is the corrected script:
#!/bin/bash
/cygdrive/c/xampp/php /cygdrive/c/xampp/htdocs/Bash/testingphp.php

Categories