【问题标题】:Join query is not working properly in laravel 4.2连接查询在 laravel 4.2 中无法正常工作
【发布时间】:2014-12-03 06:34:57
【问题描述】:

我正在使用 laravel 4.2。当我运行这个Left Join 查询时,

$data = DB::table('posts')
        -> leftJoin('users','posts.user_id','=','users.id')
        -> where('posts.id',$id)
        -> select('posts.id as post_id,posts.title as title,users.firstname as fname')
        -> first();

当我打印$data 得到结果时,

stdClass Object(
   [post_id,posts.title] => 5
)

这不是预期的答案。我希望得到这样的答案,

stdClass Object(
  [post_id] => 2
  [title] => Test3
  [fname] => myname
)

我怎样才能得到正确的结果?这个查询有什么问题吗?

【问题讨论】:

    标签: jquery join laravel left-join


    【解决方案1】:

    改变选择

      $data = DB::table('posts')
                -> leftJoin('users','posts.user_id','=','users.id')
                -> where('posts.id',$id)
                -> select('posts.id as post_id','posts.title as title','users.firstname as fname')
                -> first();
    

    【讨论】:

      【解决方案2】:
      $data = DB::table('posts')
          -> select('posts.id,users.id,posts.title users.firstname')
          -> leftJoin('users','posts.user_id','=','users.id')
          -> where('posts.id',$id)
          -> first();
      

      这应该会给你适当的结果

      【讨论】:

        猜你喜欢
        • 2015-01-12
        • 2015-11-06
        • 1970-01-01
        • 2015-02-20
        • 2020-12-10
        • 1970-01-01
        • 1970-01-01
        • 2021-02-04
        • 2014-09-30
        相关资源
        最近更新 更多