Why am I getting undefined variable in my view file when using compact()?

  debugging, laravel, php

Anyone know why I get Undefined variable $shortLinks error? I’m getting this error inside my app view. I’m using PHP 8.0 and Laravel 8.40.

I’ve tried virtually many different ways shown on the web in terms of sending the controller variable to the view but to no avail. What am I doing wrong?


Route::get('/', function () {
    return view('app');

Route::get('generate-shorten-link', [UrlController::class, 'index']);
Route::post('generate-shorten-link', [UrlController::class, 'store'])->name('generate.shorten.link.post');
Route::get('{code}', [UrlController::class, 'shortenLink'])->name('shorten.link');



namespace AppHttpControllers;
use AppModelsUserLink;
use IlluminateHttpRequest;
use IlluminateHttpResponse;
use IlluminateSupportStr;

class UrlController extends Controller {

    public function index() {
        $shortLinks = UserLink::findOrFail();
        return view('app',compact('shortLinks'));

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

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


        return redirect('generate-shorten-link')->with('success', 'Shorten Link Generated Successfully!');

     * Display a listing of the resource.
     * @return Response
    public function shortenLink($code) {
        $find = UserLink::where('code', $code)->first();
        return redirect($find->link);

app.blade.php view:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>LTV URL Shortener</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Nunito:[email protected];600;700&display=swap" rel="stylesheet">

<body class="antialiased">
<div class="card">
    <div class="card-header">
        <form method="POST" action="{{ route('generate.shorten.link.post') }}">
            <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('shorten.link', $row->code) }}" target="_blank">{{ route('shorten.link', $row->code) }}</a></td>
                    <td>{{ $row->link }}</td>

Source: Ask PHP