简体   繁体   English

Laravel 5.4如何获取每月新申请总数

[英]Laravel 5.4 how can I get the sum of the total new applications per month

I have in my application that I will get all the reports of total new application per month. 我的应用程序中有我每月将获得全部新应用程序的所有报告。

Here is my applications table structure: 这是我的应用程序表结构:

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();
    });
}

and here is the code in my controller: 这是我的控制器中的代码:

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]);
    }

There is no errors or problem here. 这里没有错误或问题。 I just don't know where to start because I am new in laravel especially in backend using model and controller. 我只是不知道从哪里开始,因为我是laravel的新手,尤其是在使用模型和控制器的后端。 So I am looking for help. 因此,我正在寻求帮助。 Thanks in advance. 提前致谢。

Get a datefrom and dateto either from your blade or somewhere else, depends on you. 从刀片或其他位置获取datefromdateto取决于您。

But in this example, it would be from the blade's select tag with a name as period : 但在此示例中,它将来自刀片的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>

So it will decide how it will show the reports, whether it'll be Daily, Weekly, Monthly, and Yearly. 因此,它将决定如何显示报告,它是“每日”,“每周”,“每月”和“每年”。

Inside the controller, I set an example: 在控制器内部,我设置了一个示例:

            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();   
            }

And for the query, it would be something like this: 对于查询,将是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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