更新 Laravel 中两个表的字段

Update fields from two tables in Laravel

我有 2 个模型:customer 和 customerName。在我的客户控制器中,我尝试创建一个方法来更新两个表中的字段。任何的想法?谢谢!

客户控制器

public function update(Request $request, Customer $customer)
{
    $customer = \App\CustomerName::where('customer_id', $customer->id)->first();  // if I remove this line I can update just "name" from first table

    $data = $request->validate([
        'name' => 'required|string',  //is in customer migration
        'first_name'=> 'required',    //is in customerName migration
        'last_name'=> 'required',     //is in customerName migration
    ]);
    $customer->update($data);

    return response($customer,200);
}

客户模型

class Customer extends Model
{
    protected $fillable = ['name'];

    public function customerName()
    {
        return $this->hasOne('App\CustomerName');
    }

}

CustomerName 型号

class CustomerName extends Model
{
    protected $fillable = ['first_name', 'last_name'];

    public function customer()
    {
        return $this->belongsTo('App\Customer');
    }
}

假设客户总是为 CustomerName 创建记录,那么您应该使用:

$customer->update(['name' => $data['name']);
$customer->customerName->update(\Arr::only($data, ['first_name', 'last_name']));

此外,您应该像这样将其包装在数据库事务中:

\DB::transaction(function() use ($customer, $data) {
    $customer->update(['name' => $data['name']);
    $customer->customerName->update(\Arr::only($data, ['first_name', 'last_name']));
});

当然你应该删除这一行:

$customer = \App\CustomerName::where('customer_id', $customer->id)->first();  // if I remove this line I can update just "name" from first table

因为您应该已经使用 Route model binding.

设置了 $customer 对象

看看你的代码。您通过将它们命名为相同的东西来覆盖一些变量:

public function update(Request $request, Customer $customer)
{
    $customer = \App\CustomerName::where('customer_id', $customer->id)->first();
    ...

$customer = \App\CustomerName...之前,$customerCustomer的实例。在该行之后,它是 CustomerName 的一个实例,您不再有权访问 Customer 实例。只需更改您的命名:

public function update(Request $request, Customer $customer)
{
    $customerName = \App\CustomerName::where('customer_id', $customer->id)->first();
    // Or, $customerName = $customer->customerName;
    // You shouldn't have to query if your relationship is defined properly.
    ...

接下来,相应地保存值:

$customer->name = $request->input("name"); // or $data["name"]
$customer->save();

$customerName->first_name = $request->input("first_name"); // or $data["first_name"]
$customerName->last_name = $request->input("last_name"); // or $data["last_name"]
$customerName->save(); 

相应地设置 $customer$customerName 的值,然后在两个实例上调用 save()

您正在注入 Customer 实例,因此您不需要在函数中加载它。试试这个:

public function update(Request $request, Customer $customer)
{
    $data = $request->validate([
        'name' => 'required|string',  //is in customer migration
        'first_name'=> 'required',    //is in customerName migration
        'last_name'=> 'required',     //is in customerName migration
    ]);
    $customer->name = $data['name'];
    $customer->customerName->first_name = $data['first_name'];
    $customer->customerName->last_name = $data['last_name'];

    $customer->push(); // This saves the model AND the related models as well.
    return response($customer,200);
}