【发布时间】: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