I'm developing a blogging application in Laravel 8.
I'm preparing to deploy it on a live server, and I want the deployment process to be very user-friendly.
To this end, I have been developing an "installer" for the application:
In routes\web.php
I have:
Route::get('/install', [InstallController::class, 'index']);
In app\Http\Controllers\InstallController.php
I have this code in order to run the migration If there is no user Table:
class InstallController extends Controller { public function index() { if (!Schema::hasTable('users')) { Artisan::call('migrate'); } return redirect('/register')->with('success', 'Way to go! You can create an account.'); } }
The code above works, all tables are created and the (first) user is invited to register.
The problem is that I haven't found a way to have the controller run the database seeder after successfully creating the table.
You can do this by running
php artisan db:seed
or via theArtisan
look and feel, such asArtisan::call('db:seed');
Your code will be:
source: https://laravel.com/docs/9.x/seeding#running-seeder
However, I recommend not going this route and instead creating a deployment script that will do all of this for you. As such, you expose this route to all users who will use the application, and malicious users can exploit it.