Home > Net >  How to add validate data search
How to add validate data search

Time:03-30

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,

CodePudding user response:

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
}

CodePudding user response:

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
}
  • Related