编辑API下拉
我在选择选项中填充了国家列表,当我在用户菜单数据中单击“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论