Laravel create or update without two queries

后端 未结 6 2244
有刺的猬
有刺的猬 2020-12-15 09:21

I\'m trying to use one form for both creates and updates. Both actions save through this method:

public function store() {
    $data = Input::all();
    $dat         


        
6条回答
  •  轮回少年
    2020-12-15 09:41

    If you have all fields unguarded in your model, I think you can do it like this:

    $feature = new Feature($data);
    $feature->exists = Input::has('id');
    $feature->save();
    

    If you have some guarded fields, then you can unguard it first:

    $feature = new Feature();
    $feature->unguard();
    $feature->fill($data);
    $feature->exists = Input::has('id');
    $feature->reguard();
    $feature->save();
    

    The reguard() call is not actually needed if you don't do anything else with the model.

提交回复
热议问题