Laravel – How to retrieve input field

laravellaravel-5

How do I get input field for updating a task? When trying to
dd(Request::input('task')); I get "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');
}

Edit view (its working ok)

{!! 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() !!}

Best Solution

You imported the wrong Request.

To use the Request facade you have to do:

use Illuminate\Support\Facades\Request

Or just the alias:

use Request

You could also use dependency injection like this:

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');
    }
}

Or only use DI on that one method:

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