来自另一个表的 Laravel 5 where 子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33404532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 where clause from another table
提问by wobsoriano
I have a simple code and what I want to do is to access a field from another table and put it in my where clause. This is my code:
我有一个简单的代码,我想要做的是从另一个表访问一个字段并将其放入我的 where 子句中。这是我的代码:
ReportController.php
报表控制器.php
$reservations = Reservation::with('charge', 'room', 'client')
-> whereBetween('reservation_from', [$from, $to])
-> where('room.type', \Request::input('type')) //what should this be
-> orderBy('created_at')
-> get();
Room.php
房间.php
class Room extends Model
{
use SoftDeletes;
protected $table = 'rooms';
protected $fillable = ['id', 'roomNumber', 'type', 'price', 'description'];
public function reservations() {
return $this->hasMany('App\Reservation', 'id', 'room_number');
}
}
Reservation.php
预订.php
class Reservation extends Model
{
use SoftDeletes;
protected $table = 'reservations';
protected $fillable = ['roomNumber', 'clientId', 'reservation_from', 'reservation_to'];
public function room() {
return $this->belongsTo('App\Room');
}
}
As you can see in the ReportController.php
, there is a comment saying "what should this be", that's the part that I want to fix. What I wanted to do is access the type
field in the rooms table in my eloquent query.
正如您在 中看到的ReportController.php
,有一条评论说“这应该是什么”,这就是我想要修复的部分。我想要做的是type
在我雄辩的查询中访问房间表中的字段。
The query that I want to do is like this:
我想做的查询是这样的:
select * from `reservations` where `reservation_from` between '2015-10-29' and '2015-10-29' and `rooms.type` = "test"
Is there a way to do this? Thank you.
有没有办法做到这一点?谢谢你。
回答by Thomas Kim
What you are looking for is the whereHas
method.
你要找的是whereHas
方法。
$reservations = Reservation::with('charge', 'room', 'client')
->whereBetween('reservation_from', [$from, $to])
->whereHas('room', function($query) {
$query->where('type', '=', \Request::input('type'));
})
->orderBy('created_at')
->get();
Link to docs: http://laravel.com/docs/5.1/eloquent-relationships#querying-relations
链接到文档:http: //laravel.com/docs/5.1/eloquent-relationships#querying-relations
Edit:
编辑:
Editing this to clarify some things in the comments.
编辑此内容以澄清评论中的一些内容。
To create convenient, reusable query constraints to make your code cleaner, you can use query constraints: http://laravel.com/docs/5.1/eloquent#query-scopes
要创建方便、可重用的查询约束以使您的代码更简洁,您可以使用查询约束:http: //laravel.com/docs/5.1/eloquent#query-scopes
Also, because queries can be chained, you can do something like this:
此外,由于查询可以链接,因此您可以执行以下操作:
// Create query with common constraints
$query = Reservation::with('charge', 'room', 'client')
->whereBetween('reservation_from', [$from, $to]);
// Ternary operator to decide whether or not to add whereHas constraint
$query = (\Request::input('type') == "all") ? $query : $query->whereHas('room', function($query) {
$query->where('type', '=', \Request::input('type'));
});
// Finally, fetch the results sorted by 'latest', which is a convenient way of doing "orderBy('created')"
$reservations = $query->latest()->get();
回答by whoacowboy
I believe you are looking to do this. Updated per your question update. The with
method takes a string or an array.
我相信你正在寻找这样做。根据您的问题更新更新。该with
方法采用字符串或数组。
$reservations = Reservation::with(['charge', 'client', 'room' =>
function($query){
$query->where('type', \Request::input('type'));
}])
->whereBetween('reservation_from', [$from, $to])
->orderBy('created_at')
->get();