Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使Laravel访问器返回查询结果_Php_Laravel - Fatal编程技术网

Php 使Laravel访问器返回查询结果

Php 使Laravel访问器返回查询结果,php,laravel,Php,Laravel,更新: 我正在尝试将我自己的带有子查询结果的属性添加到主查询的结果中 现在,我有三个表之间的多对多关系:锦标赛、参与者和用户 以下是锦标赛模型中关系的定义: public function users() { return $this->belongsToMany('App\User', 'Participants', 'Id_tourn', 'Id_user')->withPivot('Rating'); } 表的结构是: Users: -Id -Name Partic

更新:

我正在尝试将我自己的带有子查询结果的属性添加到主查询的结果中

现在,我有三个表之间的多对多关系:锦标赛、参与者和用户

以下是锦标赛模型中关系的定义:

public function users() {
    return $this->belongsToMany('App\User', 'Participants', 'Id_tourn', 'Id_user')->withPivot('Rating');
}
表的结构是:

Users:
-Id
-Name

Participants:
-Id
-Id_user
-Id_tournament
-Final
-Final_place

Tournaments:
-Id
-Name
我需要在我的最终查询结果中有额外的
Winners
属性,其中我将有前三名的信息

根据文档,我创建了一个访问器并尝试了不同的变体:

  • 那只是冻结了系统。没有发生任何事情,在30秒内我得到超时错误

    公共函数getWinnersAttribute(){
    返回锦标赛::where(“Id”,“=”,$this->attributes['Id'])->where(“决赛”,“=”,1)->limit(3)->orderBy(“决赛”,“asc”)->get();
    }

  • 这将返回一个错误,即“决赛”列不在Tournaments表中,因此
    $This
    没有关系:

    public function getWinnersAttribute($value)
    {
        return $this->where("Finals",'=',2);
    }
    
    公共函数getWinnersAttribute()
    {
    返回$this->where(“final”、“=”,1)->limit(3)->orderBy(“final_place”、“asc”)->get();
    }

  • 这将返回空白的白色页面,没有任何内容:

    公共函数getWinnersAttribute()
    {
    返回$this->with('users')->where('final',“=”,1)->limit(3)->orderBy('final_place”,“asc”)->get();
    }

  • 此返回“Winners”属性为空:

    公共函数getWinnersAttribute()
    {
    返回$this->with(“users”)->其中(“final”和“=”,1)->limit(3)->orderBy(“final_place”,“asc”);
    }

  • 我已经创建了
    $appends
    变量来应用访问器:
    受保护的$appends=['Winners']

    但是,我检查了访问器,它可以工作。如果我只是回来:

    public function getWinnersAttribute()
    {
        return "123";
    }
    
    它工作正常,我在主查询结果的
    “winners”
    属性中获得
    “123”

    主要查询是:

    Tournaments::with(['users'])->get();
    
    final
    列位于多对多关系的透视表中

    更新: 当我尝试将查询返回到该模型时,没有关系:

    public function getWinnersAttribute($value)
    {
        return $this->where("Finals",'=',2);
    }
    
    我在这方面也一无所获。与此类似,不执行子查询

    如果我在返回的末尾添加
    get()

    return $this->where("Finals",'=',2)->get();
    
    我得到一张空白的白纸。 我怎样才能解决这个问题


    非常感谢。

    如果锦标赛模型上有
    getWinnersAttribute
    ,这意味着您已经有了一个您正在调用的锦标赛模型,例如在属性中执行
    Tornament::find(1)->winners
    ,您也在尝试再次查找该模型,这可能会使它成为一个永远的循环,试图找到一个已经有一个的新循环,等等。尝试使用
    $this

    public function getWinnersAttribute()
    {
        return $this->where("finals","=",1)->limit(3)->orderBy("final_place","asc")->get();
    }
    

    getWinnersAttribute是否在锦标赛模型上?@Casper,是的,它是赢家实际上是锦标赛对象本身,还是他们是参与锦标赛的另一个对象?为什么不创建一个关系?@Pablo,我忘了提到这一点。我已经有关系了,但我需要在我的“winners”属性中使用特殊的“where”子句。谢谢。我将在15分钟内检查该版本并让您知道。不幸的是,这不起作用,因为我需要将其与关系(内部关系)一起使用。我忘了说那个了。我会更新这个问题。