I want to read data from my database, but I keep receiving the following error;
Undefined array key "title"
I am using PHP 8.3.13 and Laravel 11.42.1 with Livewire 3
my table
app\Livewire\Product.php
<?php
namespace App\Livewire;
use Livewire\Component;
use App\Models\Product;
#[Layout('layouts.app')]
class Products extends Component
{
public $products = [];
public function mount()
{
$this->products = Products::all();
}
public function render()
{
return view('livewire.products');
}
}
app\Models\Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Products extends Model
{
protected $fillable = [
'title',
'description',
'price',
'image'
];
}
app.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ $title ?? 'Page Title' }}</title>
<!-- Fonts -->
<link rel="preconnect" href=";>
<link href=":400,500,600&display=swap" rel="stylesheet" />
<!-- Scripts -->
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body class="font-sans antialiased">
<div class="min-h-screen bg-gray-100">
<livewire:layout.navigation />
<!-- Page Heading -->
@if (isset($header))
<header class="bg-white shadow">
<div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
{{ $header }}
</div>
</header>
@endif
<!-- Page Content -->
<main>
{{ $slot }}
</main>
</div>
</body>
</html>
product.blade.php
<div>
<table>
<thead>
<th>Title</th>
<Th>Deskripsi</Th>
<th>Price</th>
<th>Action</th>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
@foreach ($products as $product)
<tr class="hover:bg-gray-50">
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900">{{ $loop->iteration }}</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900">{{ $product['title'] }}</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900">
Rp{{ number_format($product->price, 0, ',', '.') }}</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
When I dump-die the products;
dd($this->products);
I receive the following output;
array:1 [▼ // app\Livewire\Products.php:20
"products" => []
]
The table is not detected.
I have tried to
- Clear the cache with
php artisan cache:clear
- Run the migrations using
php artisan migrate
- Build a new project, but I keep running into the same problem
I want to show all of the data from the product table in product.blade.php
I want to read data from my database, but I keep receiving the following error;
Undefined array key "title"
I am using PHP 8.3.13 and Laravel 11.42.1 with Livewire 3
my table
app\Livewire\Product.php
<?php
namespace App\Livewire;
use Livewire\Component;
use App\Models\Product;
#[Layout('layouts.app')]
class Products extends Component
{
public $products = [];
public function mount()
{
$this->products = Products::all();
}
public function render()
{
return view('livewire.products');
}
}
app\Models\Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Products extends Model
{
protected $fillable = [
'title',
'description',
'price',
'image'
];
}
app.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ $title ?? 'Page Title' }}</title>
<!-- Fonts -->
<link rel="preconnect" href="https://fonts.bunny">
<link href="https://fonts.bunny/css?family=figtree:400,500,600&display=swap" rel="stylesheet" />
<!-- Scripts -->
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body class="font-sans antialiased">
<div class="min-h-screen bg-gray-100">
<livewire:layout.navigation />
<!-- Page Heading -->
@if (isset($header))
<header class="bg-white shadow">
<div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
{{ $header }}
</div>
</header>
@endif
<!-- Page Content -->
<main>
{{ $slot }}
</main>
</div>
</body>
</html>
product.blade.php
<div>
<table>
<thead>
<th>Title</th>
<Th>Deskripsi</Th>
<th>Price</th>
<th>Action</th>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
@foreach ($products as $product)
<tr class="hover:bg-gray-50">
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900">{{ $loop->iteration }}</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900">{{ $product['title'] }}</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900">
Rp{{ number_format($product->price, 0, ',', '.') }}</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
When I dump-die the products;
dd($this->products);
I receive the following output;
array:1 [▼ // app\Livewire\Products.php:20
"products" => []
]
The table is not detected.
I have tried to
- Clear the cache with
php artisan cache:clear
- Run the migrations using
php artisan migrate
- Build a new project, but I keep running into the same problem
I want to show all of the data from the product table in product.blade.php
1 Answer
Reset to default 0You need to specify the Post
class explicitly since both your livewire component and eloquent model have the same classname.
On app\Livewire\Product.php
, change your mount()
method to the following.
public function mount()
{
$this->products = \App\Models\Product::all();
}
This should fix your issue.
I recommend you to change your Livewire classname to something else like ProductTable
. Try to name the component that will describe what it will do.
dd($this->products);
would return an array with 1 element with a key ofproducts
that is an empty array, when you set it as$this->products = Products::all();
– Qirel Commented yesterday