尝试将原始 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();
当然,您需要在 Booking
和 Status
之间具有 status
关系
如果您的 BookingStatus
模型中有 color
列,您现在就可以访问它:
foreach ($bookings as $b) {
echo $b->booking_status->color;
}
关于php - 使用 Eloquent 在选择查询中将外部元素传递到模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26778581/