【问题标题】:dropdown box using Laravel 8 jetstream使用 Laravel 8 jetstream 的下拉框
【发布时间】:2021-03-25 20:48:37
【问题描述】:

如何使用 laravel Laravel 8 jetstream .. ?JetStream 在用户注册中创建依赖下拉国家、州、城市国家、州和城市?

【问题讨论】:

    标签: laravel-8


    【解决方案1】:

    假设您使用的是 Livewire + Blade Stack,按照官方documentation

    这些模板中的每一个都将接收整个经过身份验证的用户对象,以便您可以根据需要向这些表单添加其他字段。添加到表单的任何其他输入都将包含在传递给 UpdateUserProfileInformation 操作的 $input 数组中。

    以上意味着你将需要一些普通的 Laravel、HTML(或 Blade 部分)和 Livewire 模型标签。

    1. 将任何必要的迁移添加到 users 表中。
    2. 将给定的 $fillable 属性添加到 User 模型中:
    //app/Models/User.php
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
     protected $fillable = [
        ...
        'city', //Assuming new column name is city
        ...
     ];
    
    1. App\Actions\Fortify\UpdateUserProfileInformation 操作类中添加任何输入验证逻辑及其将保存到的属性。
    //app/Actions/Fortify/UpdateUserProfileInformation.php
    
                $user->forceFill([
                    ...
                    'city' => $input['city'],
                    ...
                ])->save();
    
    1. 修改 resources/views/profile/update-profile-information-form.blade.php 以包含呈现下拉选择框的 HTML 或部分内容:
    //resources/views/profile/update-profile-information-form.blade.php
    //Assuming the column name is 'city'
    //Assuming you are not using a Blade partial
    
    <div class="col-span-6 sm:col-span-4">
        <x-jet-label for="city" value="{{ __('City') }}" />
        <select 
            name="city"
            id="city"
            class="block w-full mt-1"
            wire:model.defer="state.city"
            >
            <optgroup label="Ontario">
                <option value="toronto">Toronto</option>
                <option value="markham">Markham</option>
            </optgroup>
            <optgroup label="Quebec">
               <option value="montreal">Montreal</option>
               <option value="qc">Quebec City</option>
            </optgroup>
        </select>
    

    请注意Livewire 应该通过标签 wire:model.defer="state.city" 处理任何数据绑定。

    【讨论】:

    • 如果您的选项是数据库驱动的呢?有什么好方法可以提取这些数据吗?
    猜你喜欢
    • 2021-01-13
    • 2021-10-01
    • 1970-01-01
    • 2021-04-07
    • 2021-12-11
    • 2021-01-01
    • 2021-04-23
    • 2021-06-15
    • 2021-10-24
    相关资源
    最近更新 更多