最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - Undefined array key "title" in laravel 11, llivewire 3 - Stack Overflow

programmeradmin5浏览0评论

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

Share Improve this question asked 2 days ago Joshia Agrisa Anrhiely LutherJoshia Agrisa Anrhiely Luther 1 1
  • Is that your actual code? Because I can't see how dd($this->products); would return an array with 1 element with a key of products that is an empty array, when you set it as $this->products = Products::all(); – Qirel Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

You 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.

发布评论

评论列表(0)

  1. 暂无评论