Skip to content

Instantly share code, notes, and snippets.

@andyg1
Last active April 26, 2018 15:48
Show Gist options
  • Save andyg1/dacdcae2ce93c5c29c6de10fcc2ac72e to your computer and use it in GitHub Desktop.
Save andyg1/dacdcae2ce93c5c29c6de10fcc2ac72e to your computer and use it in GitHub Desktop.
Working with Laravel Migrations on servers without shell access
Description:
- Provides a web interface to allow migrations to run from a webserver without shell access.
- The interface consists of three buttons: Migrate, Rollback and Status.
Usage:
- Add the required lines to the web.php routes file
- Place the controller and the blade files into their respective locations (either with version control, a cPanel interface, FTP, etc)
- Put the Laravel project APP_ENV key into any status except "production" in the .env file
- Hit the /install URL in a browser
- Run the command you want by hitting the button
- Review the feedback
- Put the APP_ENV back into "production" to lock out the migrations again
Notes:
- Tested in Laravel 5.5 - probably works in others too.
- Styled with Bootstrap 4.0.0
<!-- resources/views/migration/index.blade.php -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Migration Control</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-3">
<div class="row">
<h2>Migration Control</h2>
</div>
@if(Session::has('flash_message'))
<div class="row">
<div class="alert alert-info">
<pre>{{ print_r(Session::get('flash_message'), true) }}</pre>
</div>
</div>
@endif
<div class="row">
<div class="d-inline pr-3">
<form method="post" action={{ action('MigrationController@migrate') }}>
{{ csrf_field() }}
<button name='install' type="submit" class="btn btn-primary">Migrate</button>
</form>
</div>
<div class="d-inline pr-3">
<form method="post" action={{ action('MigrationController@rollback') }} onsubmit="return confirm('Are you sure you want to rollback? Database content will be lost!')">
{{ csrf_field() }}
<button name='install' type="submit" class="btn btn-danger">Rollback</button>
</form>
</div>
<div class="d-inline pr-3">
<form method="post" action={{ action('MigrationController@status') }}>
{{ csrf_field() }}
<button name='install' type="submit" class="btn btn-info">Status</button>
</form>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" type="text/javascript" ></script>
</body>
</html>
<?php
// app/Http/Controllers/MigrationController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
class MigrationController extends Controller
{
public function index()
{
return view('migration.index');
}
public function migrate()
{
Artisan::call('migrate');
return $this->report();
}
public function rollback()
{
Artisan::call('migrate:rollback');
return $this->report();
}
public function status()
{
Artisan::call('migrate:status');
return $this->report();
}
public function report()
{
return redirect()->action('MigrationController@index')->with('flash_message', $this->getReport());
}
public function getReport()
{
return Artisan::output();
}
}
// add these to the existing: routes/web.php
Route::get('/install','MigrationController@index');
Route::post('/migrate','MigrationController@migrate');
Route::post('/rollback','MigrationController@rollback');
Route::post('/status','MigrationController@status');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment