Laravel Controller,如果条件取决于输入字段

时间:2019-01-08 12:00:42

标签: php laravel

我正在尝试为用户提供一种编辑功能,使他们可以编辑自己的数据,但是我希望它取决于用户将要更改的内容以及其他字段是否保持不变。您将看到下面的代码,如果仅更改了一个代码,则该代码将不起作用,但是如果我更改了所有代码,则数据将被更改。

  

我的个人资料//我的用户查看

Update myTable set MyColumn = NULL where Field = ''
  

StudentController.php //我的用户控制器

 <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>

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

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

  

myprofile //用户查看

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');
}
  

Web.php //路由

     <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>
  

学生控制器

//  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
 }

你们能帮我这个谢谢吗!

2 个答案:

答案 0 :(得分:0)

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

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

例如:

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

答案 1 :(得分:0)

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

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

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();

}