php - 使用 Eloquent 在选择查询中将外部元素传递到模型

标签 php mysql sql laravel eloquent

尝试将原始 mysql 中的查询转换为 Eloquent laravel 查询。我一直试图在其中获取外部元素(来自其他模型)...主要是“颜色”(来自 BookingStatus 并创建 URL 并将其作为 URL 放入。 你能帮我一下吗?

这是 mysql 查询:

$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(); 

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

$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 的文档,但我就是无法正确...我如何在我的选择查询中传递它?如果我只是添加“颜色”,显然它不会拾取它,因为它位于附加数组中,而不是“主要”数组中......

非常感谢您的帮助!!

/////编辑///// 这是我发现能够传递我想要的所有内容的解决方案,我没有使用模型关系思想,因为无法理解如何在我的选择请求中传递它们的数组值...

                $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();

最佳答案

您可以使用:

$bookings = Booking::with('booking_status')
                   ->where('photographer_id', $photographer->id)
                   ->orWhere('user_client_id', $user->id)
                   ->get();

当然,您需要在 BookingStatus 之间具有 status 关系

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

foreach ($bookings as $b) {
  echo $b->booking_status->color;
}

关于php - 使用 Eloquent 在选择查询中将外部元素传递到模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26778581/

相关文章:

mysql - 我尝试安装 MySQL Community,但它总是安装 MySQL Commercial

mysql - 如何返回 WHERE 子句的下一行?

PHP MySQL 查询生成器

php - 如何动态设置变量php,设计模式

php - 没有明显原因的多个 mysql 行条目(偶尔发生一次)

mySQL - 获取正在查询的分片

mysql - 创建唯一索引多列,其中一列的值为零

php - 安全、社区驱动的 mysql 论坛?

javascript - 如何更改 PHP 电子邮件表单(返回部分)以匹配 Javascript 和 html 模板?

mysql - 在 Mysql 中选择具有数字范围的查询