php - Laravel 控制器如果条件取决于输入文件
问题描述
我正在尝试为我的用户创建一个编辑功能,他们可以在其中编辑自己的数据,但我希望它取决于用户将更改的内容以及其他字段是否未更改将是。您将看到下面的代码,如果只更改了一个,则它不起作用,但如果我更改了所有代码,则数据将被更改。
Myprofile // 我的用户视图
<form class="form-group" method="POST" action="{{ route('updateprofilemany', ['id' => auth()->user()->id]) }}" >
@csrf
//username
<input class="col-md-3 container justify-content-center form-control {{$errors->has('username') ? 'has-error' : ''}}" type="text" name="username" id="username" placeholder="{{ Auth::user()->username }}" Value="{{ Request::old('username') }}" />
//bio
<textarea class="border-info form-control {{$errors->has('bio') ? 'has-error' : ''}}" type="text" name="bio" id="bio" placeholder="{{ Auth::user()->bio }}" Value="{{ Request::old('bio') }}"></textarea> <br />
<button id="btn-login" class="btn btn-md r btn-primary" type="submit" > <i class="fa fa-cog"> </i> Save Changes </button>
</form>
StudentController.php // 我的用户控制器
public function updateprofilemany(Request $request, $id)
{
// Validation
$this->validate($request, [
'username' => 'max:15|unique:Students',
'bio' => 'max:50',
]);
if ($request->has('username'))
{
// if there is a new username value
$username = $request->input('username');
} else {
$username = Auth::user()->username;
}
if ($request->has('bio'))
{
// if there is a new username value
$bio = $request->input('bio');
} else {
$bio = Auth::user()->bio;
}
// Find the user and inject the new data if there is then go back to
myprofile
$students = User::find($id);
$students->username = $username;
$students->bio = $bio;
$students->save();
//redirect
return redirect()->route('myprofile');
}
它可以读取数据,但如果我将它添加到 if else 语句中,它需要这两个字段。
我尝试像这样传递默认数据
myprofile // 用户视图
<form class="form-group" method="POST" action="{{ route('updateprofilemany', ['id' => auth()->user()->id, 'username' => auth()->user()->username, 'bio' => auth()->user()->bio]) }}" >
@csrf
//username
<input class="col-md-3 container justify-content-center form-control {{$errors->has('username') ? 'has-error' : ''}}" type="text" name="username" id="username" placeholder="{{ Auth::user()->username }}" Value="{{ Request::old('username') }}" />
//bio
<textarea class="border-info form-control {{$errors->has('bio') ? 'has-error' : ''}}" type="text" name="bio" id="bio" placeholder="{{ Auth::user()->bio }}" Value="{{ Request::old('bio') }}"></textarea> <br />
<button id="btn-login" class="btn btn-md r btn-primary" type="submit" > <i class="fa fa-cog"> </i> Save Changes </button>
</form>
Web.php // 路由
// calls user update profile many function
Route::post('/updateprofilemany/{id}', [
'uses' => 'StudentController@updateprofilemany',
'as' => 'updateprofilemany',
'name' => 'updateprofilemany'
]);
学生控制器
public function updateprofilemany(Request $request, $id, $username ,$bio)
{
//functions like above
}
并将其添加到特定功能,例如
public function Functionname(Request $request, $id, $username, $bio)
你们能帮我解决这个问题吗?谢谢!
解决方案
当您Value="{{ Request::old('username') }}"
在用户名字段中使用时,它可能为空,因为它在您第一次打开该页面时没有任何值。而在控制器中,当您使用 时if ($request->has('username'))
,它会返回 true,因为已发送用户名字段但值为空。
您应该检查用户名字段是否为空值,然后继续...
例如 :
if ($request->filled('username')) {
// do something
}
推荐阅读
- ios - 错误 ITMS-90171:“无效的捆绑结构 - 二进制文件 'xxxx.app/app/tns_modules/fsevents/lib/binding/Release/node-v47-darwin-x64/fse.node'
- android - 使用 Gson 解析嵌套的 json 数组
- css - 如何将字体转换为实际链接以在 HTML 文档中使用
- rsync - 如何在 OpenStack Swift 中使用密码配置 rsync
- vue.js - 如何在 vue.js 中编写无序列表?
- amazon-web-services - Rails ActiveStorage 与 AWS S3 层
- c++ - C ++ 0x数组到迭代器而不使用开始/结束
- apache-nifi - 带有 grok 模式错误的 Telegraf 尾部
- javascript - 为什么 WP Bakery Page Builder 会自动删除内联 Javascript 调用?
- android - 尝试为当前用户 Firebase Auth 选择个人资料图片时不断出错