Laravel Serve command does not respect --env parameter - laravel

To be able to run Browser tests directly in my IDE (without using the artisan dusk command), I want to run php artisan serve --env=dusk.local. While it indeed starts the local PHP server, it uses the wrong database. It uses the database specified in .env not the one in .env.dusk.local.
I ran php artisan cache:clear thousands of times, but it doesn't change anything.
Running things like php artisan migrate --env=... works.
Is there a way to achieve my goal without needing to rename my .env.dusk.local file to .env before each test?

This is a bug in Laravel 5.8: https://github.com/laravel/framework/issues/27828
There is currently no solution (other than downgrading to Laravel 5.7).
It has been fixed in the latest release 5.8.7.

Related

From Local to Live, which files to edit - Laravel

strangely I have never programmed locally, I am a beginner but unfortunately I have always created my little scripts directly live. I am using Laravel with Xampp, but now I would like to put my script online.
My question is, what files do I need to edit to make it effective online? At the moment I edit the .env file locally to connect to the database, but is it the same even if I put it online?
Thanks
Here you can find some basic information: https://laravel.com/docs/7.x/installation and https://laravel.com/docs/7.x/deployment
In general, to have a site in production (Laravel or not) it is better to manage your code with git (through Github, Gilab, etc.)
to have a clean version management (the alternative is to use Ftp trought Cpanel, ...).
If you are a beginner, you can go to https://forge.laravel.com/ which is a portal that greatly facilitates the production of a Laravel project.
Otherwise, a generic hosting is better that has the possibility to connect via ssh, in order to easily execute the commands
(composer install, php artisan, ...).
You can do many optimizations in production (I leave out the ones on the .env file you know):
When you do maintenance on the site it is always best to disable it first(status 503):
php artisan down
Upload the latest version of the code from the git repository:
git pull
Clean the item cache on the server:
php artisan cache:clear
Clean the route cache and recreate it (to do if there is no static code in web.php, but only references to code in the controller):
php artisan route:clear
php artisan route:cache
Clean and optimize the config file (reduces the number of files to be read from ten to one):
php artisan config:clear
php artisan config:cache
Clean up expired passwords and reset tokens (clean up tokens when a user requests a password reset):
php artisan auth:clear-resets
Recreate the framework classes or update the application:
composer dump-autoload or composer install
Activate the site:
php artisan up

Creating Schema automatically using migration in Laravel

Is it possible to create schemas automatically in the database when xampp is turning on? There is no information about it in the Laravel documentation, documentation says only how to do it manually by writing the command php artisan migrate.
Xampp includes a script called apache_startup.bat in the root directory, you can add the following line to it
c:\xampp\php\php.exe -f /path/to/laravel/app/artisan migrate
Then when the apache server starts, it should run that the equivalent of php artisan migrate to setup the database.
You can do php artisan make:migration [migration-name] - other than that, you have to fill the rest yourself.

My laravel project works great on xampp but when I try and run it using php artisan serve it times out

I have a strange issue where I can run my Laravel project that uses mysql perfectly when using xamp php 7.2 , So normally if it runs well in xampp it should run well if I just decide to use php artisan serve and access it via http://127.0.0.1:8000/ But to my surprise it doesn't'.
If I use php artisan serve the debug message I get from my Laravel project is
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Maximum execution time of 60 seconds exceeded
I tried giving full permissions to all folders as well as run the following commands:
composer update
composer dump-auto-load
php artisan serve
Any one else experience this strange issue? Where it works perfect in xampp but if you cd to the project directory and run php artisan serve, it just times out.
If you are using the PHP's default web server (eg. php artisan serve) you need to restart your server after any changing your php.ini file values.
Try to run in a different port.
php artisan serve --port=8080
and if still doesn't work, set time limit to your controller, in __contruct function,
public function __construct()
{
set_time_limit(300);
}
This problem is occering due to maximum execution time defined on php configuration. As common factor please use following:
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
Add the above lines at the top of your server.php file.

Laravel-Php artisan serve url (127.0.0.1:8000) vs localhost/laravelproject/public

I want to access my laravel project.I run php artisan serve and access the 127.0.0.1:8000 in browser.
But i learned that I can also check my project using the localhost/laravelproject/public url whithout running php artisan serve.
Question: What is the point of using php artisan serve?
No point in two different methods like you mentioned run laravel by "php artisan serve" and by "project url" followed by localhost. But advantage of "php artisan serve" is you can run you laravel project without putting in htdocs/www directory i.e servers root directory. You can put laravel project anywhere where you want and run through the artisan command.
I found some information you may find interesting:
https://www.quora.com/How-can-I-use-php-artisan-serve
But in simple words, php artisan serve is an easy way to create a php server and that is what laravel needs to run.
You could do the same with "php -S 8080 (which would start a php web server (single threaded) in the current directory on port 8080)"
Also if you have already a php server running with apache or nginx it would not be necessary any of the commands.
Hope you find this helpful.
The `Serve command is just a shortcut for the PHP Builtin Webserver, something PHP has out of the box, so the point of using it is to start testing your application as fast as you could, you just need to install PHP, Composer and your application is up (if you don't need anything else, of course). But if you already have Nginx installed, there is no point at all, just use it.
It's not wise to use the Builtin Webserver in production.

Laravel Dusk ignores .env.dusk and .env.dusk.local (Using Valet and Laravel 5.5)

I'm trying to run Laravel Dusk and I need to use a test database. When I check the screenshot it says that the database doesn't exist. Problem is that is the database defined on .env and not the one on .env.dusk ... I've tried to rename the file to .env.dusk.local and still no luck. What am I doing wrong?
Try clearing your cache? I remember having similar issues when I first started using dusk.
php artisan cache:clear
I got the same problem. Lately I figured out that the .env file is being cached before dusk start.
I resolved it by adding
$this->artisan('config:cache');
into the createApplication() function in CreateApplication.php. It works :)
But then, if I finished the dusk test, I must run
php artisan config:cache
to get the original server running.
Edit:
later I tried simply call the
php artisan config:clear
also prevent the application caching the env variable. (Much simpler and faster)
I was able to get this to work by following these steps:
Remove .env.testing
Run php artisan dusk (This will create phpunit.dusk.xml).
Add the following to the end of this newly created file
<phpunit>
...
<php>
<server name="APP_ENV" value="local"/>
</php>
</phpunit>
You can now restore .env.testing which can still be used for phpunit testing.
ORIGINAL ANSWER
I found that if you remove .env.testing it then uses .env.dusk.local. It seems that .env.testing always take precedence.
This does not solve the problem of using a separate .env for unit tests & dusk; which is useful when running unit tests in memory and dusk tests using sqlite.
Same issue... The problem is you forgot to create config cache file for your testing env so dusk will use your current local config cache file instead. Here is the fix:
1/ duplicate .env to .env.dusk.testing and edit the following:
APP_ENV=testing
DB_DATABASE=halfwish_test #I use different MYSQL database for testing.
2/ run this cmd:
$ php artisan config:cache --env=dusk.testing && php artisan clear //clear compiled services and packages files and cache config file.
$ php artisan migrate:fresh --env=dusk.testing && php artisan seed --env=dusk.testing //this is migrating and seeding demo data to DB.
Now you can run php artisan dusk.

Resources