我的表格:<form action="{{route('settings.update')}}" method="POST"> @csrf <div class="col-sm-3"> <div class="form-group"> <label for="avatar">Upload a new one:</label> <input type="file" id="pic" name="pic"/> </div> </div> <div class="form-group"> <button type="submit" class="btn btn-primary">Upload avatar</button> </div> </form>我的控制器:public function update_settings($request){ $this->validate($request, [ 'pic' => 'required|image' ]); $path = $request->image->store('/img/'); Auth::user()->image = $path; return view('pages.settings');}错误:Too few arguments to function App\Http\Controllers\PagesController::update_settings(), 0 passed and exactly 1 expected我在 $request 中只传递图像文件,但由于某种原因控制器看不到它,我做错了什么?
1 回答
慕田峪9158850
TA贡献1794条经验 获得超7个赞
要通过依赖注入获取当前 HTTP 请求的实例,您应该Illuminate\Http\Request
在控制器构造函数或方法上对该类进行类型提示。当前请求实例将由服务容器自动注入:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
/**
* Store a new user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$name = $request->input('name');
//
}
}
很好看,在我注入依赖的方法中Illuminate\Http\Request
- 1 回答
- 0 关注
- 88 浏览
添加回答
举报
0/150
提交
取消