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?

web.php:

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');

UrlController.php:

<?php

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) {
        $request->validate([
            'link' => 'required|url'
        ]);

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

        UserLink::create($input);

        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()) }}">
<head>
    <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">
</head>

<body class="antialiased">
<div class="card">
    <div class="card-header">
        <form method="POST" action="{{ route('generate.shorten.link.post') }}">
            @csrf
            <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>
            </div>
        </form>
    </div>
    <div class="card-body">

        @if (Session::has('success'))
            <div class="alert alert-success">
                <p>{{ Session::get('success') }}</p>
            </div>
        @endif

        <table class="table table-bordered table-sm">
            <thead>
            <tr>
                <th>ID</th>
                <th>Short Link</th>
                <th>Link</th>
            </tr>
            </thead>
            <tbody>
            @foreach($shortLinks as $row)
                <tr>
                    <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>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
</div>
</body>
</html>

Source: Ask PHP

LEAVE A COMMENT