如何在一个Laravel关联模型中同时实现关联新增和关联更新的长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计561个文字,预计阅读时间需要3分钟。
在网上查找了Laravel相关的关联新增和更新文档,发现大部分内容都是直接抄袭文档,缺乏理解。下面整理一下自己在代码中实现的关联操作方法,并按照Laravel文档中的说明进行设置。
1. 关联操作方法: - 添加关联:$model->relation()->attach($relatedId); - 删除关联:$model->relation()->detach($relatedId); - 批量添加关联:$model->relation()->syncWithoutDetaching($ids); - 替换关联:$model->relation()->sync($ids);
2. 关联模型设置: - 在模型中使用`belongsToMany()`或`hasMany()`方法定义关联。 - 使用`withPivot()`方法指定关联的中间表字段。 - 使用`through()`方法指定关联的中间表模型。
3. 参考地址: - [Laravel文档 - 关联](https://laravel.com/docs/eloquent-relationships#one-to-many)
php// 示例代码class Post extends Model{ public function tags() { return $this->belongsToMany(Tag::class, 'post_tags', 'post_id', 'tag_id'); }}
// 添加关联$post->tags()->attach($tagId);
// 删除关联$post->tags()->detach($tagId);
// 批量添加关联$post->tags()->syncWithoutDetaching([$tagId1, $tagId2]);
// 替换关联$post->tags()->sync([$tagId1, $tagId2]);
网上找了 Laravel 相关的关联新增和关联更新文档,写的都不是很满意。(基本都在抄文档)下面整理下自己代码中的关联操作方法
按照 Laravel 文档中的说明设置关联模型 参考地址
//病人模型 class Patient extends Model { /** * 病人附表 * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function patientdata () { return $this->hasOne(PatientData::class); } //病人附表模型 class PatientData extends Model { public function patient() { return $this->belongsTo(Patient::class); }
关联更新代码
/** * 新增病人信息 * @param array $data * * @return bool */ public function savePatient($data=[]) { DB::beginTransaction(); if($patient = $this->create($data)){ if ($res = $patient->patientdata()->create(["数据"])){ DB::commit(); } else{ DB::rollBack(); } return true; } return false; }
关联更新代码
public function updatePatient($data=[]) { DB::beginTransaction(); //先通过主键获得病人模型的实例 $patient = $this->find($data['id']); if($patient->update($data)){ if ($res = $patient->patientdata()->where('patient_id',$data['id'])->update(["数据"])){ DB::commit(); } else{ DB::rollBack(); } return true; } return false; }
以上这篇Laravel 关联模型-关联新增和关联更新的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计561个文字,预计阅读时间需要3分钟。
在网上查找了Laravel相关的关联新增和更新文档,发现大部分内容都是直接抄袭文档,缺乏理解。下面整理一下自己在代码中实现的关联操作方法,并按照Laravel文档中的说明进行设置。
1. 关联操作方法: - 添加关联:$model->relation()->attach($relatedId); - 删除关联:$model->relation()->detach($relatedId); - 批量添加关联:$model->relation()->syncWithoutDetaching($ids); - 替换关联:$model->relation()->sync($ids);
2. 关联模型设置: - 在模型中使用`belongsToMany()`或`hasMany()`方法定义关联。 - 使用`withPivot()`方法指定关联的中间表字段。 - 使用`through()`方法指定关联的中间表模型。
3. 参考地址: - [Laravel文档 - 关联](https://laravel.com/docs/eloquent-relationships#one-to-many)
php// 示例代码class Post extends Model{ public function tags() { return $this->belongsToMany(Tag::class, 'post_tags', 'post_id', 'tag_id'); }}
// 添加关联$post->tags()->attach($tagId);
// 删除关联$post->tags()->detach($tagId);
// 批量添加关联$post->tags()->syncWithoutDetaching([$tagId1, $tagId2]);
// 替换关联$post->tags()->sync([$tagId1, $tagId2]);
网上找了 Laravel 相关的关联新增和关联更新文档,写的都不是很满意。(基本都在抄文档)下面整理下自己代码中的关联操作方法
按照 Laravel 文档中的说明设置关联模型 参考地址
//病人模型 class Patient extends Model { /** * 病人附表 * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function patientdata () { return $this->hasOne(PatientData::class); } //病人附表模型 class PatientData extends Model { public function patient() { return $this->belongsTo(Patient::class); }
关联更新代码
/** * 新增病人信息 * @param array $data * * @return bool */ public function savePatient($data=[]) { DB::beginTransaction(); if($patient = $this->create($data)){ if ($res = $patient->patientdata()->create(["数据"])){ DB::commit(); } else{ DB::rollBack(); } return true; } return false; }
关联更新代码
public function updatePatient($data=[]) { DB::beginTransaction(); //先通过主键获得病人模型的实例 $patient = $this->find($data['id']); if($patient->update($data)){ if ($res = $patient->patientdata()->where('patient_id',$data['id'])->update(["数据"])){ DB::commit(); } else{ DB::rollBack(); } return true; } return false; }
以上这篇Laravel 关联模型-关联新增和关联更新的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

