PHP model层查询数据库 封装X轴、Y轴 ,想知道应该怎么写是正确的
这是自己写的model层
<?php
namespace App\Model\model;
use DB;
class modelData{
public function change($type, $date) {
if(!empty($type) && !empty($action) && !empty($plane) && empty($instrument)){
$X_data = ["TK5-01","TK5-02","TK5-03","TK5-04","TK5-05"];
foreach ($X_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$Y_data[] = $result->variation;
}
//var_dump($Y_data);die;
$A_data = ["TK5-06","TK5-07","TK5-08","TK5-09","TK5-10"];
foreach ($A_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$B_data[] = $result->variation;
}
$E_data = ["TK5-11","TK5-12","TK5-13","TK5-14"];
foreach ($E_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$F_data[] = $result->variation;
}
$G_data = ["TK5-15","TK5-16"];
foreach ($G_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$H_data[] = $result->variation;
}
$I_data = ["TK5-17","TK5-18"];
foreach ($I_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$J_data[] = $result->variation;
}
$K_data = ["TK5-19"];
foreach ($K_data as $key => $value) {
$result = DB::table('instruments_tuokong')->select('variation')->orderBy('create_time', 'desc')->where('code', $value)->first();
$L_data[] = $result->variation;
}
}
}
}
?>
参考GPT和自己的思路:根据你提供的代码,我发现其中一些变量命名不规范,也没有进行注释说明清楚,不过总体来说,该方法是从数据库中查询数据,并将X轴和Y轴的数据分别封装在$X_data和$Y_data数组中。如果你的目的是实现封装X轴、Y轴的功能,该代码已经完成了该功能。如果你想进一步优化代码,可以考虑修改变量名和加上注释,以提高代码的可读性。