gpt4 book ai didi

php - 拉拉维尔 : user defined object name in Join Query

转载 作者:行者123 更新时间:2023-11-29 12:31:19 26 4
gpt4 key购买 nike

我有一个用 Laravel 编写的成功返回的连接查询。我现在尝试将对象名称添加到返回值,就像它是从表返回一样。

Laravel 查询

$post = DB::table('follow')
->join('posts', 'follow.user2', '=', 'posts.userid')
->where('follow.user1',Auth::user()->id)
->where('follow.user2','!=',Auth::user()->id)
->where('posts.created_at','>',$update)
->select('posts.created_at', 'posts.userid')
->orderBy('posts.created_at','desc')
->get();

上面的查询返回以下内容

array (size=1)
0 =>
object(stdClass)[208]
public 'created_at' => int 1418466963
public 'userid' => int 5

我想要实现的是以下输出

array (size=1)
0 =>
object(stdClass)[208]
public 'created_at' => int 1418466963
public 'userid' => int 5
public 'oType' => string 'post' //This is user defined.

我尝试的是(显然是错误的,但只是我在尝试什么的提示)

$post = DB::table('follow')
->join('posts', 'follow.user2', '=', 'posts.userid')
->where('follow.user1',Auth::user()->id)
->where('follow.user2','!=',Auth::user()->id)
->where('posts.created_at','>',$update)
->select('posts.created_at', 'posts.userid', 'oType as post') //Compare this line with 1st query
->orderBy('posts.created_at','desc')
->get();

最佳答案

您可以通过使用 DB::raw() 来实现此目的。

->select('posts.created_at', 'posts.userid', DB::raw('\'post\' as oType'))

关于php - 拉拉维尔 : user defined object name in Join Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27458256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com