mardi 28 mars 2017

How to use raw query in laravel and show ouput in blade page?

In my controller i use raw query in this format

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

And in 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

                }
                ?>

The problem I am facing is it doesn't show the data, and I don't know either query is running or not.

How can I execute this query and show query data in show.blade.php?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire