我是Laravel的新手,目前正面临一个问题。
我正在尝试将记录插入另一个数据库,但是当我尝试保存它时,它会引发以下错误:
调用未定义的方法Illuminate \ Database \ Query \ Builder :: save()
这是我的代码:
Max
答案 0 :(得分:2)
Illuminate\Database\Eloquent\Model::on()
方法返回一个Illuminate\Database\Eloquent\Builder
类的实例,该实例没有名为save()
的任何方法。因此,您必须执行以下操作才能使其正常工作:
$logs = CustomerAgentCodeUpdateLog::on($this->connection);
$logs->create([
'customer_id' => $targetedCustomer->customer_id;
'old_agent_code' => $targetedCustomer->agent_code;
'new_agent_code' => $user->agent;
'type' => 1;
]);
您不需要致电save()
。
答案 1 :(得分:0)
尝试此方法:
$targetedCustomer = Customer::on($this->connection)
->where('fusercode', $user->loginname)
->first();
if($targetedCustomer and $targetedCustomer->agent_code != $user->agent)
{
// Set agent code
$targetedCustomer->agent_code = $user->agent;
$targetedCustomer->save();
// New log
$logs = (new CustomerAgentCodeUpdateLog)->on($this->connection); // with `new`
$logs->customer_id = $targetedCustomer->customer_id;
$logs->old_agent_code = $targetedCustomer->agent_code;
$logs->new_agent_code = $user->agent;
$logs->type = 1;
$logs->save();
}