In this tutorial, we'll create a simple Laravel application, add products using migrations and seeders, display them on a welcome page, push the project to GitHub, and deploy it completely free using Railway.
By the end of this guide, you'll have a live Laravel website connected to a MySQL database and accessible from anywhere in the world.
Step 1: Create a New Laravel Project
Create a fresh Laravel application:
laravel new test
Move into the project directory:
cd test
Run the application:
php artisan serve
Step 2: Create Product Migration
Create a Product model, migration, and seeder:
php artisan make:model Product -ms
Update the migration file:
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->decimal('price', 8, 2);
$table->timestamps();
});
}
Run the migration later to create the products table.
Step 3: Create Product Seeder
Open the ProductSeeder and insert sample products:
public function run(): void
{
\App\Models\Product::create([
'name' => 'Product 1',
'description' => 'This is a greate product',
'price' => 19.99
]);
\App\Models\Product::create([
'name' => 'Cake',
'description' => 'This is a cafe product',
'price' => 10.99
]);
}
These products will be inserted into the database automatically when the seeder runs.
Step 4: Create Application Routes
Next, let's create routes to display products and manage migrations directly from the browser.
Update your routes/web.php file:
use Illuminate\Support\Facades\Artisan;
use App\Models\Product;
Route::get('/', function () {
$products = Product::all();
return view('welcome', compact('products'));
});
Route::get('/migrate-seed', function () {
Artisan::call('migrate:fresh', ['--seed' => true]);
return 'Migration and seeding completed successfully.';
});
Route::get('/migrate', function () {
Artisan::call('migrate');
return 'Migration and seeding completed successfully.';
});
Route::get('/clear', function () {
Artisan::call('config:cache');
Artisan::call('cache:clear');
Artisan::call('view:clear');
Artisan::call('route:clear');
return 'Caches cleared and config cached successfully.';
});
These routes are useful when deploying to Railway and quickly running migrations or clearing caches.
Note: For production applications, secure these routes or remove them after deployment.
Step 5: Create the Welcome Page
Update the welcome.blade.php file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel Products</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f8f9fa;
color: #333;
margin: 0;
padding: 40px;
display: flex;
justify-content: center;
}
.container {
background-color: #fff;
max-width: 600px;
width: 100%;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
h1 {
margin-top: 0;
color: #f53003;
text-align: center;
}
.product {
border: 1px solid #eee;
border-radius: 8px;
padding: 15px;
margin-bottom: 15px;
background-color: #fafafa;
}
.product h3 {
margin: 0 0 10px 0;
color: #222;
}
.product p {
margin: 0 0 10px 0;
color: #666;
line-height: 1.5;
}
.price {
font-weight: bold;
color: #28a745;
font-size: 1.1em;
}
.no-data {
text-align: center;
color: #888;
}
</style>
</head>
<body>
<div class="container">
<h1>Our Products</h1>
@if(isset($products) && $products->count() > 0)
@foreach($products as $product)
<div class="product">
<h3>{{ $product->name }}</h3>
<p>{{ $product->description }}</p>
<div class="price">
${{ number_format($product->price, 2) }}
</div>
</div>
@endforeach
@else
<div class="no-data">
<p>No products found. Please run the migration and seeder.</p>
</div>
@endif
</div>
</body>
</html>
Once migrations and seeders are executed, this page will display the products stored in the database.
Step 6: Push the Project to GitHub
Initialize Git:
git init
git add .
git commit -m "Laravel Railway Deployment Tutorial"
Create a repository on GitHub and push the code:
git remote add origin YOUR_GITHUB_REPOSITORY_URL
git branch -M main
git push -u origin main
Your project is now available on GitHub.
Step 7: Deploy on Railway
Go to Railway and create a new project.
Select:
Deploy from GitHub Repo
Choose the repository you just created.
Railway will automatically build and deploy the Laravel application.
Step 8: Add Laravel Environment Variables
Open the Railway dashboard and navigate to:
Variables
Add your Laravel environment variables add all the env file there .
APP_NAME=Laravel
APP_ENV=production
APP_DEBUG=false
APP_URL=https://your-app.up.railway.app
Step 9: Create a MySQL Database
Inside Railway:
-
Click New Service
-
Select MySQL
-
Railway will automatically generate database credentials
You will see variables such as:
MYSQLHOST
MYSQLPORT
MYSQLDATABASE
MYSQLUSER
MYSQLPASSWORD
Step 10: Connect MySQL to Laravel
Add these values to your Laravel variables:
DB_CONNECTION=mysql
DB_HOST=${{MYSQLHOST}}
DB_PORT=${{MYSQLPORT}}
DB_DATABASE=${{MYSQLDATABASE}}
DB_USERNAME=${{MYSQLUSER}}
DB_PASSWORD=${{MYSQLPASSWORD}}
Save the changes and allow Railway to redeploy the application.
Step 11: Run Migrations and Seeders
Visit:
https://your-domain.up.railway.app/migrate-seed
This route will:
-
Create database tables
-
Run all seeders
-
Insert sample products
Step 12: Clear Cache
Visit:
https://your-domain.up.railway.app/clear
This will clear and rebuild Laravel caches.
Step 13: View the Live Website
Open your Railway domain.
You should now see:
-
Product 1
-
Cake
Displayed on your Laravel welcome page directly from the MySQL database.
Final Result
Congratulations! You have successfully:
-
Created a Laravel application
-
Built migrations and seeders
-
Displayed database data on a Blade page
-
Pushed code to GitHub
-
Connected GitHub to Railway
-
Created a MySQL database
-
Configured environment variables
-
Ran migrations and seeders
-
Deployed a live Laravel website for free
You now have a production-ready Laravel deployment workflow that you can use for portfolios, business websites, SaaS applications, blogs, hotel booking systems, and many other projects.