dqxhit3376
2015-09-05 18:41
浏览 389
已采纳

在第二个参数中使用DB :: raw()时的不同值where()

I have next query in Laravel Eloquent:

$buildings = Building::select('buildings.*')->join(
    DB::raw('('.
        (
            IngameBuilding::select('buildings.building_id', 'buildings.level')
                          ->join('buildings', 'buildings.id', '=', 'ingame_buildings.building_id')
                          ->toSql()
        ).
    ') as `added_buildings`'), 'added_buildings.building_id', '=', 'buildings.building_id')
    ->where('buildings.level', '>', 'added_buildings.level')
    ->get();

This query returns all allowed rows from base, but one row more. When I added DB::raw() in where() return values is valid.

Good-working code:

$buildings = Building::select('buildings.*')->join(
    DB::raw('('.
        (
          IngameBuilding::select('buildings.building_id', 'buildings.level')
                        ->join('buildings', 'buildings.id', '=', 'ingame_buildings.building_id')
                        ->toSql()
        ).
    ') as `added_buildings`'), 'added_buildings.building_id', '=', 'buildings.building_id')
    ->where('buildings.level', '>', DB::raw('`added_buildings`.`level`'))
    ->get();

Why first code workig, hmm.. Wrong?

图片转代码服务由CSDN问答提供 功能建议

我在Laravel Eloquent中有下一个查询:

  $ buildings  = Building :: select('buildings。*') - > join(
 DB :: raw('('。
(
 \ IngameBuilding :: select('buildings.building_id','buildings.level')  
  - > join('buildings','buildings.id','=','ingame_buildings.building_id')
  - > toSql()
)。
')为`added_buildings`'),  'added_buildings.building_id','=','buildings.building_id')
  - > where('buildings.level','>','added_buildings.level')
  - > get(); \  n   
 
 

此查询从base返回所有允许的行,但更多行。 当我在中添加 DB :: raw()时,其中()返回值有效。

良好的代码:

  $ buildings = Building :: select('buildings。*') - > join(
 DB :: raw('('。
(
 \ IngameBuilding :: 
)  select('buildings.building_id','buildings.level')
  - > join('buildings','buildings.id','=','ingame_buildings.building_id')
  - > toSql()\  n)。
')作为`added_buildings`'),'added_buildings.building_id','=','buildings.building_id')
  - > where('buildings.level','>',DB:  :raw('`added_buildings``level`'))
  - > get(); 
   
 
 

为什么第一个代码workig,嗯..错了吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dougu4027 2015-09-05 18:55
    已采纳

    I'm not a big fan of Laravel at all. I've got only small experience with this framework but i'm almost sure that where function accepts only a 'constant' values to be checked against.

    If you'll get an output of this query using toSQL method on the query object you will see that eloquent will convert it as something like:

    (...) where buildings.level > 'added_buildings.level'
    

    so the condition checks if the buildings.level (whatever the type is) is greater than the given string and not the column value.

    Using the DB::raw you're getting the proper sql as the eloquent won't parse/convert it.

    You would need to use whereRaw method I suppose. http://laravel.com/docs/4.2/queries#introduction

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题