【问题标题】:Update fields from two tables in Laravel更新 Laravel 中两个表的字段
【发布时间】:2019-11-19 19:05:22
【问题描述】:

我有 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');
    }

}

客户名称模型

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

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

【问题讨论】:

    标签: laravel eloquent laravel-6 eloquent-relationship


    【解决方案1】:

    假设客户总是为 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 对象。

    【讨论】:

      【解决方案2】:

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

      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()

      【讨论】:

      • 感谢您的宝贵时间!
      • 没问题,乐于助人!
      【解决方案3】:

      您正在注入 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);
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-14
        • 2020-07-24
        • 2020-11-12
        • 2019-09-15
        • 2017-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-29
        相关资源
        最近更新 更多