Laravel 5.2查询不起作用

时间:2016-12-01 19:59:55

标签: php mysql laravel-5

我尝试让这个查询起作用:

DB::select(
    DB::raw('select 
        sum(case when outreach_links.status="Pending" and outreach_links.created_at >= :from and outreach_links.created_at <=:to then 1 else 0 end) as pp,
        sum(case when outreach_links.status="Approved" and outreach_links.start_date >= :from and outreach_links.start_date <=:to then 1 else 0 end) as aa,
        sum(case when outreach_links.status="Approved" then outreach_links.cost else 0 end) as cc,
        MONTH(outreach_links.created_at) month,
        MONTHNAME(outreach_links.created_at) month_name,
        YEAR(outreach_links.created_at) year from outreach
        inner join outreach_links on outreach.id = outreach_links.outreach_id
        where outreach.profile_id=:proid
        group by month, year',
    ["from" => $from, "to" => $to, "proid" => $pro_id])
)->get();

但我收到错误:

Call to a member function get() on a non-object

我改为这个并且有效:

DB::select(
    DB::raw('select 
        sum(case when outreach_links.status="Pending" and outreach_links.created_at >= ? and outreach_links.created_at <=? then 1 else 0 end) as pp,
        sum(case when outreach_links.status="Approved" and outreach_links.start_date >= ? and outreach_links.start_date <=? then 1 else 0 end) as aa,
        sum(case when outreach_links.status="Approved" then outreach_links.cost else 0 end) as cc,
        MONTH(outreach_links.created_at) month,
        MONTHNAME(outreach_links.created_at) month_name,
        YEAR(outreach_links.created_at) year from outreach
        inner join outreach_links on outreach.id = outreach_links.outreach_id
        where outreach.profile_id=?
        group by month, year'),
    [$from, $to, $from, $to, $pro_id]
);

1 个答案:

答案 0 :(得分:0)

DB::raw() doesn't parse the query.尝试将您的条件移到raw来电之外:

DB::select(
    DB::raw('select 
        sum(case when outreach_links.status="Pending" and outreach_links.created_at >= :from and outreach_links.created_at <=:to then 1 else 0 end) as pp,
        sum(case when outreach_links.status="Approved" and outreach_links.start_date >= :from and outreach_links.start_date <=:to then 1 else 0 end) as aa,
        sum(case when outreach_links.status="Approved" then outreach_links.cost else 0 end) as cc,
        MONTH(outreach_links.created_at) month,
        MONTHNAME(outreach_links.created_at) month_name,
        YEAR(outreach_links.created_at) year from outreach
        inner join outreach_links on outreach.id = outreach_links.outreach_id
        where outreach.profile_id=:proid
        group by month, year'),
    ["from" => $from, "to" => $to, "proid" => $pro_id]
)->get();