SinghWithLaravel
3 months ago
438
1
Laravel

How to get soft deleted records from eager loading and lazy eager loading ?

Posted 3 months ago by SinghWithLaravel

I want get deleted records eager & lazy eager loading relations. How can i achieve that ? I found some solutions on stackoverflow, which have been written below -

  1. use withTrashed() method wih relation function like below -
     public function deletedVideos() {
           return $this->hasMany(Video::class, 'deleted_by_user_id')
            ->withTrashed();
          }
        
    
  2. Use withTrashed() with after relation while calling like below -
Model::find($id)->post()->withTrashed()->get();

I want to know the solution in case, if i have many relations, what can i do ? have a look at below situation -

$assetDetails = $assetDetails->with(['assetDocuments' => function ($query) {
$query->select('id', 'assetId','assetUsage','volume','amount', 'documentType', 'links', 'link','userId', 'uploadTime', 'expiryDate','coordinate')
->orderBy('assetdocument.uploadTime', 'desc')
->get();
},'assetDocuments.userDetails:id,firstName,lastName,middleName']);
$assetDetails = $assetDetails->with(['safetyCheckListSaveInfo' => function ($query){
$query->select('id', 'assetId', 'userId', 'type','checks' , 'coordinate', 'created_at','created_at')->orderBy('created_at', 'desc');
},
'safetyCheckListSaveInfo.userDetails:id,firstName,middleName,lastName']);

How can i use soft delete with bunch of realtions ?

Please sign in or create an account to participate in this conversation.