Laravel 5.1 更新数据透视表列而不删除现有记录

标签 laravel

我正在使用 Laravel 5.1

我有两个表,即具有多对多关系的usersmedicines。相关型号如下:

class User extends BaseModel  {

    use SoftDeletes;
    protected $dates = ['deleted_at'];

    protected $table = 'users';

    public function medicines(){
        return $this->belongsToMany('App\Models\Medicine')->withPivot('id', 'time','config' ,'start','end' );
    }
}

class Medicine extends BaseModel{

    use SoftDeletes;
    protected $dates = ['deleted_at'];

    protected $table = 'medicines';

    public function users(){
        return $this->belongsToMany('App\Models\User')->withPivot('id', 'time','config' ,'start','end'  );
    }
}

多对多关系表user_medicine具有一些额外的数据透视列,如下所示:

id(PK) user_id(FK users) medicine_id(FK medicines) time   config        start       end 
1      1                 41                        09:00  {dispense:2}  2015-12-01  2015-12-25
2      1                 43                        10:00  {dispense:1}  2015-12-10  2015-12-22
3      1                 44                        17:00  NULL          2015-12-10  2015-12-31

现在,我想更改特定药物的时间。我编写的代码如下:

public function updateMedicationTime($fields){

    $result = array();

    try {
        $user = User::find($fields['user_id']); 

        $medicines = $user->medicines()->where('medicines.id',$fields['medicine_id'])->first();

        if (!empty($medicines)){
            $medicine_times = json_decode($medicines->pivot->time);

            foreach ($medicine_times as $key=>$medicine_time ){

                if ($medicine_time->time == $fields['oldtime']){

                    $medicine_time->time = $fields['newtime'];
                }
            }

            // Update the time column of provided medicine id
            $user->medicines()->where('medicines.id',$fields['medicine_id'])->sync(array($medicines->id , array("time"=>json_encode($medicine_times))), false);

            // I also have tried 

            // $medicines->sync(array($medicines->id , array("time"=>json_encode($medicine_times))), false); OR 
            // $medicines->pivot->sync(array($medicines->id , array("time"=>json_encode($medicine_times))), false);

        }

    }
    catch(Exception $e ){
        throw $e;
    }

    return $result;
}

但它不是更新现有多对多表的时间列,而是插入一条包含药物的新记录 id = 1 并提供 timenull 其他列。谁能建议我在哪里犯了错误?

最佳答案

您可以使用名为 updateExistingPivot 的方法:

$user = User::find($id);
$user->medicines()->updateExistingPivot($medicine_id, ['time' => $time], false);

最后一个参数指定是否应“触及”父表,这意味着更新 updated_at 字段的时间戳。

关于Laravel 5.1 更新数据透视表列而不删除现有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34307871/

相关文章:

php - Laravel 中的四(或五)向表关系

javascript - VueJs 路由器链接在实时服务器上不起作用

laravel - 在 Laravel Elixir 5.2 上调用任务之前检查文件是否存在

javascript - Ajax调用url最大长度

laravel - 结合 Laravel 和 Create-React-App

php - ReflectionException - 中间件类不存在 Laravel 5.5

laravel - 如何在 Laravel 中转发电子邮件?

javascript - 与 Laravel 分页一起使用的表格搜索

php - Laravel 使用 array_merge 进行分页

php - 高分辨率图像上传失败 stream_copy_to_stream() : read of 8192 bytes