首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Eloquent在select查询中将外部元素传递给模型

使用Eloquent在select查询中将外部元素传递给模型
EN

Stack Overflow用户
提问于 2014-11-06 19:39:59
回答 2查看 659关注 0票数 0

尝试将原始mysql中的查询转换为有说服力的早期查询。我一直在尝试获取it...Mainly内部的外部元素(来自其他模型)“颜色”(来自BookingStatus ),然后创建网址并将其作为网址放入。我可以请您帮个忙吗?

下面是mysql查询:

代码语言:javascript
复制
$booking = DB::table('bookings')
                        ->select('bookings.id as id', 'booking_reference as description', 'booking_status as title', 'color', DB::raw('concat("http://127.0.0.1/blabla/public/profile/Calendar/detail/",bookings.id) as url'),'booking_date as start')
                        ->join('booking_status','booking_status.id','=','bookings.bookingstatus_id')
                        ->where('photographer_id', '=', $photographer->id)
                        ->union(DB::table('bookings')
                            ->select('bookings.id as id', 'booking_reference as description', 'booking_status as title', 'color',DB::raw('concat("http://127.0.0.1/KYMA/public/profile/Calendar/detail/",bookings.id) as url'),'booking_date as start')
                            ->join('booking_status','booking_status.id','=','bookings.bookingstatus_id')
                            ->where('user_client_id', '=', $user->id)
                        )
                        ->get(); 

编辑:这就是我到目前为止所做的

代码语言:javascript
复制
$booking_info = Booking::with('bookingStatus')
                        ->where('photographer_id', $photographer->id)
                        ->orWhere('user_client_id', $user->id)
                        ->select(['id as id', 'booking_reference as description','booking_date as start', 'bookingstatus_id as title'])
                        ->get();

我尝试了一些关于颜色的东西,并阅读了Laravel的文档,但我就是不能正确地理解它。如何在select查询中传递它?如果我简单地添加'color‘,显然它不会拾取它,因为它在附加的数组中,而不是“主”数组中……

非常感谢你的帮助!

/ EDIT /这是我发现的一个能够传递我想要的一切的解决方案,我没有使用模型关系思想,因为我不能理解如何在我的select请求中传递它们的数组值...

代码语言:javascript
复制
                $booking_info = Booking::where('photographer_id', $photographer->id)
                        ->orWhere('user_client_id', $user->id) //because he could be client of a shoot ;-)
                        ->join('booking_status','booking_status.id','=','bookings.bookingstatus_id')
                        ->join('shooting_types', 'shooting_types.id', '=', 'bookings.stype_id')
                        ->join('users', 'users.id', '=', 'user_client_id')
                        ->join('addresses', 'addresses.id', '=', 'users.address_id')
                        ->select('bookings.id as id', 'first_name', 'phonenumber', 'booking_reference as description', 'stype_name as title', 'color', 'booking_date as start')
                        ->get();
EN

回答 2

Stack Overflow用户

发布于 2014-11-06 20:14:36

您可以使用:

代码语言:javascript
复制
$bookings = Booking::with('booking_status')
                   ->where('photographer_id', $photographer->id)
                   ->orWhere('user_client_id', $user->id)
                   ->get();

当然,您需要在BookingStatus之间建立status关系

如果您的BookingStatus模型中有color列,现在就可以访问它:

代码语言:javascript
复制
foreach ($bookings as $b) {
  echo $b->booking_status->color;
}
票数 1
EN

Stack Overflow用户

发布于 2014-11-06 19:53:15

只要模型核心api可以通过与其他表的相关关联在控制器中直接调用,就不建议使用原始SQL。我建议使用带关系的模型核心api。

例如

代码语言:javascript
复制
$bookings = Booking::with('booking_status')
                   ->where('photographer_id', $photographer->id)
                   ->get();

上面的示例代码只是在控制器中使用laravel雄辩关联的一个示例。但是,您需要根据您的要求对上述条件进行更改。

注意:只有当您在所有相关模型中定义了关联时,上述代码才会起作用。例如,预订模型必须与BookingStatus模型关联"hasMany“,类似地,BookingStatus模型应与预订表关联"belongsTo”。通过这种方式,您不需要编写自定义查询。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26778581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档