您可以使用 Laravel 5 在不在数据库中创建条目的情况下填充 Eloquent 模型吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30462397/
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 11:38:18  来源:igfitidea点击:

Can you populate an Eloquent model without creating an entry in the database using Laravel 5

phplaraveleloquentlaravel-5

提问by Styphon

When I'm adding or editing an entry to my database table websitesI load the instance of the website to be modified (or a blank one for creating a website). This works great, this is my controller:

当我向我的数据库表网站添加或编辑条目时,我加载要修改的网站实例(或用于创建网站的空白实例)。这很好用,这是我的控制器:

<?php namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
use App\Models\User;
use App\Models\Status;
use App\Models\Website;

class WebsitesController extends Controller {

    /**
     * Show / Process the create website form to the user.
     *
     * @return Response
     */
    public function create()
    {
        $statuses = Status::all();
        $users = User::all();
        $website = Website::find(0);
        return view('admin/websites/create', [
            'statuses' => $statuses,
            'users' => $users,
            'website' => $website
        ]);
    }

    public function update($id)
    {
        $statuses = Status::all();
        $users = User::all();
        $website = Website::findOrFail($id);
        return view('admin/websites/update', [
            'statuses' => $statuses,
            'users' => $users,
            'website' => $website
        ]);
    }

}

The problem is when I submit the form and there is an error. The user is returned to the page and the errors displayed. I also pass the users input back so I can repopulate the form with what they entered. But how can I replace the values in website with the values from input if it's present without actually saving to the database? I've been playing around with this all day and not found a working solution.

问题是当我提交表单时出现错误。用户返回到页面并显示错误。我还将用户输入传回,以便我可以用他们输入的内容重新填充表单。但是,如果网站中的值存在而没有实际保存到数据库中,我该如何用输入中的值替换网站中的值?我整天都在玩这个,但没有找到可行的解决方案。

My create method is this:

我的创建方法是这样的:

public function postCreate(Request $request)
{
    $v = Validator::make($request->all(), Website::rules());
    if ($v->fails())
    {
        return redirect()->back()->withInput()->withErrors($v);
    }
    $website = Website::create($request->all());
    return redirect()->action('Admin\HomeController@index')->with('messages', [['text' => 'Website created', 'class' => 'alert-success']]);
}

I'm passing the input back to the original form, but the form populates its values from the Website Eloquent model. **How can I get the input from $request->all()into $website?

我将输入传递回原始表单,但该表单从网站 Eloquent 模型填充其值。**如何从$request->all()into获取输入$website

I've tried using fill(), but I just get Call to a member function fill() on nullwhen using it in the create function.

我试过使用fill(),但我只是Call to a member function fill() on null在 create 函数中使用它时才得到。

回答by Don't Panic

The createmethod attempts to insert a record to the database and returns an instance of the model if it is successful. If you use create()with invalid values, the insert will fail. I think this is why there is a nullinstead of an instance of the model, which causes your error:

create方法尝试向数据库插入一条记录,如果成功则返回模型的一个实例。如果使用create()无效值,插入将失败。我认为这就是为什么有一个null而不是模型的实例,这会导致您的错误:

Call to a member function fill() on null

在 null 上调用成员函数 fill()

Instead of using create()You could create the website model without the database insert using

而不是使用create()您可以使用创建没有数据库插入的网站模型

$website = new Website;
$website->fill($request->all());

before you run the validation. If the validation passes, then you can insert to your database with $website->save();, otherwise, it will not try to save, but the model should be available for you to use in your form.

在运行验证之前。如果验证通过,那么您可以使用 插入到您的数据库中$website->save();,否则,它不会尝试保存,但模型应该可供您在表单中使用。