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() !!}
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');
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With