我有一个名为“详细信息”的视图,它根据搜索条件从控制器的调用方法获取数据。我在详细信息视图中有一个打印按钮,当我按下它时,我想将搜索到的数据传递到打印视图,如下所示:
class ProductSerialNoWiseLedgerController extends Controller
{
public function __invoke()
{
$searchParameters = request()->only([
'machineries_ID',
'hospital_ID',
'project_ID',
'batchNo',
'unit_ID',
'DR',
'CR',
'Balance',
]);
$query = Hospital_Stocks_SerialNoWise::with('hospitals', 'project', 'unit', 'machinery')
->orderBy('id');
// Apply search parameters
foreach ($searchParameters as $field => $value) {
if ($value !== null) {
if ($field === 'machineries_ID') {
$query->whereHas('machinery', function ($subQuery) use ($value) {
$subQuery->where('en_name', 'like', '%' . $value . '%');
});
} elseif ($field === 'hospital_ID') {
$query->whereHas('hospitals', function ($subQuery) use ($value) {
$subQuery->where('en_name', 'like', '%' . $value . '%');
});
} elseif ($field === 'project_ID') {
$query->whereHas('project', function ($subQuery) use ($value) {
$subQuery->where('en_name', 'like', '%' . $value . '%');
});
} elseif ($field === 'unit_ID') {
$query->whereHas('unit', function ($subQuery) use ($value) {
$subQuery->where('en_name', 'like', '%' . $value . '%');
});
} else {
$query->where('hospital__stocks__serial_no_wises.' . $field, 'like', '%' . $value . '%');
}
}
}
if (request()->has('print')) {
$properties = $query->get();
return view('Details.Stocks.printProductSNWLedger')->with(['data' => $properties]);
}
if (isset(request()->per_page)) {
$perPage = in_array(request('per_page'), getPerPageOptions()) ? \request('per_page') : 10;
$properties = $query->paginate($perPage);
} else {
$properties = $query->get();
}
return response()->json(['data' => $properties]);
}
}
对于详细信息视图,搜索工作正常,但当单击打印按钮时,它会将所有数据传递到打印视图,而不是搜索到的数据。 这是我的路线:
Route::get('printProductSNWLedger', [\App\Http\Controllers\Details\Stocks\ProductSerialNoWiseLedgerController::class, '__invoke']);
这是我前面的打印功能:
const handlePrint = () => {
window.open('printProductSNWLedger?print=1', '_blank');
};
使用打印按钮请求路线时,您没有传递搜索参数。例如你在 js 中的打印部分应该是这样的:
const handlePrint = () => {
window.open('printProductSNWLedger?print=1&machineries_ID=227', '_blank');
};
在上面的代码中,您会看到我已在查询字符串中包含
machineries_ID=227
,以便应用它