Laravel save()显示未定义的方法

时间:2018-11-12 08:46:57

标签: php laravel laravel-5

我是Laravel的新手,目前正面临一个问题。

我正在尝试将记录插入另一个数据库,但是当我尝试保存它时,它会引发以下错误:

  

调用未定义的方法Illuminate \ Database \ Query \ Builder :: save()

这是我的代码:

Max

2 个答案:

答案 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();
}