简体   繁体   中英

How to add validate data search

I am using laravel for this project, i'm newbie at laravel then i want to add validate data if there is true then go to pdf blade, unless the data is false or wrong (i don't know what is it call so i named it True and False, but i hope you understand what i mean)

there is code in controller method search pdf

$id = $request->id;
    $date = $request->date;

    $pegawai = DB::table('pegawais')
            ->where('id', $id)
            ->whereDate('date', $date)
            ->get();

    $pdf = PDF::loadview('pegawai_pdf', [
        'pegawai'=>$pegawai
    ]);

    return $pdf->stream();

and this is the output blade when i searched the data is true or exist here

and this is the output blade when i searched but the data is false or not found data exist

here

fyi the data are fake data from seeder,

From your example, I will assume that you want to check if there is a result or not in $pegawai right? Then just count it.

if (count($pegawai) > 0) {
    // show your pdf output here
} else {
    // there is no data, do something here
}

to check this does not need to be a true false variable you can check this like this

if($pegawai){
 $pdf = PDF::loadview('pegawai_pdf', [
        'pegawai'=>$pegawai
    ]);

    return $pdf->stream();
}else{
    //show error here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM