带有模型::带有附加列的Laravel Prolem

时间:2018-07-05 21:45:40

标签: mysql laravel laravel-5.1 laravel-query-builder

我尝试使用Model :: with

添加附加列
$pitchChech =  Pitch::with(['prices'=>function ($query){
            $query->select('*',\DB::Raw("(select sum(HOUR(TIMEDIFF(valid_to ,valid_from))) from pitch_price as xpitch_price where pitch_id = pitch_price.pitch_id ) as x "))
            ->whereRaw(" x < ?",[24]);
        }])->get();
      dd($pitchChech);

我有这个输出

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'x' in 'where clause'  

我无法理解使用laravel 5.1是什么问题,但是我不认为ut会有问题

1 个答案:

答案 0 :(得分:0)

对于派生列,您必须使用having()

->having('x', '<', 24)