6

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() !!}
1
  • Just saying, you can format code inline by wrapping it in backticks (`) and it'll make it look like code instead of emphasized text. Commented Feb 5, 2015 at 13:37

2 Answers 2

17

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');
}
Sign up to request clarification or add additional context in comments.

Comments

10

You should use Dependency Injection for this:

use Illuminate\Http\Request;

...

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

Please see the docs for more information: http://laravel.com/docs/5.0/controllers#dependency-injection-and-controllers

2 Comments

thx, I will need to look at that. I am watching Jeffrey Way tutorials but I dont understand that specific part so i just tried something very basic.
Laravel 5 is a great way to help developers stick to a set of standards. DI is extremely useful, and I much prefer them from Facades (not that I have any complaints about them). So yes, refer to as much as you can regarding the way in which L5 does things - your learning experience will be highly advantageous. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.