即使我在数据库Laravel 5中有数据,我的模型总是空的

时间:2017-04-19 13:03:36

标签: laravel laravel-5

UPDATE 我刚刚在这里发现了问题,这是在$ instID = $ request-> insititution; 它应该是$ instID = $ request->机构; 谢谢你的帮助......

更新

我尝试插入CRConfigDetail Model / table,但首先我需要从CRConfig Model / Table获取具有特定规则的CRConfigID。所以我可以获得CRConfigID并将其放到CRConfigDetail列。

但每次我尝试检索时,它总是会提供空数据,即使我已经在我的数据库中有数据。在其他控制器中,我可以使用类似的规则检索数据。

我做错了逻辑吗?因为我没有看到任何错误。

这是我的HTML /表单:

<form action="doInsertSCC" method="POST" enctype="multipart/form-data" id="scheduleDetailForm">
                {{csrf_field()}}
                <div class="form-group">
                    <label for="institution">Institution</label>
                    <select name="institution" class="form-control" id="institution">
                    </select>
                </div>
                <div class="form-group">
                    <label for="acadCareer">Academic Career</label>
                    <select name="acadCareer" class="form-control" id="acadCareer">
                    </select>
                </div>
                <div class="form-group">
                    <label for="period">Period</label>
                    <select name="period" class="form-control" id="period">
                    </select>
                </div>
                <div class="form-group">
                    <label for="department">Department</label>
                    <select name="department" class="form-control" id="department">
                    </select>
                </div>
                <div class="form-group">
                    <label for="fos">Field of Study</label>
                    <select name="fos" class="form-control" id="fos">
                    </select>
                </div>
                <div class="form-group">
                    <label for="scc">Lecturer's ID - Name</label>
                    <select name="scc" class="form-control" id="scc">
                    </select>
                </div>
                <button type="submit" class="btn btn-default">Assign SCC</button>
                <div id="search" class="btn btn-default">Search</div>
            </form>

这是我访问控制器的路线

Route::post('/doInsertSCC', "ScheduleController@insertSCC");

这是我的ScheduleController

public function insertSCC(Request $request){
        $this->validate($request, [
            'scc' => 'required'
        ]);

        $instID = $request->insititution;
        $acadID = $request->acadCareer;
        $termID = $request->period;
        $depID = $request->department;
        $rule = ['institutionID' => $instID, 'acadCareerID' => $acadID, 'termID' => $termID, 'departmentID' => $depID];
        $crConfig = CRConfig::where($rule)->first();

        if( !empty($crConfig) ){
            foreach ($crConfig as $cr) {
                $crConfigID = $cr->CRConfigID;
            }
            $schedule = new CRConfigDetail;
            $schedule->status = 'Pending';
            $schedule->numRevision = 0;
            $schedule->FOSID = $request->fos;
            $schedule->SCC = $request->scc;
            $schedule->CRConfigID = $crConfigID;
            $schedule->save();
            return redirect("AssignSCC")->with('status', 'Add SCC success!');
        }else{
            return redirect("AssignSCC")->with('status', 'Add schedule first!');
        }


    }

我已经检查过我的规则数据与我的CRConfig表的数据匹配(使用console.log()

每次我提交此表单时,我都会执行“其他”并重定向并“首先添加计划表”!消息。

2 个答案:

答案 0 :(得分:1)

实际上,它接受一个数组,但数组应该这样格式化..

cv2

来源:https://laravel.com/docs/5.4/queries#where-clauses

答案 1 :(得分:-2)

使用以下

snapshot.value

而不是

$crConfig = CRConfig::where('institutionID' , $instID)
                      ->where('acadCareerID' , $acadID)
                      ->where('termID', $termID)
                      ->where('departmentID', $depID)
                      ->first();

您可以通过获取查询日志来检查在后端构建的查询,为此您必须在构建查询之前启用查询日志,并在构建查询后获取查询日志两个查询日志方法都属于DB facade

 $rule = ['institutionID' => $instID, 'acadCareerID' => $acadID, 'termID' => $termID, 'departmentID' => $depID];
 $crConfig = CRConfig::where($rule)->first();