我现在正在玩Laravel,试图找出它是否适合用于项目的框架。
我已经从here下载了生成器包,并根据文档创建了一个资源。
这给了我一个作者和身体的表格。
生成的商店方法如下所示:
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$input = Input::all();
$validation = Validator::make($input, Tweet::$rules);
if ($validation->passes())
{
$this->tweet->create($input);
return Redirect::route('tweets.index');
}
return Redirect::route('tweets.create')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
似乎工作正常,只是$ input数组包含$ _GET变量以及$ _POST。它验证确定但在尝试保存模型时会导致异常,因为它包含意外字段($ _GET超全局中的任何内容都会添加到查询中)。
SQLSTATE [42S22]:未找到列:1054未知列'推文' '字段列表'(SQL:插入
tweets
(author
,body
,tweets
,updated_at
,created_at
)值(?,?,?,?,?))(绑定:数组( 0 => 'zzz',1 => 'zzzzz',2 => '',3 => '2013-07-02 10:23:16',4 => '2013-07-02 10:23:16',))
有没有办法只传递相关值,还是我必须手动删除任何我不想使用的内容?
任何建议表示赞赏。
由于
答案 0 :(得分:1)
据我所知,目前Laravel 4中没有办法只使用Input::all();
方法检索$ _GET或$ _POST输入,但有Input::only()
和{ {1}}如果你不知道的方法,那就完全按照他们在锡上说的那样......
只需传递一系列您想要包含在字符串中的键
Input::except()
要排除的键数组
$input = Input::only('author', 'body','tweets');
它只会检索您指定的值(或您未排除的值)。我知道它不像某种$input = Input::except('updated_at');
函数那么简单,但它是唯一不用改变Laravel的方法。我认为这可能是个人框架的一个很好的补充
答案 1 :(得分:1)
You can pass selective input variables to the withInput() method.
This is how your code would look like:
return Redirect::route('tweets.create')
->withInput(Input::except('tweets'))
->withErrors($validation)
->with('message', 'There were validation errors.');
also you can use to pass "selective inputs" or remove "selective inputs from all input vars". Here is the code you can use:-
$input = Input::only('username', 'password'); // selective inputs
$input = Input::except('tweets'); // remove selective inputs
答案 2 :(得分:0)
在Laravel 4中获取POST(PUT等)变量的最简单方法如下:
$post = Input::duplicate(array())->all();
表示:获取当前请求,克隆它,并用空数组替换$ _GET参数。
您可能需要进一步调查duplicate()
以了解如何避免与Cookie等进一步发生冲突。