找到你要的答案

Q:Laravel query delete relationships

Q:laravel查询删除关系

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

我的模式是病人>;样品,我删除了一个病人,我的病人withtrashed()查询删除,但不删除患者的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;
}

但在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;
}

但找不到删除的病人,所以我的样本没有得到病人的信息

answer1: 回答1:

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;
}

如果我正确地理解你的问题,你想包括了患者在你吗?如果是这样,那么尝试以下。

public function query(Request $request){

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

  return $result;
}
php  laravel  laravel-5.1