3

I'm new in using Laravel 4, running it in my localhost. I would like to know how can I make artisan skip the question asking Do you really wish to run this command? every time I execute migration command? I would just like to execute command immediately without typing y. And also without adding --force in the end of the migration command.

I know the prompt is really important on production but I find it a bit annoying especially when you're in tutorial. I just want to turn it off to make life easier.

Any help would be greatly appreciated.

Jed
  • 1,664
  • 8
  • 33
  • 65

3 Answers3

4

If you set your environment to local you won't get the warning question, as that is only shown when Laravel detects you're in production, where running migrations by mistake might be dangerous. You can simply add the following in your bootstrap/start.php file:

$env = $app->detectEnvironment(array(
    // The array value should be your hostname
    'local' => array('your-machine-name'),
));

The above will allow Laravel to set the environment to local when it detects that your computer hostname maches the one you specified, thus avoiding displaying the message.


For more information on configuring environments and the advantages you get from that, read the Laravel Environment Configuration Docs.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • It worked. Thanks. I should mention though that I used `'local' => array(gethostname()),` because assigning the parameter to `local` or `localhost` still says that the app is on production. – Jed Oct 16 '15 at 02:04
  • 3
    @Jed but be aware that by doing that, even when the application goes into production, it will still be in the local enviroment. A better option would be `$env = $app->detectEnvironment(function() { return getenv('APP_ENV') ? : 'production'; });` and set `APP_ENV` in your VHOST file. – Malitta N Oct 16 '15 at 02:17
  • 1
    @blackpla9ue is right, by doing that the environment will always be **local** regardless of the machine you run the app on. Run this in your terminal `php -r "echo gethostname();"` to get the hostname string and use that to set the environment instead. – Bogdan Oct 16 '15 at 02:25
  • Also if you say you're new to Laravel, you should really be pick up and learn Laravel 5.1 instead, as that's the latest version. – Bogdan Oct 16 '15 at 03:20
  • @blackpla9ue, I appreciate your suggestion! Thanks. Just doesn't make sense for me how the conditional works. And also I'm using WAMP I'm not sure how can I set APP_ENV in my vhost file. Would you able to give more a little details about it or any helpful links would do. – Jed Oct 16 '15 at 03:30
  • @Bogdan noted! Thanks again. – Jed Oct 16 '15 at 03:30
  • @Jed I added a detailed answer. – Malitta N Oct 16 '15 at 05:51
1

Adding more to Bogdan's answer and comments.

In case you are not familiar with the concept of environments in Laravel, it's there to specify different configurations specific to the environment your application runs in. For example you will have different database credentials in your local env than the live server (production env).

So one reason why you shouldn't use 'local' => array(gethostname()) is that this would make your environment local regardless of where you run this (your local machine, testing env or production).

This is the preferred method of determining the env

$env = $app->detectEnvironment(function() { 
    return getenv('APP_ENV') ? : 'production'; 
});

So unless you have an environment variable set in your VHOST file, it will default to production.

But since you don't have anyway to specify your env variables, I suggest you stick to Bogdan's solution.

Malitta N
  • 3,384
  • 22
  • 30
0

For a docker prod. deployment, this was added to run artisan migrate the first time.

BASEDIR="/foo/bar/storage"
if [ ! -f "$BASEDIR/.artisan_init" ]; then
    # ...

    { printf "yes\n"; } | ./artisan migrate

    date -I > "$BASEDIR/.artisan_init"
        
    # ...
fi

In case there are more than one question to answer, you can pile up answers:

{ printf "yes\n"; printf "foo\n"; printf "bar\n"; } | ./artisan migrate
Michael D.
  • 1,795
  • 2
  • 18
  • 25