Laravel 5.4如何获得每月新应用总数的总和

时间:2017-08-16 02:15:26

标签: php laravel laravel-5 laravel-5.4

我在我的申请表中将获得每月新申请总数的所有报告。

这是我的应用程序表结构:

public function up()
{
    Schema::create('applications', function (Blueprint $table) {
        //$table->increments('id');
        $table->string('application_number')->primary();
        $table->string('applicant_id');
        //$table->string('reference_number', 50);
        $table->string('business_name');
        $table->string('business_nature');
        $table->string('location_street');
        $table->string('location_building')->nullable();
        $table->string('location_barangay');
        $table->string('location_district');
        $table->string('landmarks')->nullable();
        $table->string('owner_fn');
        $table->string('owner_mn')->nullable();
        $table->string('owner_ln');
        $table->string('ar_fn');
        $table->string('ar_mn')->nullable();
        $table->string('ar_ln');
        $table->string('landline')->nullable();
        $table->string('cellphone_number')->nullable();
        $table->string('occupancy_type');
        //$table->string('cluster_no', 50);
        $table->string('land_area')->nullable();
        $table->string('no_floor')->nullable();
        $table->string('floor_area')->nullable();
        $table->string('no_employees')->nullable();
        $table->string('requirements_1')->nullable();
        $table->string('requirements_2')->nullable();
        $table->string('requirements_3')->nullable();
        $table->string('requirements_4')->nullable();
        $table->string('applicant_name');
        $table->string('status');
        $table->string('application_type');
        $table->date('expired_at');
        $table->timestamps();
        $table->softDeletes();
    });
}

这是我控制器中的代码:

public function index()
    {   
        $applications = DB::table('applications')->sum('created_at');
        $inspections = DB::table('for_inspections')->sum('created_at');
        $certifications = DB::table('certification')->sum('created_at');    

        $chart = Charts::multi('bar', 'material')
            // Setup the chart settings
            ->title("Monthly Report")
            // A dimension of 0 means it will take 100% of the space
            ->dimensions(0, 400) // Width x Height
            // This defines a preset of colors already done:)
            ->template("material")
            // You could always set them manually
            // ->colors(['#2196F3', '#F44336', '#FFC107'])
            // Setup the diferent datasets (this is a multi chart)
            ->dataset('New Applications', [5,20,100,0,0,0,0,0,0,0,0,0])
            ->dataset('Total Inspection', [15,30,80,0,0,0,0,0,0,0,0,0])
            ->dataset('Certified', [25,10,40,0,0,0,0,0,0,0,0,0])
            // Setup what the values mean
            ->labels(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']);
        return view('admin-dashboard.reports_home', ['chart' => $chart]);
    }

这里没有错误或问题。我只是不知道从哪里开始,因为我是laravel的新手,特别是在使用模型和控制器的后端。所以我正在寻求帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

从您的刀片或其他地方获取datefromdateto,取决于您。

但在此示例中,它将来自刀片的select标记,其名称为period

<select name="period" id="period" class="form-control">
    <option></option>
    <option>Daily</option>
    <option>Weekly</option>
    <option>Monthly</option>
    <option>Yearly</option>
</select>

因此,它将决定如何显示报告,无论是每日,每周,每月还是每年。

在控制器内部,我举了一个例子:

            if($period == 'Daily'){
                    $datefrom = Carbon::now()->startOfDay();
                    $dateto = Carbon::now()->endOfDay();
                }
            elseif ($period == 'Weekly') {
                $datefrom = Carbon::now()->startOfWeek();
                $dateto = Carbon::now()->endOfWeek();
            }
            elseif ($period == 'Monthly') {
                $datefrom = Carbon::now()->startOfMonth();
                $dateto = Carbon::now()->endOfMonth();
            }
            elseif ($period == 'Yearly'){
                $datefrom = Carbon::now()->startOfYear();
                $dateto = Carbon::now()->endOfYear();   
            }

对于查询,它将是这样的:

 $orders = DB::table('orders')
            ->whereDate('created_at','>=', $datefrom)
            ->whereDate('created_at', '<=', $dateto)
            ->get();