Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel 7中更改用户需要身份验证_Php_Laravel_Forms_Dropdown_Html Select - Fatal编程技术网

Php 在Laravel 7中更改用户需要身份验证

Php 在Laravel 7中更改用户需要身份验证,php,laravel,forms,dropdown,html-select,Php,Laravel,Forms,Dropdown,Html Select,有人知道我如何要求授权才能更改用户吗。在我的表单中,我有一个下拉列表选择“所有用户”,默认用户是auth用户 控制器 public function create() { $users = User::all(); $auth = Auth()->user()->name; return view('evaluation.create', ['users' => $users, 'auth' => $auth

有人知道我如何要求授权才能更改用户吗。在我的表单中,我有一个下拉列表选择“所有用户”,默认用户是auth用户 控制器

 public function create()
    {   
        $users = User::all();
        $auth = Auth()->user()->name;
        return view('evaluation.create', ['users' => $users, 'auth' => $auth]);
    }

以及视图中的下拉列表

<label name="expert">Expert:</label>
<select name="expert">
  <option selected="">{{$auth}}</option>
  @foreach ($users as $user)   
    <option value="{{$user->name}}">{{$user->name}}</option>
  @endforeach
</select>
专家:
{{$auth}}
@foreach($users作为$user)
{{$user->name}
@endforeach

所以我列出了所有用户,值是auth user,但是如果我想选择另一个用户,我可以这样做,这不是我想要的。我想要的是,如果您想更改用户以将您发送到登录,并且您必须使用您选择的用户登录,因为默认选项是auth user。谢谢你

最好在选择框中使用
id
作为值,因为它在所有用户中都是唯一的。然后可以使用
auth()->login()
设置用户。那么您的代码应该是这样的:

html部分:

<form method="post" action="/somewhere">
<label name="expert">Expert:</label>
<select name="expert">
  <option selected="">{{$auth}}</option>
  @foreach ($users as $user)   
    <option value="{{$user->id}}">{{$user->name}}</option>
  @endforeach
</select>
</form>
控制器部分:

function switch(Request $request) {
    auth()->loginUsingId($request->get('expert'));
    // or use this: auth()->login(User::findOrFail($request->get('expert')));
    return redirect()->back();
}

但是此代码极易受攻击,您应该使用验证和更多的值和权限检查。

基本上您想显示登录的用户名,如果用户经过身份验证,则应禁用选择框?如果没有授权用户,我们可以选择任何用户名?不,在我的选择框中,我显示了所有用户,如果我想选择另一个不是经过身份验证的用户的用户,它应该发送给我登录到该用户的日志,他的名字将显示出来,因为选择的选项是经过身份验证的用户。我不想禁用选择框。
function switch(Request $request) {
    auth()->loginUsingId($request->get('expert'));
    // or use this: auth()->login(User::findOrFail($request->get('expert')));
    return redirect()->back();
}