【问题标题】:Laravel Request ConversionLaravel 请求转换
【发布时间】:2017-07-11 09:42:57
【问题描述】:

任何人都可以帮我转换它并帮助菜鸟了解其中的区别。

/**
     * User Delete Account via Profile
     *
     * @access protected
     * @return void
     *
     */
    protected function deleteProfile(Request $request) {
        $this->validate($request, [
            'confirmation' => 'required',
            'password' => 'required',
        ]);
        $usr = User::findOrFail(Auth::user()->id);
        if (Hash::check($request->password, $usr->password)) {
            Auth::logout();
            // Removes UserID from Torrents if any and replaces with System UserID (0)
            foreach(Torrent::where('user_id', '=', $usr->id)->get() as $tor) {
                $tor->user_id = 0;
                $tor->save();
            }
            // Removes UserID from Comments if any and replaces with System UserID (0)
            foreach(Comment::where('user_id', '=', $usr->id)->get() as $com) {
                $com->user_id = 0;
                $com->save();
            }
            if($usr->delete()) {
                return view('members.delete_account');
            }
        } else {
            return redirect()->back()->with(Toastr::warning('Your Password Was Incorrect!', 'Error', ['options']));
        }
    }

这是设置为使用 Illuminate\Http\Request;

但我需要它与 Illuminate\Support\Facades\Request 一起使用;

【问题讨论】:

  • 错误代码按原样抛出:``` ValidatesRequests.php 第 50 行中的 FatalThrowableError:类型错误:传递给 App\Http\Controllers\Controller::validate() 的参数 1 必须是Illuminate\Http\Request,给定的 Illuminate\Support\Facades\Request 实例,在 /home/UNIT3D_2.0/app/Http/Controllers/UserController.php 的第 269 行调用```
  • 您应该编辑您的问题并在此处添加错误。

标签: php laravel laravel-5 namespaces laravel-5.4


【解决方案1】:

查看评论,您在控制器中导入了无效的请求类。在您的控制器文件中,您应该有:

use Illuminate\Http\Request;

代替:

use Illuminate\Support\Facades\Request;

编辑

如果你的其他函数已经使用了Illuminate\Support\Facades\Request,你可以像这样给这个类起别名:

 use Illuminate\Http\Request as IlluminateRequest;

然后在你的deleteProfile 中使用:

protected function deleteProfile(IlluminateRequest $request) 
{
   // ...
}

【讨论】:

  • 是的,唯一的问题是我的控制器中的其他功能使用 Illuminate\Support\Facades\Request;
猜你喜欢
  • 2019-12-17
  • 2017-04-04
  • 2021-01-09
  • 2020-05-12
  • 1970-01-01
  • 2021-06-30
  • 2018-04-16
  • 1970-01-01
  • 2021-02-06
相关资源
最近更新 更多