Laravel在哪里查询不工作

pobjuy32  于 2023-06-24  发布在  其他
关注(0)|答案(3)|浏览(88)

我占了这张table

id
姓名
尺寸
房间
地位
酒店
创建于
更新_at
我需要过滤属于某个房间的所有床。为了做到这一点,我对这些行进行了编码。

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') );
        }
    }

所以,如果我给它房间ID,它应该只返回那个房间的。问题是它返回了所有的表项。
有什么想法吗
更新
固定的关系,并尝试了这个:

return Room::with('beds')->findOrFail($data)->beds;

现在它给了我项目的数量。我如何才能获得物品?
更新
这是模型的代码:

class Room extends \Eloquent {
    protected $fillable = array('beds', 'price', 'name', 'description','hotel');

    public function beds(){
        return $this->hasMany('Bed', 'id', 'room');
    }
}

更新
var_dump用于:

var_dump( Room::with('beds')->findOrFail($data)->beds );

是:

int(1)

更新
最后的代码如下。
控制器

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') );    
        }

    }

模型

class Room extends \Eloquent {
    protected $fillable = array('beds', 'price', 'name', 'description','hotel');

    public function camas(){
        return $this->hasMany('Bed', 'room', 'id');
    }
}

谢谢你们!

6ioyuze2

6ioyuze21#

您在尝试中遇到了一些问题:

return $bed->get( array('size', 'room', 'id') );
// runs SELECT size, room, id from `rooms`

所以它返回所有的房间(你到底为什么要在foreach中这样做呢?)

return $this->hasMany('Bed', 'id', 'room');
// should be:
return $this->hasMany('Bed', 'room', 'id');
protected $fillable = array('beds', ...
public function beds(){

这是冲突-你永远不会得到一个关系时,调用$room->beds,因为你有一个列beds在您的表。
也就是说,这就是你需要的:

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

}
gopyfrb3

gopyfrb32#

试试看它是否有效。如果没有,你能提供Input::get('room')var_dump和床表的结构吗?

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

更好的是,如果你想得到特定的床在一个房间,你有你的关系设置正确:

return Room::with('beds')->findOrFail($data)->beds;

编辑
我看到你的更新了。你确定它给你一个项目的数量,也许有一个项目和编号是它的id。你能证实吗?如果不是这样,请提供一个vardump。你也可以在模型中发布你的关系代码吗?

nimxete2

nimxete23#

从您的模型中删除以下内容,有时这就是问题所在

use SoftDeletes;

相关问题