Why php artisan run all commands in the folder Commands when I create a model for example ?
Did I made something wrong ?
Laravel will initialize all the commands (User defined and Default commands) whenever you run any command in laravel application.
Which means don't perform any operation in __construct method of the commands.
For Example:
If you are performing any Databse operation in __construct method of custom command it will perform Databse operation no matter what command to ran.
So Move all the logics and operations to handle method of command.
I have faced the same issue back in the day You can see the github issue to clarify.
Artisan is the command line interface included with Laravel. Artisan exists at the root of your application as the artisan script and provides a number of helpful commands that can assist you while you build your application.
See more at : https://laravel.com/docs/8.x/artisan
Related
I have this process setting.
php artisan queue:work beanstalkd --sleep=3 --tries=1 --timeout=0 --queue=medium,messages
I also have a job with the setting
public $timeout = 100000000;
But the job is stopped much before that with message has been attempted too many times or run too long. The job may have previously timed out Which was run for the first time.
I also have this in php.ini
max_execution_time = 0
What am I missing here?
Console
Things I want to execute from the command line (As you mentioned with your job creates more jobs). But the thing is, you could extract the business logic of it to a command.
Example: A console command with fires a command to fetch images from Imgur. The Class FetchImages contains the actual business logic of fetching images.
Command
Class which contains the actual logic. You should also be able to call this command from your application with app()->make(Command::class)->handle().
Example: Command mentioned in Example 1. Contains logic which does the actual API calls to Imgur and process returned data.
Jobs
Laravel 5.0 so jobs weren't a thing back then. But as I see it, Jobs are like commands but they are queued and can be dispatched. (As you may have seen in those examples, those commands implement your mentioned Interfaces SelfHandling and ShouldBeQueued).
I see changes in Commands and Jobs are quite difficult to understand.
EDIT:
From the Laravel Docs:
The app/Commands directory has been renamed to app/Jobs. However, you are not required to move all of your commands to the new location, and you may continue using the make:command and handler:command Artisan commands to generate your classes.
Likewise, the app/Handlers directory has been renamed to app/Listeners and now only contains event listeners. However, you are not required to move or rename your existing command and event handlers, and you may continue to use the handler:event command to generate event handlers.
By providing backwards compatibility for the Laravel 5.0 folder structure, you may upgrade your applications to Laravel 5.1 and slowly upgrade your events and commands to their new locations when it is convenient for you or your team.
In my laravel project I am using multi tenancy. For project related work I am creating a laravel custom artisan command. I am trying to connect with repository model in that artisan command that I am creating. But I could not connect with the database. It says
Database [tenant] not configured.
How to use multi tenancy in my custom artisan command. So that I can use all the needed repository models in custom artisan command. Please suggest.
I assume you are using: hyn/multi-tenant
To run specific commands you could use the tenancy:run command. Lets say you have a command sync:donuts, which needs to be run on tenant id=1.
Then you should use:
php artisan tenancy:run sync:donuts --tenant=1
Read more in the documentation
I want to test some Laravel applications on my basic shared host.
Currently I just upload my complete application including the vendor files, however this takes quite long.
Since I do not have ssh access to my host I'd like to know whether there's an option to run composer / artisan commands without this.
I found this link: Use Composer without ssh access to server (Second Answer) which describes how to run composer using http://phpshell.sourceforge.net/
However, I can change folders in the console etc. But I cannot run php commands - I always get internal server error.
Check if your shared hosting provider has console feature in their CP which allows to run shell commands. Maybe you'll be able to run commands from there.
As alternative, you could right your own artisan runner and call artisan commands from the code:
Artisan::call('migrate');
To run composer command from PHP code, use shell_exec:
shell_exec('composer update');
I didn't know composer before laravel and i encountered them at the same time. So, I can't distinguish the commands of them, but memorize them.
What is the difference between php artisan and composer.phar and also the commands they run?
Can anyone help me on this issue? Thanks..
Composer is a package manager for php and artisan is a command line tool using php
Composer
- download libraries
Artisan
- run scripted commands like you would in bash
The reason to use artisan instead of direct php commands is that you have to implement certain functions so you get an universal feel to the commands. It will help with a lot of things like handling arguments. You could see it as a nice helper for writing great php command line tools.
Once your command is generated, you should fill out the name and
description properties of the class, which will be used when
displaying your command on the list screen.
The fire method will be called when your command is executed. You may
place any command logic in this method.
https://laravel.com/docs/5.0/commands
There is a basic difference in these two commands that they belongs to two different packages.
Composer: composer is a dependency management tool for php. It auto manages dependencies of your project according to your requirements and thus makes your project more distributable.
You don't have to package all the dependencies inside your application. And, if your dependencies are variable it is hard to manage them all within your application. So, composer does it for you.
So, your composer command operates this package. And, it can be used for any project whether it is laravel or other php application.
Artisan: This is the command line utility of Laravel. It comes integrated with your laravel installation.
You can perform operations on your laravel project using its commands. As it is a tool built over php. So, you need to execute its command over php-CLI. That is why you use the php before it.
And, you need to use artisan before it because this is the script name which operates the commands of this tool. If you see your laravel project directory it has a file named artisan inside the root folder. It is the file which bootstraps the laravel's command line code. Or, you can say it is like index.php of directory.
Hope I was helpful.
There are a few Artisan commands that I do not want to be run on production. For example, I would like to block the use of php artisan migrate:reset on production.
I would like to completely block this command
OR
Update the template artisan uses when running php artisan make:migration. The reason for updating the template is so I can make the template extend a different Class instead of the Migration class. In this new class I can inject my custom protection logic. I just don't want other developers using the artisan command and extending the wrong class.
Well,
Laravel will always ask for confirmation when you run migration commands in production, for safety purposes.
Other developers shouldn't have access to your production. Normally, almost none of the developers have access to production. And even if they did, why would they run migration commands there?
If you are still sure you want to do this, you could just add a very simple condition (if app()->environment('production')) when you load the command in the bootstrap process or inside the command class itself.