Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve input field

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() !!}
like image 247
Marko Avatar asked Feb 05 '15 13:02

Marko


2 Answers

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');
}
like image 82
lukasgeiter Avatar answered Oct 15 '22 12:10

lukasgeiter


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

like image 40
Mike Rockétt Avatar answered Oct 15 '22 12:10

Mike Rockétt