Laravel雄辩的关系插入/更新最佳实践

时间:2020-01-22 06:56:02

标签: laravel eloquent polymorphism

关于这个问题,我进行了很多搜索,但是找不到“正确”的答案。 我正在尝试使用关系的自动更新来插入/更新数据库条目(据我所知,这在单个调用中是不可能的)。

第一个问题:

  • 这是正确的方法
  • 是否可以更新人员中的相关地址(记录)->创建/保存呼叫种类。
->save([
'persondata',
'address' => [
    'zip' => 'xxxx',
    'nr' => 1
 ]
];

我目前的状况

数据库布局:

表:人员: id |名称

表:地址: id | zip | nr | addressable_id | addressable_type

型号:

人员:

public function address()
{
    return $this->morphOne('App\Addresses', 'addressable');
}

地址:

public function addressable()
{
    return $this->morphTo();
}

控制器 人:

store()
{
    $validated = request()->validate([
        'name' => 'required',
        'address.zip' => 'required',
        'address.nr' => 'required'
    ]);
    $person = Person::create($validated);
    $address = new Addresses(request()->input('address'));
    $address->addressable()->associate($person);
    $address->save();
}
update(Person $person)
{
    $validated = request()->validate([
        'name' => 'required',
        'address.zip' => 'required',
        'address.nr' => 'required'
    ]);
    $person->update($validated);
    $person->address->update(request()->input('address'));
}

2 个答案:

答案 0 :(得分:0)

这几乎是基于意见的,但我会执行以下操作:

php artisan make:request PersonRequest

# app\Http\Request\PersonRequest.php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PersonRequest extends FormRequest
{
    public function rules()
    {
        // Note I'm NOT putting zip an nr into an array.
        return [
            'name' => 'required',
            'zip' => 'required',
            'nr' => 'required'
        ];
    }
}
public function store(PersonRequest $request)
{
    $person = Person::create($request->validated());
    $person->address()->create($request->validated());
}

public function update(PersonRequest $request, Person $person)
{
    $person->update($request->validated());
    $person->address()->update($request->validated());
}

您会发现我只是在重复使用validated。那是因为我依赖于模型的可填充属性,然后让那些去掉我不需要的已验证请求的部分。

# Assuming Person's fillable attributes are only 'name'
Person::create(['name' => 'name', 'zip' => 'zip', 'nr' => 'nr'])
# Is the same as Person::create(['name' => 'name']);

此解决方案最适用于不共享属性名称的模型。

答案 1 :(得分:0)

// Person Model
public function person()
    {
        return $this->belongsTo('App\Person');
    }

// Address Model
    public function addresses()
    {
        return $this->hasMany('App\Addresses');
    }

// Controller 
// create function
 public function create()
    {
        $address = Addresse::all();
        return view('admin.post.create',compact('categories));
    }
// store function
 public function store(Request $request)
    {

            $this->validate($request,[
                'name' => 'required',
                'addressable_id' => 'required',
            ]);
            $person             = new Person();
            $person->addressable_id  =$id;
            $person->name      = $request->name;
            $person->save();
            Toastr::success('Your Name Successfully submited','Success');
            return redirect()->back();

    }