编辑API下拉

发布于 2025-02-05 13:01:59 字数 2023 浏览 5 评论 0原文

我在选择选项中填充了国家列表,当我在用户菜单数据中单击“ country”中的country secal保存,然后在更新用户字段的同时,country名称未显示/或从API加载中的旧列表中,这发生在Laravel 08

代码中,以填充选择选项

        document.addEventListener('DOMContentLoaded', () => {
        
            const selectDrop = document.querySelector('#countries');
            // const selectDrop = document.getElementById('countries');
        
        
            fetch('https://restcountries.com/v3.1/all').then(res => {
              return res.json();
            }).then(data => {
            let output = "";
              data.forEach(country => {
                console.log(country.name.common)
                output += `<option value="${country.name.common}">${country.name.common}</option>`;
              })
        
            selectDrop.innerHTML = output;
        
            }).catch(err => {
              console.log(err);
            })
          });

是我的控制器

    public function update(Request $request){
                try{
            $user= User::data_update($request);
            return view ('profile_edit', ['user'=>$user]);
                }catch (\Exception $exception){
                    return $exception;
                }
            }

,这是我的模型

    public static function data_update($user1)
        {
                $user = self::user_data();
                $user->name = $user1['name'];
                $user->email = $user1['email'];
                $user->phone = $user1['phone'];
                $user->gender = $user1['gender'];
                $user->country = $user1['country'];
                $status = $user->save();
                return $user;
    
    }

,这里是我的刀片

    <div class="col-sm-9 text-secondary">
        <select id="countries"  name="country">
            <option value="{{$user->country}}" selected ></option>
        </select>
    </div>

I populated the country list in my select option and when I click on country in user menu data save successfully and afterward while updating the user field the country name is not showing/or the old list from api loads this happens in laravel 08

Code to populate select option

        document.addEventListener('DOMContentLoaded', () => {
        
            const selectDrop = document.querySelector('#countries');
            // const selectDrop = document.getElementById('countries');
        
        
            fetch('https://restcountries.com/v3.1/all').then(res => {
              return res.json();
            }).then(data => {
            let output = "";
              data.forEach(country => {
                console.log(country.name.common)
                output += `<option value="${country.name.common}">${country.name.common}</option>`;
              })
        
            selectDrop.innerHTML = output;
        
            }).catch(err => {
              console.log(err);
            })
          });

here is my controller

    public function update(Request $request){
                try{
            $user= User::data_update($request);
            return view ('profile_edit', ['user'=>$user]);
                }catch (\Exception $exception){
                    return $exception;
                }
            }

here is my model

    public static function data_update($user1)
        {
                $user = self::user_data();
                $user->name = $user1['name'];
                $user->email = $user1['email'];
                $user->phone = $user1['phone'];
                $user->gender = $user1['gender'];
                $user->country = $user1['country'];
                $status = $user->save();
                return $user;
    
    }

here is my blade

    <div class="col-sm-9 text-secondary">
        <select id="countries"  name="country">
            <option value="{{$user->country}}" selected ></option>
        </select>
    </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文