I have been hours at this and cannot get past this error which highlights the "@foreach ($vendors as $vendor)" line in the vendorapp.blade.php file.
The login (breeze) works fine and I can enter user credentials so I know the database is working fine. Somehow though the vendorapp blade doesn’t seem to use the vendorapp controller. When I change the vendorapp.blade.php file to only display $vendoritems, it gives the same error (undefined variable $vendoritems).
To also test that the controller is not used by the blade file, I can enter any rubbish (like "haha") in the controller without it throwing an error.
The route is web.php:
<?php
use IlluminateSupportFacadesRoute;
Route::view('/', 'welcome')
->middleware(['auth', 'verified'])
->name('welcome');
require __DIR__.'/auth.php';
The welcome.php file is below:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Vendor</title>
<!-- Fonts -->
<link rel="preconnect" href="https://fonts.bunny.net">
<link href="https://fonts.bunny.net/css?family=figtree:400,600&display=swap" rel="stylesheet" />
<!-- Styles -->
<script src="https://cdn.tailwindcss.com"></script>
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body class="antialiased font-sans">
@livewire('vendorapp')
</body>
</html>
The vendorapp.php controller:
<?php
namespace AppLivewire;
use AppModelsVendorItem;
use AppModelsVendor;
use LivewireComponent;
use AppLivewireActionsLogout;
use LivewireWithPagination;
class VendorApp extends Component
{
use WithPagination;
public $perPage = 25;
public $search = '';
public $sortDirection = 'ASC';
public $sortColumn = 'price';
public $filter = [];
public $filterInStockSydney;
public $filterInStockOther;
private $vendoritems = [];
/**
* Log the current user out of the application.
*/
public function logout(Logout $logout): void
{
$logout();
$this->redirect('/', navigate: true);
}
public function doSort($column)
{
if($this->sortColumn == $column) {
if ($this->sortDirection == "ASC") {
$this->sortDirection = "DESC";
}else{
$this->sortDirection = "ASC";
}
}else{
$this->sortColumn = $column;
$this->sortDirection = "ASC";
}
}
//lifecycle hooks
// reset the page when there is an updated perpage field
public function updatedPerPage(){
$this->resetPage();
}
// reset when there is an updated search
public function updatedSearch(){
$this->resetPage();
}
public function render()
{
$vendoritems = VendorItem::search($this->search);
if (!empty($this->filterInStockSydney)) {
$vendoritems = $vendoritems->Where('in_stock_sydney',true);
}
if (!empty($this->filterInStockOther)) {
$vendoritems = $vendoritems->Where('in_stock_other',true);
}
return view('livewire.vendorapp',
[
'vendoritems' => $vendoritems->orderBy($this->sortColumn, $this->sortDirection)
->paginate($this->perPage),
'vendors' => Vendor::orderBy('vendor','ASC')->get()
]
);
}
}
And the vendorapp.blade.php blade file:
)
<div>
<section class="mt-10">
<div class="md:flex">
<div class="md:flex-shrink-0">
<div class="mx-auto max-w-screen-xl px-4 lg:px-12">
<div class="bg-white dark:bg-gray-800 overflow-hidden">
<div class="flex items-center justify-between d p-4">
<div class="flex">
<div x-data="{ tooltip: false }">
<div class="flex gap-10">
<div>
<input wire:model.live.debounce.300ms="search" type="text" class="bg-gray-50 border border-gray-300" placeholder="Search" required="">
</div>
<div>
Stock Sydney: <input wire:model.live="filterInStockSydney" type="checkbox" class="bg-gray-50 border border-gray-300" required="">
</div>
<div>
Stock Other: <input type="checkbox" wire:model.live="filterInStockOther" class="bg-gray-50 border border-gray-300" required="">
</div>
<div>
<!-- this section is a tool tip popup -->
<button x-on:click="tooltip = !tooltip">
File dates
</button>
<div x-show="tooltip" class="z-50 x-50 absolute bg-white border-graphite border-2 rounded p-4 mt-1">
@foreach ($vendors as $vendor)
<tr class="border-b dark:border-gray-700">
<td class="px-4 py-3">
{{$vendor->vendor}} {{$vendor->refreshed}}<br>
</td>
</tr>
@endforeach
</div>
</div>
<div>
<label>Per Page</label>
<select class="w-20" wire:model.live="perPage">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
</select>
</div>
<div>
<button wire:click="logout" class="w-full text-start">
<x-dropdown-link>
{{ __('Log Out') }}
</x-dropdown-link>
</button>
</div>
</div>
</div>
</div>
</div>
<div class="overflow-x-auto">
<table class="w-full text-sm text-left text-gray-500">
<thead class="text-xs text-gray-700 uppercase">
<tr>
<th wire:click="doSort('vendor')" class="px-4 py-3">
<x-sort-column :sortColumn="$sortColumn" :sortDirection="$sortDirection" columnName="vendor"/>
</th>
<th wire:click="doSort('item')" class="px-4 py-3">
<x-sort-column :sortColumn="$sortColumn" :sortDirection="$sortDirection" columnName="item"/>
</th>
<th wire:click="doSort('manufacturer')" class="px-4 py-3">
<x-sort-column :sortColumn="$sortColumn" :sortDirection="$sortDirection" columnName="manufacturer"/>
</th>
<th class="px-4 py-3">
Description
</th>
<th wire:click="doSort('price')" class="px-4 py-3">
<x-sort-column :sortColumn="$sortColumn" :sortDirection="$sortDirection" columnName="price"/>
</th>
<th class="px-4 py-3">
Stock Sydney
</th>
<th class="px-4 py-3">
Stock Other
</th>
<th class="px-4 py-3">
ETA Sydney
</th>
<th class="px-4 py-3">
ETA Other
</th>
</tr>
</thead>
<tbody>
@foreach ($vendoritems as $vendoritem)
<tr class="border-b dark:border-gray-700">
<td class="px-4 py-3">
{{$vendoritem->vendor}}
</td>
<td class="px-4 py-3">
{!!$vendoritem->item!!}
</td>
<td class="px-4 py-3">
{{$vendoritem->manufacturer}}
</td>
<td class="px-4 py-3">
{{$vendoritem->description}}
</td>
<td class="px-4 py-3">
{{$vendoritem->price}}
</td>
<td class="px-4 py-3">
{{$vendoritem->stock_sydney}}
</td>
<td class="px-4 py-3">
{{$vendoritem->stock_other}}
</td>
<td class="px-4 py-3">
{{$vendoritem->eta_sydney}}
</td>
<td class="px-4 py-3">
{{$vendoritem->eta_other}}
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="py-4 px-3">
{{$vendoritems->links()}}
</div>
</div>
</div>
</div>
</div>
</section>
</div>
I have looked at capitals of classes and tried with different options to no avail.
Any help would be great!
2
Answers
Ah, I feel such an idiot. The problem was the name of the controller. After changing it from vendorapp.php to Vendorapp.php (capital V), it worked.
please comment foreach block in cpanel and report us if your page loads