gpt4 book ai didi

laravel - 如何检索输入字段

转载 作者:行者123 更新时间:2023-12-02 10:27:19 26 4
gpt4 key购买 nike

如何获取用于更新任务的输入字段?当试图 dd(Request::input('task')); 我得到“非静态方法 Illuminate\Http\Request::input() 不应静态调用,假设 $this 来自不兼容的上下文”

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

编辑 View (工作正常)

{!! 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外观,您必须执行以下操作:

use Illuminate\Support\Facades\Request

或者只是别名:

use Request
<小时/>

您还可以像这样使用依赖注入(inject):

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

关于laravel - 如何检索输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28345330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com