简体   繁体   English

无法使用 Laravel Livewire 显示我的数据库中的值

[英]Can't display the values from my data base with Laravel Livewire

I'm using a Livewire component, this is my code:我正在使用 Livewire 组件,这是我的代码:

Search.php :搜索.php:

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Recipe;
use App\Models\Vegetable;
use App\Models\VegetablesRecipe;


class Search extends Component
{
    public $query = '';
    public $vegetables;

    public function mount()
    {
        $this->resetQuery();
    }

    public function resetQuery()
    {
        $this->vegetables = [];
    }

    public function render()
    {
        if ($this->query != null) {
            return view('livewire.search', [
                'vegetables' => Vegetable::where('name', 'like', '%'.$this->query.'%')->get()->toArray()
            ]);
        } else {
            return view('livewire.search', [
                'vegetables' => Vegetable::all()->toArray()
            ]);
        }
    }
}

Because on the mount you are resetting the vegetable array to empty array因为在安装时您将蔬菜阵列重置为空阵列

refer this fiddle for more details有关更多详细信息,请参阅此小提琴

https://laravelplayground.com/#/snippets/f51e212a-dab3-4325-b6b0-4c6af4c0ab72 https://laravelplayground.com/#/snippets/f51e212a-dab3-4325-b6b0-4c6af4c0ab72

Defining public $vegetables;定义public $vegetables; will prevent you from passing it to the view.将阻止您将其传递给视图。 Remove that.删除它。 Also remove your mounting logic.还要删除您的安装逻辑。

Thus you should have:因此你应该有:

class Search extends Component
{
    public $query = '';

    public function render()
    {
        if ($this->query != null) {
            return view('livewire.search', [
                'vegetables' => Vegetable::where('name', 'like', '%'.$this->query.'%')->get()->toArray()
            ]);
        } else {
            return view('livewire.search', [
                'vegetables' => Vegetable::all()->toArray()
            ]);
        }
    }
}

Also make sure you are calling @livewireScripts and @livewireStyles or their alternative syntaxes <livewire:styles /> <livewire:scripts /> from your main view which uses <livewire:search /> .还要确保从使用<livewire:search />的主视图中调用@livewireScripts@livewireStyles或它们的替代语法<livewire:styles /> <livewire:scripts /> > 。

Edit:编辑:

Alternatively, you could also use a public properly and write to it using $this->vegetables like so:或者,您也可以正确使用 public 并使用 $this->vegetables 写入它,如下所示:

class Search extends Component
{
    public $query = '';
    public $vegetables;

    public function render()
    {
            $this->vegetables = Vegetable::where('name', 'like', '%' . $this->query . '%')->get()->toArray();
            return view('livewire.search');
    }
}

Also, you can remove the @if(!empty($query))<\/code> and the @if(!empty($vegetables))<\/code> in place of a @forelse<\/code> utilising the @empty<\/code> statement rather than a @foreach<\/code> .此外,您可以使用@empty<\/code>语句而不是@foreach<\/code>删除@if(!empty($query))<\/code>和@if(!empty($vegetables))<\/code>来代替@forelse<\/code> 。 Eg例如

@forelse($vegetables as $vegetable)
    <li><span class="material-icons">lunch_dining</span>{{ $vegetable['name'] }}</li>
@empty
    <li>No result</li>
@endforelse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM