php - 带警告消息的 Laravel DB 查询

标签 php mysql laravel

我想要的只是有一个超过缺席次数的 if 语句并显示消息。

这是 SQL 查询

public function countAbsent()
    {

        $absent = Attendances::select(DB::raw('count(status)'))
                 ->where('student_id', '=',  Auth::user()->id)
                 ->where('status', '=', 'absent')
                 ->where('section_name', 'like', Input::get('section_name'))
                 ->where('subject_code', 'like', Input::get('subject_code'))
                 ->count();

        return View::make('student.view_absent')
                 ->with('absent', $absent);


    }

最佳答案

您可以传递另一个变量来查看:

public function countAbsent()
{

    $absent = Attendances::select(DB::raw('count(status)'))
             ->where('student_id', '=',  Auth::user()->id)
             ->where('status', '=', 'absent')
             ->where('section_name', 'like', Input::get('section_name'))
             ->where('subject_code', 'like', Input::get('subject_code'))
             ->count();

    $absent_message = 'Students are not exceeding the absence threshold';

    $threshold = 10; //whatever you like

    if ($absent > $threshold)
    {
        $absent_message = 'Students are exceeding the absence threshold';
    }

    return View::make('student.view_absent')
             ->with('absent', $absent)
             ->with('absent_message', $absent_message);


}

并在 View student.view_absent 中回显 $absent_message

关于php - 带警告消息的 Laravel DB 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711850/

相关文章:

php - Symfony 5 - 同一页面上的多个表单

javascript - 如何修改 PHP/Jquery/Ajax 脚本以拥有多个表单字段

php - 奇怪的 PHP 字符串错误

mysql - 求和MYSQL中多行时间戳之间累积时间的最佳方法

php - Laravel - 不向表中的新列写入任何内容

PHP Laravel : How to Avoid duplicate data while importing xl/csv into mysql?

Laravel 将参数传递给路由/操作

php - cake php 3.x,模型连接 3 个表

php - 交响乐2 : How to concatenate multiple fields into one in a form

php - 使用 mysqli_* 连接时 aura/sqlquery 中的变量绑定(bind)