Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PolMac's avatar

Passing DB to Blade template

Hi guys,

I am working on an application and as part of that app, when the user logins in (admin), it brings you to a admin page that will display the DB. When I pass in the credentials, the route works as the url changes to the correct blade but I get a "undefined variable" error and that variable is a table in the DB (customer), any ideas? Im pretty sure the code is all over the place at this stage as Ive been wrestling with it so long, chopping and changing

THIS IS THE BLADE TEMPLATE

@extends('layouts.layout')

@section('content')

Customer Enquiries

        <div class="col-lg-6">
                <h3>Customer Table</h3>
                 @foreach ( $customer as $customer)

                <!--Display customers table-->
                <h2>{{ $customer->custID }} <small>{{ $customer->name }}: {{ $customer->number }}:{{ $customer->email }}: {{ $customer->comments }}:{{ $customer->bookingID }}</small></h2>
                @endforeach

                <!-- Display bookings table -->
                <h3>Booking Table</h3>
                @foreach ($bookings as $booking)
                    <p>{{ $booking->bookingID }}:{{ $booking->custID }}:{{ $booking->productID }}</p>
                @endforeach

                <!-- Display Products table -->
                <h3>Products Table</h3>
                @foreach($products as $product)
                <p>{{ $product->productID }}:{{ $product->name }}:{{ $product->description }}</p>
                @endforeach
        </div><!-- col-lg-6 -->
@endsection

THIS is the ROUTES file

0 likes
8 replies
tykus's avatar

You will need to show us your routes file and/or controller.

One thing that jumps out is @foreach ( $customer as $customer); should it be $customers as $customer?

PolMac's avatar

thanks for the quick reply. the undefined variable was originally "$customers" I changed to customer to see if it had any effect.

this is the route file

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

Route::get('home', function (){ return View::make('home'); });

Route::get('example', function(){ return View::make('example'); });

Route::get('enquiries', function (){ return View::make('enquiries'); });

Route::post('enquiries','EnquiriesController@');

Route::get('login', array('uses' => 'HomeController@showLogin'));

// route to process the form /Route::post('login', array('uses' => 'HomeController@doLogin'));/

/Route::get('admin', function () { //brings Auth method into action })->middleware('auth');/

Auth::routes();

Route::get('/', 'HomeController@index')->name('welcome');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
1 like
tykus's avatar

Still not seeing the routes file!

PolMac's avatar

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

Route::get('home', function (){ return View::make('home'); });

Route::get('example', function(){ return View::make('example'); });

Route::get('enquiries', function (){ return View::make('enquiries'); });

Route::post('enquiries','EnquiriesController@');

Route::get('login', array('uses' => 'HomeController@showLogin'));

// route to process the form /Route::post('login', array('uses' => 'HomeController@doLogin'));/

/Route::get('admin', function () { //brings Auth method into action })->middleware('auth');/

Auth::routes();

Route::get('/', 'HomeController@index')->name('welcome');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Robstar's avatar

Back on topic, you at least need to show us (within proper code blocks) relevant Controllers and anything else relevant like View Composers.

I'm not use if the following will work, but it will helps show you what data is available to Blade:

{{ dd(get_defined_vars()['__data']) }}

However, it's probably easier to just show us the controller where you're creating all the variables in question.

tykus's avatar
tykus
Best Answer
Level 104

That routes file is a bit of a mess... you have multiple definitions for some URIs. The routes file is read from top to bottom, so the first match will be used.

I notice that there are views being returned from the closures in some of the earlier route definitions; if these are the offending routes, then you are doing nothing to pass data from into the view.

I would suggest tidying up your routes file first, removing any routes that you are not using. If you are new to all of this, just use closure routes for now, e.g.

Route::get('home', function () {
    // Example queries, yours might be more specific
    $customers = App\Customer::all();
    $bookings = App\Booking::all();
    $products = App\Product::all();

    // return the view along with the data
    return view('enquiries', compact('customers', 'bookings', 'products'));
});

The compact() function creates an associative array of the specified variables which you can then use inside your view:

compact('customers', 'bookings', 'products');

// is equivalent to 
[
    'customers' => $customers,
    'bookings' => $bookings,
    'products' => $products
]
PolMac's avatar

Thank you for the help and suggestions

Please or to participate in this conversation.