更新特定列 Laravel

我有一个编辑配置文件部分,我希望用户能够更新除密码之外的所有内容。我尝试删除控制器上的密码行,但它没有更新它只是显示与以前相同的数据。感谢任何帮助。


这是我的编辑控制器


<?php


namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;

use Illuminate\Http\Request;

use App\User;



class UserController extends Controller

{

  public function __construct()

  {

      $this->middleware('auth');

  }


  public function edit(User $user)

  {

      $user = Auth::user();

      return view('users.edit', compact('user'));

  }


  public function update(Request $request,User $user)

  {



        $this->validate($request, [

            'org_name' => 'required|string|max:255|unique:users,org_name,'.$user->id,

            'email' => 'required|string|email|max:255|unique:users,email,'.$user->id,

            'username' => 'required|string|max:255|unique:users,username,'.$user->id,

            'password' => 'required|string|min:8',

            'country' => 'required|string|max:255',

            'org_type' => 'required|string|max:255',

            'depart' => 'required|string|max:255',

      ]);


        $user->org_name = $request->get('org_name');

        $user->email = $request->get('email');

        $user->username = $request->get('username');

        $user->password = bcrypt($request->get('password'));

        $user->country = $request->get('country');

        $user->org_type = $request->get('org_type');

        $user->depart = $request->get('depart');

        $user->save();


        return back();

    }

}

这是表格的一部分


  <div class="form-group row">

      <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>


      <div class="col-md-6">

          <input id="password" type="password" class="form-control" name="password" >


          @error('password')

              <span class="invalid-feedback" role="alert">

                  <strong>{{ $message }}</strong>

              </span>

          @enderror

      </div>

  </div>


慕村225694
浏览 84回答 2
2回答

慕仙森

问题是,如果您没有发送密码,您将返回视图。您需要在控制器上删除这些行:$user->password = bcrypt($request->get('password'));并删除:'password' => 'required|string|min:8',来自$this->validate($request,然后更新您的视图,使其不发送密码:<div class="form-group row">&nbsp; <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>&nbsp; <div class="col-md-6">&nbsp; &nbsp; &nbsp; <input id="password" type="password" class="form-control" name="password" >&nbsp; &nbsp; &nbsp; @error('password')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="invalid-feedback" role="alert">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>{{ $message }}</strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>&nbsp; &nbsp; &nbsp; @enderror&nbsp; </div></div><div class="form-group row">&nbsp; <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>&nbsp; <div class="col-md-6">&nbsp; &nbsp; &nbsp; <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">&nbsp; </div></div>这将起作用。

呼啦一阵风

尝试这个:&nbsp;public function update(Request $request)&nbsp; {&nbsp; &nbsp; $userId = auth()->user()->id;&nbsp; &nbsp; $this->validate($request, [&nbsp; &nbsp; &nbsp; &nbsp; 'org_name' => 'required|string|max:255|unique:users,org_name,'.$user->id,&nbsp; &nbsp; &nbsp; &nbsp; 'email' => 'required|string|email|max:255|unique:users,email,'.$user->id,&nbsp; &nbsp; &nbsp; &nbsp; 'username' => 'required|string|max:255|unique:users,username,'.$user->id,&nbsp; &nbsp; &nbsp; &nbsp; 'country' => 'required|string|max:255',&nbsp; &nbsp; &nbsp; &nbsp; 'org_type' => 'required|string|max:255',&nbsp; &nbsp; &nbsp; &nbsp; 'depart' => 'required|string|max:255',&nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user = User::find($userId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user->org_name = $request->get('org_name');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user->email = $request->get('email');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user->username = $request->get('username');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user->country = $request->get('country');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user->org_type = $request->get('org_type');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user->depart = $request->get('depart');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$user->save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return back();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP