(Laravel)是否可以将下拉值直接传递到控制器方法中?

时间:2019-02-18 05:39:40

标签: laravel methods controller

我想要做的就是将一个下拉值直接传递给我的控制器方法。是否有任何可能的方法可以执行此操作,或者是否有任何文档或教程来执行此操作?顺便说一句,我正在使用Laravel 5.7。任何可以帮助我解决此问题的意见将不胜感激。提前致谢。

这是我的下拉菜单的代码。

<form  method="POST" id="year" name="year">
              <select name="year">
                    <option class="dropdown-item" selected name="years" disabled>--Select Year--</option>
                 <?php 
                    $start = "2017";
                    $end = (int)date("Y");                
                       for($i=$start; $i<=$end; $i++){                  
                          echo '<option class="dropdown-item" value='.$i.'>'.$i.'</option>';
                       }
                 ?>                          
             </select>                  
   </form>

这是我要传递下拉值的控制器中的方法。

public function resolution_time_of_calls(){         

        //declarations            
        $arr_val = array();
        $arr_val1 = array();
        $arr_val2 = array();
        $arr_monthname = array();
        $arr_monthInt = array();               

        //SELECTING MONTHS  
        $quer = DB::select("SELECT MONTH(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS MonthInt
            ,monthname(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS Month
            ,year(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS Year               
            FROM rtdb.Tickets T
            LEFT JOIN rtdb.ObjectCustomFieldValues O ON O.ObjectId=T.EffectiveId 
            AND O.CustomField=15 AND O.ObjectType='RT::Ticket' AND O.Disabled=0
            WHERE T.Status!='Deleted' AND T.IsMerged IS NULL AND T.Type='ticket'
            AND year(DATE_ADD(T.Created, INTERVAL 8 HOUR))= 2018
            GROUP BY Month,MonthInt,Year Order by MonthInt asc
            ");   
        $chart = new SampleChart;                            
        $chart->title('Resolution Time of Calls');                 
        foreach($quer as $query){
            array_push($arr_monthname, $query->Month.", ".$query->Year);  
            array_push($arr_monthInt, $query->MonthInt);   
        }                     
        $chart->labels($arr_monthname); 

        ///////////////////// TOTAL, AVERAGE,MAXIMUM Query  ////////////////////
        foreach($arr_monthInt as $month){
            $sql = DB::select("SELECT MONTH(DATE_ADD(T.Created, INTERVAL 8 HOUR)) AS MonthInt

                ,AVG(TIMESTAMPDIFF(HOUR,T.Created,O.Created)) AS AVG   
                ,MAX(TIMESTAMPDIFF(HOUR,T.Created,O.Created)) AS MAX               
                FROM rtdb.Tickets T
                LEFT JOIN rtdb.ObjectCustomFieldValues O ON O.ObjectId=T.EffectiveId 
                AND O.CustomField=15 AND O.ObjectType='RT::Ticket' AND O.Disabled=0
                WHERE T.Status!='Deleted' AND T.IsMerged IS NULL AND T.Type='ticket'
                AND year(DATE_ADD(T.Created, INTERVAL 8 HOUR))= 2018                    
                GROUP BY MonthInt Order by MonthInt asc");
        }
        foreach($sql as $value){

            array_push($arr_val1, $value->AVG );
            array_push($arr_val2, $value->MAX );
        }

        $chart->dataset('Average Hours', 'bar', $arr_val1)->backgroundcolor('#00833E');   
        $chart->dataset('Maximum Hours', 'bar', $arr_val2)->backgroundcolor('#AF1D2D');   
        $chart->height(600);


    return view('resolution_time_of_calls',['chart'=>$chart]);
} 

1 个答案:

答案 0 :(得分:1)

您可以使用Ajax实现此目的,因为您的表单中没有提交按钮。选择任何选项后(即发生更改事件时),ajax调用将被执行

$(document).on('change', '#year', function () {

    $.ajax({
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        url: YOUR_URL,
        type: 'POST',
        data: $("#year").serialize(),
        success: function (data) {
            console.log('Success==>', data);
        },
        error: function (data) {
            console.log('Error==>', data);
        }
    });
});   

在您的控制器中,您可以使用$request->all()进行检查。