如何检索输入字段

How to retrieve input field

如何获取用于更新任务的输入字段?当试图 dd(Request::input('task')); 我得到 "Non-static method Illuminate\Http\Request::input() should not be called statically, assuming $this from incompatible context"

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
use Illuminate\Http\Request;

class TaskController extends Controller {

public function update($id)
{
    dd(Request::input('task'));
    $task = Task::findOrFail($id);
    $task->title = Request::input('task');
    $task->save();
    return Redirect::to('tasks');
}

编辑视图(工作正常)

{!! Form::model($task, array('url' => 'tasks/'.$task->id, 'method' => 'PATCH')) 
    !!}
    {!! Form::text('task', $task->task, array('class' => 'form-control')) !!}
    {!! Form::submit('Edit', array('class' => 'btn btn-default')) !!}
    {!! Form::close() !!}

你输入错误Request

要使用 Request 外观,您必须:

use Illuminate\Support\Facades\Request

或者只是别名:

use Request

你也可以像这样使用依赖注入:

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;
use Illuminate\Http\Request;

class TaskController extends Controller {

    public function __construct(Request $request){
        $this->request = $request;
    }

    public function update($id){
        $task = Task::findOrFail($id);
        $task->title = $this->request->input('task');
        $task->save();
        return Redirect::to('tasks');
    }
}

或仅在该方法上使用 DI:

public function update(Request $request, $id){
    $task = Task::findOrFail($id);
    $task->title = $request->input('task');
    $task->save();
    return Redirect::to('tasks');
}

你应该为此使用依赖注入:

use Illuminate\Http\Request;

...

public function update(Request $request, $id)
{
    //
    $task->title = $request->input('task');
    //
}

请参阅文档以获取更多信息:http://laravel.com/docs/5.0/controllers#dependency-injection-and-controllers