首页 > 解决方案 > 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)

你们能帮我解决这个问题吗?谢谢!

标签: phplaravel

解决方案


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

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

例如 :

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

推荐阅读