我的模型關係是一對多例如:PatientToSampleLaravel 5.1刪除關係
Patient_Model:
class Patient_Model extends Model implements Jsonable{
use SoftDeletes;
protected $table = 'patients';
public function samples(){
return $this->hasMany('App\Models\Sample_Model','code','patient_id');
}
}
Sample_Model:
class Sample_Model extends Model{
use SoftDeletes;
protected $table = 'samples';
public function patient(){
return $this->belongsTo('App\Models\Patient_Model','patient_id','code');
}
}
我想使用的功能刪除病人與樣品的
public function delete(Request $request){
$patient = Patient_Model::withTrashed()
->where("id",$request->get("id"))
->delete();
return json_encode($patient);
}
但現在只能刪除病人....
看看這個:http://stackoverflow.com/a/15019260/2772319的[自動刪除相關行 – Digitlimit
可能重複在Laravel(Eloquent ORM)](http://stackoverflow.com/questions/14174070/automatically-deleting-related-rows-in-laravel-eloquent-orm) – Digitlimit