3 months ago

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')
  2. Use withTrashed() with after relation while calling like below -

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')
$assetDetails = $assetDetails->with(['safetyCheckListSaveInfo' => function ($query){
$query->select('id', 'assetId', 'userId', 'type','checks' , 'coordinate', 'created_at','created_at')->orderBy('created_at', 'desc');

How can i use soft delete with bunch of realtions ?

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