在Laravel 10 liveire 3中,我显示用户通知列表:

namespace App\Livewire\Personal;

use Livewire\WithPagination;

class ProfileEditor extends Component
{
    use WithPagination;
    public $notifications;
    public int $notificationsPage= 1;
    public bool $notificationsOnlyUnread= true;


    public function render(): View
    {
        $this->notifications =  auth()->user()->notifications()->latest()->paginate(2);

        return view('livewire.personal.profile-editor')->layout('components.layouts.personal');
    }

但在Blade中我遇到了错误:

Livewire中不支持属性的属性类型:[{"当前_页面":1,"数据":[{"id":"f4 bec 30 e-e661 - 4f 91-aafd-a9 ae 623 CF269","类型":"App\ ...

在模板行中

<table class="editor_listing_table mb-4">
    <thead class="editor_listing_table_header">
    <tr>
        <td>
            <h3 class="personal_subtitle">
                Your Notifications
            </h3>
        </td>
    </tr>
    </thead>
    @foreach($notifications as $notification)
    {{--                            {{ dd($notification)--}}
    <tr>
        <td class="editor_listing_cell whitespace-nowrap">
            {{$notification->notifiable_id}}
        </td>
        <td class="editor_listing_cell whitespace-nowrap">
            {{$notification->notifiable_type}}
        </td>

    </tr>
    @endforeach
</table>

@if(count($notifications) > 0)
<div class="livewire-pagination">
    {{ $notifications->onEachSide(2)->links(data: ['scrollTo' => false]) }}
</div>
@endif

如果要取消 comments 行

dd($notification)

我看到:

enter image description here With DatabaseNotification class for any row item.

如果在dd中使用toArray方法,我会看到有效的数据行,但无论如何,这

不支持属性类型.

错误并且我不知道如何修复它?事实上,我没有在Blade 文件中引用"类型"字段

推荐答案

您不需要将其作为活动属性,因此可以将其作为变量传递给视图.

首先,拆除房产

public $notifications;

然后更新您的渲染方法,通过view()数据参数将其传递.

public function render(): View 
{ 
    $notifications = auth()->user()->notifications()->latest()->paginate(2); 

    return view('livewire.personal.profile-editor', [
            'notifications' => $notifications,
        ])
        ->layout('components.layouts.personal'); 
}

此更改意味着您现在将变量传递到视图中,而不是它是组件类的属性(然后在视图中公开为变量).

这对Livewire来说是一个重要的变化,因为任何数据都可以像我们现在在上面所做的那样传递到视图中,但并非所有类型都可以设置为Livewire类的public个属性.

Livewire-类别中的public属性需要在每次后续请求时重新水化(Livewire将对其进行序列化,以便在重新水化时复制其确切状态),默认情况下仅支持limited number of types.翻页不是其中之一.

Laravel相关问答推荐

当使用Craftable PRO时,Wysiwig是什么时候生成的?

从 Laravel 中的值开始主键

Laravel:如何强制使用 HTTPS?

Laravel Blade - 产生内部部分

laravel 控制器中的全局变量

Laravel 5 - 仅在特定页面/控制器(页面特定assets资源)上添加样式表

Laravel 中间件 except除外规则不起作用

Laravel 监听器监听多个事件

使用'with'时,Laravel belongsTo 返回 null

Laravel 判断约束违规

laravel 4:更新行时如何从列的当前值中减一?

判断输入是否来自控制台

是否可以将路由参数传递给 Laravel 中的控制器构造函数?

Select,where JSON 包含数组

控制器中的artisan调用输出?

在 Laravel 中包含 bootstrap 程序

Eloquent - 带有字符串值而不是列标题的连接子句

使用哪个 Auth::check() 或 Auth::user() - Laravel 5.1?

Laravel 集合计数结果

laravel:如何获取 eloquent 模型的列名?