如何使用Laravel实现关联模型的关联新增与更新操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计597个文字,预计阅读时间需要3分钟。
网上找到了Laravel相关的关联新增和更新文档,但内容大多不满足需求。下面根据自己代码整理出关联操作方法,并按照Laravel文档中的说明设置关联模型。
具体操作如下:
1. 根据Laravel文档,设置关联模型。
phppublic function orders(){ return $this->belongsToMany(Order::class);}
2. 在创建或更新关联关系时,使用`attach`和`sync`方法。
- `attach`方法添加新的关联,不会删除原有的关联。
php$user->orders()->attach($order);
- `sync`方法同时添加新的关联,并删除不存在的关联。
php$user->orders()->sync($orders);
3. 查询关联数据时,使用`with`方法加载关联数据。
php$user->load('orders');
4. 获取关联模型中对应的数据时,使用`orders`属性。
php$orders=$user->orders;
5. 更新关联模型时,先删除原有的关联,然后添加新的关联。
php$user->orders()->syncWithoutDetaching($newOrders);
以上是Laravel中关联操作的基本方法,具体实现还需根据项目需求进行调整。希望对你有所帮助!
网上找了 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 关联模型-关联新增和关联更新的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计597个文字,预计阅读时间需要3分钟。
网上找到了Laravel相关的关联新增和更新文档,但内容大多不满足需求。下面根据自己代码整理出关联操作方法,并按照Laravel文档中的说明设置关联模型。
具体操作如下:
1. 根据Laravel文档,设置关联模型。
phppublic function orders(){ return $this->belongsToMany(Order::class);}
2. 在创建或更新关联关系时,使用`attach`和`sync`方法。
- `attach`方法添加新的关联,不会删除原有的关联。
php$user->orders()->attach($order);
- `sync`方法同时添加新的关联,并删除不存在的关联。
php$user->orders()->sync($orders);
3. 查询关联数据时,使用`with`方法加载关联数据。
php$user->load('orders');
4. 获取关联模型中对应的数据时,使用`orders`属性。
php$orders=$user->orders;
5. 更新关联模型时,先删除原有的关联,然后添加新的关联。
php$user->orders()->syncWithoutDetaching($newOrders);
以上是Laravel中关联操作的基本方法,具体实现还需根据项目需求进行调整。希望对你有所帮助!
网上找了 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 关联模型-关联新增和关联更新的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

