0

我现在只是在玩 Laravel,试图确定它是否是用于项目的一个不错的框架选择。

我已经从这里下载了生成器包,并按照文档创建了一个资源。

这给了我一个带有作者和正文的表格。

生成的 store 方法如下所示:

/**
     * 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。它验证 OK,但在尝试保存模型时会导致异常,因为它包含意外字段(来自 $_GET 超全局的任何内容都会添加到查询中)。

SQLSTATE [42S22]:未找到列:1054“字段列表”中的未知列“推文”(SQL:插入tweets( 、、、、、)值(? author、 ? body、 ? tweets、 ? updated_atcreated_at?))(绑定:数组(0 => 'zzz', 1 => 'zzzzz', 2 => '', 3 => '2013-07-02 10:23:16', 4 => '2013-07-02 10:23:16' , ))

有没有办法只传递相关值,或者我必须手动删除我不想使用的任何东西?

任何建议表示赞赏。

谢谢

4

3 回答 3

1

据我所知,目前在 Laravel 4 中无法仅使用该Input::all();方法检索 $_GET 或 $_POST 输入,但是如果您不知道,请执行它们Input::only()的方法Input::except()说在锡...

只需将您想要包含在字符串中的键的数组传递给他们

$input = Input::only('author', 'body','tweets');

或要排除的键数组

$input = Input::except('updated_at');

它只会检索您指定的值(或您未排除的值)。我知道它不像某种Input::all('post');功能那么容易,但这是唯一不用改变你自己的 Laravel 的方法。我个人认为这可能是对框架的一个很好的补充

于 2013-07-02T10:37:48.177 回答
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
于 2013-07-02T10:38:36.743 回答
0

在 Laravel 4 中获取 POST(PUT 等)变量的最简单方法如下:

$post = Input::duplicate(array())->all();

这意味着:获取当前请求,克隆它,并将 $_GET 参数替换为空数组。

您可能需要进一步调查duplicate()以了解如何避免与 cookie 等发生进一步冲突。

于 2013-12-18T14:59:33.633 回答