How to Create URL Shortener Through Laravel

Step 1: Create Project

composer create-project laravel/laravel example-app

Step 2: Create Table

php artisan make:migration create_short_links_table
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateShortLinksTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('short_links', function (Blueprint $table) {
     * Reverse the migrations.
     * @return void
    public function down()
php artisan migrate

Step 3: php artisan make:model ShortLink


namespace App;
use Illuminate\Database\Eloquent\Model;
class ShortLink extends Model
     * The attributes that are mass assignable.
     * @var array
    protected $fillable = [
        'code', 'link'

Step 4: Create Route

Route::get('/{code}',[App\Http\Controllers\ShortLinkController::class, 'shortenLink'])->name('');
Route::post('/generate-shorten-link',[App\Http\Controllers\ShortLinkController::class, 'store'])->name('');
Route::get('/',[App\Http\Controllers\ShortLinkController::class, 'index'])->name('home');

Step 5: Create Controller

namespace App\Http\Controllers;
use Illuminate\Support\Str;   
use Illuminate\Http\Request;
use App\ShortLink;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log;
class ShortLinkController extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index()
    $shortLinks = ShortLink::all();

    return view('shortenLink', compact('shortLinks'));

public function store(Request $request)
        'link' => 'required|url'

    $input['link'] = $request->link;
    $input['code'] = Str::random(6);


    return redirect()->route('home')->with('success', 'Shorten Link Generated Successfully!');

public function shortenLink($code)
    $find = ShortLink::where('code', $code)->first();

    if ($find) {
        return redirect($find->link);
    } else {
        return redirect()->route('home')->with('error', 'Short Link Not Found'); // Redirect to the home page with an error message

Step 6: Create View


<!DOCTYPE html>
    <title>Url shortener</title>
    <link rel="stylesheet" href="" />
<div class="container">
    <h1> Url shortener </h1>
    <div class="card">
      <div class="card-header">
        <form method="POST" action="{{ route('') }}">
            <div class="input-group mb-3">
              <input type="text" name="link" class="form-control" placeholder="Enter URL" aria-label="Recipient's username" aria-describedby="basic-addon2">
              <div class="input-group-append">
                <button class="btn btn-success" type="submit">Generate Shorten Link</button>
      <div class="card-body">
            @if (Session::has('success'))
                <div class="alert alert-success">
                    <p>{{ Session::get('success') }}</p>
            <table class="table table-bordered table-sm">
                        <th>Short Link</th>
                    @foreach($shortLinks as $row)
                            <td>{{ $row->id }}</td>
                            <td><a href="{{ route('', $row->code) }}" target="_blank">{{ route('', $row->code) }}</a></td>
                            <td>{{ $row->link }}</td>
php artisan serve


And modify as per your requirement !!!!!

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x