如何在laravel中使用原始查询并在刀片页面中显示输出?

时间:2017-03-28 09:16:06

标签: php laravel-5

在我的控制器中,我使用此格式的原始查询

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;


use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\MonthlyActivity;

class MonthlyActivityController extends Controller
{
    //

    public function show()
    {
        //$monthly = MonthlyActivity::all();
    //  $var =MonthlyActivity::connection()->getPdo()->quote($var);
        $query = "select  mon,year,
        combo,
       registered,
       forwardedbycmo,
       clarification,
       noaction,
       disposed,mon_srno,undertaken
from monthly_activities
union
select extract('month' from actiondate) as mon,extract('year' from actiondate) as year,
        extract('year' from actiondate)|| '-' ||to_char(to_timestamp (extract('month' from actiondate)::text, 'MM'), 'TMMon') as combo,
       sum(case when actioncode = '00' then 1 else 0 end) as registered,
       sum(case when actioncode = '4T' and fromorg='CMOFF' then 1 else 0 end) as forwardedbycmo,
       sum(case when actioncode = '4D' and fromorg='CMOFF' then 1 else 0 end) as clarification,
       sum(case when actioncode = '10' then 1 else 0 end) as noaction,
       sum(case when actioncode = '50' then 1 else 0 end) as disposed,null as mon_srno,
        sum(case when actioncode = '40' and fromorg='CMOFF' then 1 else 0 end) as undertaken

from actionhistory where extract(month from actiondate)=extract(month from current_date)
and extract(year from actiondate)=extract(year from current_date)
 group by mon,year order by year,mon;";
        $result = MonthlyActivity::select(MonthlyActivity::raw($query));
        //print_r($result);

        return view('show',['monthly' => $result]);
    }
}

并在show.blade.php

<?php

                foreach($monthly as $mon)
                {
                        ?>
                        <tr>
                            <td>
                            <?php echo $mon->combo ?>
                            </td>
                            <td>
                            <?php echo $mon->year ?>
                            </td><td>
                            <?php echo $mon->id ?>
                            </td>
                            <td>
                            <?php echo $mon->id ?>
                            </td><td>
                            <?php echo $mon->id ?>
                            </td><td>
                            <?php echo $mon->id ?>
                            </td><td>
                            <?php echo $mon->id ?>
                            </td>
                        </tr>

                        <?php

                }
                ?>

我遇到的问题是它没有显示数据,我也不知道查询是否正在运行。

如何在show.blade.php

中执行此查询并显示查询数据

1 个答案:

答案 0 :(得分:0)

试试这个:

$data = DB::select(DB::raw('Query query'));

现在将此$data传递给刀片view,如:

return view('show', array('data' => $data));

并在foreach()

上应用$data在视图中使用它