插入输入数据并比较数据库中是否可用数据

时间:2019-06-03 11:32:31

标签: php laravel-5

插入输入数据并比较数据库中是否可用数据

$health         = $request->input('health');
$tran           = $request->input('transportation');
$smoke          = $request->input('smoke');
$trial =   Trial::select(array('health' => $health, 'transportation' => $tran,'smoke' => $smoke));
//dd($trial);
if(!isset($trial)){
    return view('home');
}   else{
    return view('patient-home.trialresult',compact('health','tran','smoke'));
}

4 个答案:

答案 0 :(得分:1)

public function trialquestion(QuestionRequest $request)
    {
        //Validate Trial Question
        $validatedData = $request->validated();
        \App\Question::create($validatedData);

        $health = $request->input('health');
        $tran   = $request->input('transportation');
        $smoke  = $request->input('smoke');
        $trial  = Trial::where(array('health' => $health, 'transportation' => $tran, 'smoke' => $smoke))->get();
        return view('patient-home.trialresult', compact('trial', $trial));
    } 

答案 1 :(得分:0)

查询中的某些更改

$trial =   Trial::where(array('health' => $health, 'transportation' => $tran,'smoke' => $smoke))->get();

应该工作。

where替换为select,然后在末尾加上->get()

答案 2 :(得分:0)

select替换为where,并在末尾附加->get()

$trial = Trial::where([
    ['health', '=', $health],
    ['transportation', '=', $tran,
    ['smoke', '=', $smoke]
])->get();

OR

$trial = Trial::where('health', $health)
    ->where('transportation', $tran)
    ->where('smoke', $smoke)
    ->get();

注意::如果要将数组传递到该数组,则必须是2D数组,否则您可以使用where的流利链接

来自laravel doc 5.8

答案 3 :(得分:0)

尝试一下:

$trialExists = Trial::where('health', $health) ->where('transportation', $tran) ->where('smoke', $smoke) ->exists();

然后检查它是否存在:

if($trialExists){ return view('home'); } else{ return view('patient-home.trialresult',compact('health','tran','smoke')); }

这是一种更清洁的方法,因为您只想检查它的存在。