在 Laravel 中存储和更新 - 使用哪个请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34580584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:57:54  来源:igfitidea点击:

store and update in Laravel - which Request to use?

phplaravel

提问by user3100193

I'm new to Laravel and I have problems with storing and updating a Model.

我是 Laravel 的新手,我在存储和更新模型时遇到了问题。

Here is my store method

这是我的存储方法

 public function store(Request $request)
{

    $input = Request::all();

    Klijent::create($input);

    return redirect('klijenti');

}

And I have to include use Request;to make it work.

我必须包括在内use Request;才能使其发挥作用。

Here is my update method

这是我的更新方法

    public function update(Request $request, $id)
{
    //

    $klijent = Klijent::find($id);

    $klijent->update($request->all());

    return redirect('klijenti/'. $id);

}

And I have to include use Illuminate\Http\Request;to make it work.

我必须包括在内use Illuminate\Http\Request;才能使其发挥作用。

But if I don't use first one I get this error when using store method:

但是,如果我不使用第一个,则在使用 store 方法时会出现此错误:

Non-static method Illuminate\Http\Request::all() should not be called statically, assuming $this from incompatible context

If I don't use second one, I get this error when I use update method:

如果我不使用第二个,则在使用更新方法时会出现此错误:

Call to undefined method Illuminate\Support\Facades\Request::all()

If I use both of them I get this error:

如果我同时使用它们,则会出现此错误:

Cannot use Illuminate\Http\Request as Request because the name is already in use

回答by wogsland

You need to make the call to non-static methods like

您需要调用非静态方法,例如

$input = $request->all();

in your first function. The second error is because Illuminate\Support\Facades\Requestdoes not have an allmethod to call. The third error is a namespace conflict, because you cannot have two classes with the same name in PHP.

在你的第一个函数中。第二个错误是因为Illuminate\Support\Facades\Request没有all方法可以调用。第三个错误是命名空间冲突,因为在 PHP 中不能有两个同名的类。