View in laravel error undefined variable

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Solving the "Undefined Variable" Mystery in Laravel: A Deep Dive into Data Flow

As a fellow developer, I’ve seen countless beginners struggle with data flow in frameworks like Laravel. The error "Undefined variable: results" is one of the most common hurdles when moving from simple PHP scripting to full-fledged MVC (Model-View-Controller) architecture. It doesn't usually signal a bug in the database query itself, but rather a misunderstanding of how data is passed between the Controller and the View.

This post will dissect the exact problem you are facing with your Laravel setup, explain why the error occurs, and provide a robust, best-practice solution. We will look at your provided code snippets and refactor them to ensure seamless data delivery.

Understanding the Error: Why is $results Undefined?

The error Undefined variable: results (View: ...\absen.blade.php) means that when the Blade engine tried to execute the line @foreach ($results as $value), the variable named $results did not exist in the scope of that view file.

In Laravel, data flows from the Controller to the View using the return view('view_name', $data) method. If you fetch data in the Controller but forget to explicitly pass that data into the view function, the view simply has no knowledge of those variables.

Let’s look at your setup:

  1. Controller Action (BookController@absen): You correctly executed a database query and stored the result in $results.
  2. Redirection: You used return Redirect::to('books/see');. This action immediately sends a browser redirect, it does not automatically pass data to the destination view unless you explicitly tell it to render something.
  3. View (absen.blade.php): The view expects $results to be available, but it isn't because the Controller didn't deliver it correctly in this specific flow.

The Correct Approach: Passing Data from Controller to View

The solution is to ensure that the data retrieved in the Controller is explicitly passed as an array or object into the View::make() function when rendering the view.

We need to adjust your routes and controller to coordinate the flow correctly. We will use the POST request to fetch the data, store it, and then redirect back to the GET route, passing the fetched data along the way.

Step 1: Refactoring the Controller Logic

Instead of just redirecting, the controller should handle fetching the data and ensure that this data is available for display. Since you are using a POST request to trigger the action, we need to make sure the results are accessible when the GET route is called.

For this example, let's assume the data retrieval happens in the method that handles the redirection, or we pass it directly from the initial view load. The most straightforward way for your setup is to ensure the data exists before rendering the view on the GET request.

Step 2: Implementing the Fix (The Refactored Code)

We will modify the process to retrieve and store the results within a structure that can be easily accessed by the view.

A. Routes (routes.php)

Keep your routes as they are, but we need to ensure the GET route is the primary point of display.

use Illuminate\Support\Facades\Route;

Route::get('books/see', function() {
    // This will now load the view and expect data to be present
    return View::make('books.absen'); 
});

Route::post('books/absen', 'BookController@absen');

B. Controller (BookController.php)

The controller must now capture the results and pass them when redirecting. Since you are using raw queries, we will focus on formatting the output correctly for the view.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect; // Ensure this is imported

class BookController extends Controller
{
    public function absen(Request $request)
    {
        // 1. Get input from the form
        $ruang = $request->input('ruangn');

        // 2. Perform the database query using raw SQL (as per original request)
        $results = DB::select(DB::raw("SELECT id, name, isbn, ta, tb FROM book WHERE ta = '$ruang'"));

        // 3. Redirect back to the 'see' route AND pass the results using session or redirect data.
        // For simplicity here, we will store the results in the session and redirect.
        session()->flash('books_data', $results);
        
        return Redirect::to('books/see');
    }

    public function see()
    {
        // Retrieve the data stored in the session before rendering
        $results = session('books_data', []); 
        
        // Now, pass the $results variable directly to the view!
        return View::make('books.absen', ['results' => $results]); 
    }
}

C. View (absen.blade.php)

Now that the Controller is explicitly passing the data under the key results, your Blade file can access it without error.

<select name="ruangn" class="form-control" method="post" action="{{URL::to('books/absen')}}">
    {{-- ... options loop remains the same ... --}}
</select>
<input type="submit" name="submit" value="Oke" class="btn btn-info">

<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>ISBN</th>
            <th>TA</th>
            <th>TB</th>
        </tr>
    </thead>
    <tbody>
        {{-- Access the data now successfully --}}
        @foreach ($results as $value)
            <tr>
                <td>{{$value->id}}</td>
                <td>{{$value->name}}</td>
                <td>{{$value->isbn}}</td>
                <td>{{$value->ta}}</td>
                <td>{{$value->tb}}</td>
            </tr>
        @endforeach
    </tbody>
</table>

Best Practices: Embracing Eloquent and Data Transfer

While the session method above fixes your immediate error, as a senior developer, I strongly recommend moving away from raw SQL queries (DB::select(DB::raw(...))) when working with Laravel. For database interactions, using Eloquent ORM is safer, more readable, and leverages Laravel's powerful relationships. Following principles discussed on the official site, like those outlined at https://laravelcompany.com, will make your application maintainable.

Instead of raw queries, you should define a Model (e.g., Book) and use it to interact with the database:

// Example using Eloquent instead of DB::select()
$books = Book::where('ta', $ruang)->get(); 
// This returns a Collection object, which is much easier to handle in Blade.

By making this shift, you stop manually managing raw result arrays and start working with structured objects, significantly reducing the chance of runtime errors like "Undefined variable."

Conclusion

The error "Undefined variable: results" was a classic data flow issue, not a database error. The key takeaway is that data must be explicitly passed from where it is created (the Controller) to where it is consumed (the View). By carefully managing the return values and using methods like session() or passing variables directly in View::make(), you master the MVC pattern in Laravel. Keep practicing, embrace Eloquent, and your Laravel development journey will become much smoother!