说明:在laravel中需要合并的两个模型各自关联的模型不同数据无法加载以及可以加载但是出现数据错乱的问题如何解决?
1.前一个模型和后一个模型有相同的关联模型,但是前一个模型的关联模型的数据会覆盖到后一个模型的关联模型中
解决方法尝试
1.先合并模型再加载关联模型直接使用失败,需要自己对合并后的数据进行遍历处理
2.对模型进行按需加载同样会被覆盖
3.使用collection 并且保留分页功能
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\ {Sell,Sre,Borrow,Lend,Record,User,SellInfo,SreInfo,BorrowInfo,LendInfo,Relation,Cost,Borr owRestore};
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use App\Notifications\Message;
use App\Http\Controllers\Summary;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Validate\{BorrowAndBorrowRestore as Validate /*as BorrowValidate, BorrowInfo as BorrowInfoValidate,Cost as CostValidate,Lend as LendValidate,LendInfo as LendInfoValidate,Sell as SellValidate,SellInfo as SellInfoValidate,Sre as SreValidate,SreInfo as SreInfoValidate,Order as OrderValidate*/};
class BorrowAndBorrowRestore extends Controller{
//数据记录
public function record(Request $request,Validate $validate,Query $query){
/**************************************************************************************** /
//对于不同关联的模型,在合并查询结果之后再次使用 `with` 方法加载额外的关联模型数据可能会导致数据错乱的情况。这是因为在使用 `with` 方法加载关联数据时,模型会尝试匹配当前模型的关联关系,而合并后的模型可能并不是符合原有关联关系的结构,导致加载的关联数据不正确。
//为了解决这个问题,可以考虑使用手动加载关联数据的方式来避免数据错乱的情况。具体实现如下:
// ```php
// use App\Models\A;
// use App\Models\B;
// use App\Models\C;
// use App\Models\D;
$input=$request->input();
// 加载 A 模型的数据和关联模型 C 的数据
$modelsA = Borrow::with(['userData','recordData','customerData','borrowInfoData','borrowRestoreData'])->select('id','number','state','time','amount','total','user','examine','type','data','customer','borrow_nums','borrow_restore_nums',DB::raw("-1 as nums"))->orderBy('state','asc')->orderBy('id','desc')->paginate($input['size']);
// 加载 B 模型的数据和关联模型 D 的数据
$modelsB = BorrowRestore::with(['userData','recordData','customerData','borrowInfoData','borrowRestoreInfoData','iceData'])->select('id','number','state','time','amount','total','user','examine','type','data','customer',DB::raw("-1 as borrow_nums"),DB::raw("-1 as borrow_restore_nums"),'nums')->orderBy('state','asc')->orderBy('id','desc')->paginate($input['size']);
// 手动加载关联模型数据
$mergedModels = $modelsA->merge($modelsB);
// dd($mergedModels);
$mergedModelsWithRelations = $mergedModels->map(function ($mergedModel) {
if ($mergedModel instanceof Borrow) {
$mergedModel->with(['borrowInfoData','borrowRestoreData']);
} elseif ($mergedModel instanceof BorrowRestore) {
$mergedModel->with('borrowInfoData','borrowRestoreInfoData','iceData');
}
return $mergedModel;
});
// dd($mergedModelsWithRelations);
Collection::macro('paginate', function ($perPage, $page = 1, $options = []) {
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$results = $this->slice(($page - 1) * $perPage, $perPage + 1);
return new LengthAwarePaginator($results, $this->count(), $perPage, $page, $options);
});
$paginatedCollection = $mergedModelsWithRelations->paginate($input['size']);
return $paginatedCollection;
}
}