Laravel 在哪里查询不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33374289/
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 where query not working
提问by Pablo
I've got this table
我有这张桌子
beds
床
id
ID
name
姓名
size
尺寸
room
房间
status
地位
hotel
酒店
created_at
created_at
updated_at
更新时间
I need to filter all beds that belong to a certain room. In order to do so, I've coded this lines.
我需要过滤属于某个房间的所有床位。为此,我对这些行进行了编码。
public function index()
{
//
$user = JWTAuth::parseToken()->authenticate();
$data = Input::get('room');
if( $data ){
$beds = Bed::where('room', '=', $data )->get();
}else{
$beds = Bed::where('hotel', '=', $user->hostel )->get();
}
foreach( $beds as $bed) {
return $bed->get( array('size','room', 'id') );
}
}
So, If i give it the room id, it should return me only that room's ones. The thing is that it's returning all table entries.
所以,如果我给它房间 id,它应该只返回那个房间的。问题是它正在返回所有表条目。
Any ideas?
有任何想法吗?
UPDATE
更新
Fixed relations and tried this:
固定关系并尝试这样做:
return Room::with('beds')->findOrFail($data)->beds;
Now it gives me the number of items. How can I get the items?
现在它给了我项目的数量。我怎样才能得到物品?
UPDATE
更新
This is the model's code:
这是模型的代码:
class Room extends \Eloquent {
protected $fillable = array('beds', 'price', 'name', 'description','hotel');
public function beds(){
return $this->hasMany('Bed', 'id', 'room');
}
}
UPDATE
更新
The var_dump for:
var_dump 用于:
var_dump( Room::with('beds')->findOrFail($data)->beds );
is:
是:
int(1)
UPDATE
更新
So, the final code is the following.
所以,最终的代码如下。
controller
控制器
public function index()
{
//
$user = JWTAuth::parseToken()->authenticate();
$data = Input::get('room');
if( $data ){
$d = intval( $data );
return Bed::where('room', '=', $d )->get( array('size', 'room', 'id', 'name') );
}else{
return Bed::where('hotel', '=', $user->hostel )->get( array('size', 'room', 'id', 'name') );
}
}
model
模型
class Room extends \Eloquent {
protected $fillable = array('beds', 'price', 'name', 'description','hotel');
public function camas(){
return $this->hasMany('Bed', 'room', 'id');
}
}
Thank you guys!
谢谢你们!
回答by Jarek Tkaczyk
You have quite a few issues in your attempts:
您在尝试中遇到了很多问题:
return $bed->get( array('size', 'room', 'id') );
// runs SELECT size, room, id from `rooms`
so it returns all the rooms (why on earth would you like to do this in a foreach anyway?)
所以它返回所有房间(你到底为什么要在 foreach 中这样做?)
return $this->hasMany('Bed', 'id', 'room');
// should be:
return $this->hasMany('Bed', 'room', 'id');
protected $fillable = array('beds', ...
public function beds(){
this is conflict - you will never get a relations when calling $room->beds
since you have a column beds
on your table.
这是冲突 -$room->beds
因为你beds
的桌子上有一列,所以在打电话时你永远不会得到关系。
that said, this is what you need:
也就是说,这就是你需要的:
public function index()
{
$user = JWTAuth::parseToken()->authenticate();
if(Input::has('room')){
$query = Bed::where('room', '=', Input::get('room'));
}else{
$query = Bed::where('hotel', '=', $user->hostel);
}
return $query->get(['size', 'room', 'id']); // given you need only these columns
}
回答by Pawel Bieszczad
Try this and see if it works. If not, can you provide the var_dump
of Input::get('room')
and the structure of the the beds table?
试试这个,看看它是否有效。如果没有,你可以提供var_dump
的Input::get('room')
和床表的结构?
public function index()
{
//
$user = JWTAuth::parseToken()->authenticate();
$data = Input::get('room');
if( $data ){
$beds = Bed::where('room', '=', $data );
}else{
$beds = Bed::where('hotel', '=', $user->hostel );
}
return $beds->get(['size','room', 'id'])->toArray();
}
Better yet if you want to get specific beds in a room and you have your relations set up correctly:
更好的是,如果您想在房间中使用特定的床位并且您的关系设置正确:
return Room::with('beds')->findOrFail($data)->beds;
EDIT
编辑
I saw your update. Are you sure its giving you a number of items, maybe there is one item and the number is the id of it. Can you verify? Please provide a vardump of it if thats not the case. Also can you post your code for the relations in the model?
我看到了你的更新。你确定它给了你一些物品,也许只有一个物品,数字是它的 id。你能验证吗?如果不是这种情况,请提供它的 vardump。您还可以发布模型中关系的代码吗?