【问题标题】:How to display unchecked data that is not stored in database used laravel?如何显示未存储在使用 laravel 的数据库中的未检查数据?
【发布时间】:2022-01-03 21:17:54
【问题描述】:

我有 2 个表,即 Detail Score Table 和 Criteria Table。 我使用检查输入表进行评估。在评估输入表中,有17个标准,用户可以通过检查清单进行评估。

我的评分代码和输入表单:

public function addScore(Request $request)
    {
        if ($request->floor_id == 0) {
            return redirect('pengawas/dashboard')->with('delete','Gagal , Pastikan Gedung dan Lantai Area Kerja Telah Diisi!');
        }

        //dd($totalscore);
        $score_acc = DB::table('scores')
                     ->where('cleaningservices_id','=', $request->input('cleaningservices_id'))
                     ->where(DB::raw("(DATE_FORMAT(score_dt, '%m'))"),'=', date('m', strtotime(now())))
                     ->sum('total_score');

        $pengawasid = Auth::id();
        $score      = new ScoreModel();
        $totalscore = array_sum($request->input('score'));
        $totalscoreacc = $score_acc + $totalscore;
        $score->user_id                     = $pengawasid;
        $score->cleaningservices_id         = $request->input('cleaningservices_id');
        $score->floor_id                    = $request->input('floor_id');
        $score->total_score                 = $totalscore;
        $score->total_score_accumulation    = $totalscoreacc;
        $score->status_score                = "Bersih";
        $score->save();


        $getscoreid = DB::table('scores')->select('id')->orderBy('id', 'DESC')->first();
        //dd($getscoreid);

        //dd($request->score);
        $criteria_ids = $request->criteria_id;
        $scores = $request->score;

        foreach ($scores as $i => $score) {
             $score_detail[] = [
                'score_id'      => $getscoreid->id,
                'criteria_id'   => $request->criteria_id[$i],
                'score'         => $request->score[$score],
                'description'   => $request->description[$i]
             ];
          }

        DetailScoreModel::insert($score_detail);
        return redirect('pengawas/show_scoring')->with('success','Berhasil memberikan penilaian');
    }

我只将得分 = 1 的检查标准保存到标准表中。之前我做过,如果选中的标准值为1,但如果不选中,则得分= 0。但是如果我使用这种方法,每次用户进行评估时,都会添加17个新行。而且非常消耗存储空间。因此,我只将选中的评分存储到标准详细信息表中。

我的显示细节评分代码:

(数据库)

(控制器)

public function getDetailScorePegawai($id)
        {
            $score     = DB::table('scores')
                        ->select('cleaningservices.id as nip','scores.id as id_score',
                            'scores.*','cleaningservices.*', 'buildings.*','floors.*')
                        ->join('cleaningservices','cleaningservices.id','=','scores.cleaningservices_id')
                        ->join('floors','floors.id','=','scores.floor_id')
                        ->join('buildings','buildings.id','=','floors.building_id')
                        ->where('scores.id','=',$id)
                        ->get();
            $building  = DB::table('buildings')->get();
            $detail_sp = DB::table('detailscores')
                         ->select('detailscores.criteria_id as id_criteria','criterias.*','scores.*','detailscores.*')
                         ->join('criterias','criterias.id','=','detailscores.criteria_id')
                         ->join('scores','scores.id','=','detailscores.score_id')
                         ->where('scores.id','=',$id)
                         ->get();
                         
            return view('pengawas.detail_scorepegawai', compact('score','building','detail_sp','criteria'));
    
        }

(查看)

@foreach($detail_sp as $row)
                              <div class="row">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <input type="hidden" name="criteria_id[]" value="{{ $row->id_criteria }}">
                                      {{ $row->criteria_name}} : 
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                    <input type="checkbox" class="form-control" name="score[{{ $row->id_criteria }}]" id="score" value="1"<?php if($row->score == "1") echo "checked"; ?>>
                                  </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group input-group mb-3">
                                        <input type="text" class="form-control" name="description[]" value="{{ $row->description }}">
                                        <div class="input-group-append">
                                                        <div class="input-group-text">
                                                        <span class="fas fa-file-medical"></span>
                                                        </div>
                                                        </div>
                                    </div>
                                </div>
                              </div>
                              @endforeach

当我为详细分数创建视图时,我目前遇到了问题。我想显示详细的分数,如果 score = 1,则选中,如果 score 未存储在数据库中或 0 则未选中。

我想要的结果是这样的,这是我将分数保存在数据库中时的结果,分数= 0未选中

我需要你的帮助。谢谢

【问题讨论】:

  • 1) 您的问题中没有代码,因此可能的答案必须假设您的数据库结构、php 代码和 html 布局有很多事情。 2)你真的应该把你的问题集中在你到底在挣扎什么上!您是否难以从 mysql 中获取数据,或者您是否正在寻找有关数据存储的建议?如果不是,则 mysql 标签不相关。您在遍历选项数组并将它们与从 mysql 检索的数组进行比较时遇到问题吗?生成 html 代码有问题吗?
  • 抱歉,我已经更新了我的问题,当我显示详细分数时出现问题。
  • 试试{ echo "checked=checked"; }}
  • 您在标准表上是否有类似标准的描述?在这种情况下,您应该从那里提取数据以呈现您的视图。
  • 您想在视图中显示的信息必须在某个地方。如果它不在表格中,则必须对其进行硬编码

标签: arrays laravel checkbox


【解决方案1】:

你为什么不使用枚举来存储所有这些可能只有一行和一列的字段数据来一次存储所有这些数据。

这是一种更好的方式,可以节省大量流程和工作量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多