Laravel Jetstream Livewire 未定义变量
Posted
技术标签:
【中文标题】Laravel Jetstream Livewire 未定义变量【英文标题】:Laravel Jetstream Livewire undefined variable 【发布时间】:2021-09-18 00:30:58 【问题描述】:我用<x-slot name="form2">
复制了<x-slot name="form">
并在form-section.blade.php 中插入了变量$form2
,但得到了未定义$form2
的错误。我不知道为什么 update-profile-information-form.blade.php 没有发送$form2
html。我正在使用 Laravel 8。
错误:ErrorException 未定义变量:form2(查看: \resources\views\vendor\jetstream\components\form-section.blade.php)
form-section.blade.php
@props(['submit'])
<div $attributes->merge(['class' => '']) >
<x-jet-section-title>
<x-slot name="title"> $title </x-slot>
<x-slot name="description"> $description </x-slot>
</x-jet-section-title>
<div class="md:grid md:grid-cols-2 md:gap-6">
<div class="mt-2 md:mt-0 md:col-span-1">
<form wire:submit.prevent=" $submit ">
<div class="px-4 py-5 bg-white sm:p-6 shadow isset($actions) ? 'sm:rounded-tl-md sm:rounded-tr-md' : 'sm:rounded-md' ">
<div class="grid grid-cols-6 gap-6">
$form
</div>
</div>
@if (isset($actions))
<div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6 shadow sm:rounded-bl-md sm:rounded-br-md">
$actions
</div>
@endif
</form>
</div>
<div class="mt-2 md:mt-0 md:col-span-2">
<form wire:submit.prevent=" $submit ">
<div class="px-4 py-5 bg-white sm:p-6 shadow isset($actions) ? 'sm:rounded-tl-md sm:rounded-tr-md' : 'sm:rounded-md' ">
<div class="grid grid-cols-6 gap-6">
$form2
</div>
</div>
@if (isset($actions))
<div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6 shadow sm:rounded-bl-md sm:rounded-br-md">
$actions
</div>
@endif
</form>
</div>
</div>
</div>
update-profile-information-form.blade.php
<x-jet-form-section submit="updateProfileInformation">
<x-slot name="title">
__('Profile Information')
</x-slot>
<x-slot name="description">
__('Update your account\'s profile information and email address.')
</x-slot>
<x-slot name="form">
<!-- Name -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="name" value=" __('Name') " />
<x-jet-input id="name" type="text" class="mt-1 block w-full" wire:model.defer="state.name" autocomplete="name" />
<x-jet-input-error for="name" class="mt-2" />
</div>
<!-- Email -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="email" value=" __('Email') " />
<x-jet-input id="email" type="email" class="mt-1 block w-full" wire:model.defer="state.email" />
<x-jet-input-error for="email" class="mt-2" />
</div>
</x-slot>
<x-slot name="form2">
<!-- Profile Photo -->
@if (Laravel\Jetstream\Jetstream::managesProfilePhotos())
<div x-data="photoName: null, photoPreview: null" class="col-span-6 sm:col-span-4">
<!-- Profile Photo File Input -->
<input type="file" class="hidden"
wire:model="photo"
x-ref="photo"
x-on:change="
photoName = $refs.photo.files[0].name;
const reader = new FileReader();
reader.onload = (e) =>
photoPreview = e.target.result;
;
reader.readAsDataURL($refs.photo.files[0]);
" />
<x-jet-label for="photo" value=" __('Photo') " />
<!-- Current Profile Photo -->
<div class="mt-2" x-show="! photoPreview">
<img src=" $this->user->profile_photo_url " class="rounded-full h-20 w-20 object-cover">
</div>
<!-- New Profile Photo Preview -->
<div class="mt-2" x-show="photoPreview">
<span class="block rounded-full w-20 h-20"
x-bind:style="'background-size: cover; background-repeat: no-repeat; background-position: center center; background-image: url(\'' + photoPreview + '\');'">
</span>
</div>
<x-jet-secondary-button class="mt-2 mr-2" type="button" x-on:click.prevent="$refs.photo.click()">
__('Select A New Photo')
</x-jet-secondary-button>
@if ($this->user->profile_photo_path)
<x-jet-secondary-button type="button" class="mt-2" wire:click="deleteProfilePhoto">
__('Remove Photo')
</x-jet-secondary-button>
@endif
<x-jet-input-error for="photo" class="mt-2" />
</div>
@endif
</x-slot>
<x-slot name="actions">
<x-jet-action-message class="mr-3" on="saved">
__('Saved.')
</x-jet-action-message>
<x-jet-button wire:loading.attr="disabled" wire:target="photo">
__('Save')
</x-jet-button>
</x-slot>
</x-jet-form-section>
【问题讨论】:
这能回答你的问题吗? "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP 您必须将该 $from2 属性绑定到刀片,通常来自控制器或相关组件 我有我刚刚发现了问题,在 form-section.blade.php 你有: @props(['submit', 'test' => '']) 如果你没有为那个变量 test 设置默认值,我们将看到的变量就像未定义的一样
【讨论】:
以上是关于Laravel Jetstream Livewire 未定义变量的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 8 Jetstream:无法使用使用工厂和播种机播种的帐户登录
如何使用 laravel 8 +jetstream + spatie 为注册用户分配角色
在 Laravel 8 Jetstream 的 Blade 组件中绑定 Livewire 属性