简体   繁体   中英

Laravel query delete relationships

My model is Patient -> Sample , I delete a patient, I query deleted patient by withTrashed() , But don't query sample for deleted patient by withTrashed();

Patient_Controller

class Patient_Controller extends Controller{

public function query(Request $request){

    $result = Patient_Model::withTrashed();
        ->orderBy("updated_at","desc")
        ->Paginate(15)
        ->toJson();

   return $result;
}

But in Sample_Controller

class Sample_Controller extends Controller{

public function query(Request $request){

    $result = Sample_Model::with('patient')
        ->withTrashed()
        ->orderBy("updated_at","desc")
        ->Paginate(15)
        ->toJson();

   return $result;
}

But with not find delete Patient, so My sample don't get patient info

If I understand your question correctly, you are trying to include trashed patients in your with? If so, then try the following.

public function query(Request $request){

  $result = Sample_Model::with(['patient' => function($q) {
        $q->withTrashed();
    }])
    ->withTrashed()
    ->orderBy("updated_at","desc")
    ->Paginate(15)
    ->toJson();

  return $result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM