Php 如果条件取决于输入文件,则为Laravel控制器

Php 如果条件取决于输入文件,则为Laravel控制器,php,laravel,Php,Laravel,我试图为我的用户创建一个编辑功能,他们可以在其中编辑自己的数据,但我希望这取决于用户将更改什么,如果其他字段未更改,则将处于当前状态。您将看到下面的代码,如果只更改了一个代码,则代码不起作用,但如果我更改了所有代码,则数据将更改 Myprofile//我的用户视图 它可以读取数据,但如果我将其添加到if-else语句中,则需要两个字段 我尝试像这样传递默认数据 myprofile//用户视图 学生控制员 并将其添加到特定的函数中,如 public function Functionname(R

我试图为我的用户创建一个编辑功能,他们可以在其中编辑自己的数据,但我希望这取决于用户将更改什么,如果其他字段未更改,则将处于当前状态。您将看到下面的代码,如果只更改了一个代码,则代码不起作用,但如果我更改了所有代码,则数据将更改

Myprofile//我的用户视图

它可以读取数据,但如果我将其添加到if-else语句中,则需要两个字段

我尝试像这样传递默认数据

myprofile//用户视图

学生控制员

并将其添加到特定的函数中,如

 public function Functionname(Request $request, $id, $username, $bio)
你们能帮我个忙吗谢谢

当您在用户名字段中使用
Value=“{{Request::old('username')}}”时,它可能是空的,因为在您第一次打开该页面时它没有任何值。
在控制器中,当您使用
if($request->has('username'))
时,它返回true,因为username字段已发送,但值为空

您应该检查用户名字段是否为空值,然后继续

例如:

if ($request->filled('username')) {
// do something
}

如果您只想在请求中更改数据,只需将默认值传递给输入函数即可。如果请求中不存在密钥,则将使用该值。如果键存在,但它是一个空字符串,那么可以使用三元语句检查是否有值

这可能是最简单的编写方法:

public function updateprofilemany(Request $request, $id)
{

    $this->validate($request, [
        'username' => 'max:15|unique:Students',
        'bio' => 'max:50'
    ]);

    // get the user
    $student = User::find($id);

    // just pass the default value as the old username if the key is not supplied
    // and if it is an empty string it will also use the old username
    $student->username = $request->input('username', $student->username) 
                         ?: $student->username;

    // also pass the default here as the old bio
    $student->bio = $request->input('bio', $student->bio)
                    ?: $student->bio;

    $student->save();

}

您能提供适当的学生控制功能吗?导致代码难以理解,例如
$student=User::find($id);如果($request->has('username'){$student->username=$request->input('username');}$student->save()has('username'))
总是正确的话,
不会吗?我更新了我的代码,所以你们可以check@kerbholz所以putting和else语句不起作用?我试过了,但如果我在两个语句上都输入,它就起作用了,但是如果说我只想更改我的用户名,那么它只是刷新没有错误或其他东西谢谢:)如果我在我的用户名中输入了一些东西并将bio留空,它会工作,但是如果我做了bio并将username留空,它不会工作。你能解释实际发生了什么吗?与其说它不起作用,不如说如果我在用户名中输入了一些东西,并将bio留空,它就会起作用,但如果我输入了bio并将用户名留空,它就不会起作用。也许你的bio列可以留空,但用户名不可以。我想我用你的代码修复了它,如果我去掉验证,它就会起作用。所以我所做的就是把我的验证放在if-true语句中,想知道这是否安全?好的,是的,无论如何,这是一种验证,它没有问题。
//  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)
if ($request->filled('username')) {
// do something
}
public function updateprofilemany(Request $request, $id)
{

    $this->validate($request, [
        'username' => 'max:15|unique:Students',
        'bio' => 'max:50'
    ]);

    // get the user
    $student = User::find($id);

    // just pass the default value as the old username if the key is not supplied
    // and if it is an empty string it will also use the old username
    $student->username = $request->input('username', $student->username) 
                         ?: $student->username;

    // also pass the default here as the old bio
    $student->bio = $request->input('bio', $student->bio)
                    ?: $student->bio;

    $student->save();

}