首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从拉拉维尔的关系中获得数据?

如何从拉拉维尔的关系中获得数据?
EN

Stack Overflow用户
提问于 2013-08-14 15:39:44
回答 1查看 946关注 0票数 0

我有一个商业模式和订阅模式。我以下列方式加载数据:

代码语言:javascript
复制
Business::with('subscriptions')->get()

然后,我在Business上创建了一个方法,如下所示:

代码语言:javascript
复制
public function check_for_subscription($type)
{
    if($this->subscriptions->isEmpty() === false)
    {
        foreach($this->subscriptions as $subscription)
        {
            dd($subscription);
            if($subscription->type == $type)
            {
                return true;
            }
        }
    }
    return false;
}

国防部向我展示了以下内容:

代码语言:javascript
复制
object(Subscription)#175 (17) {
  ["connection":protected]=>
  NULL
  ["table":protected]=>
  NULL
  ["primaryKey":protected]=>
  string(2) "id"
  ["perPage":protected]=>
  int(15)
  ["incrementing"]=>
  bool(true)
  ["timestamps"]=>
  bool(true)
  ["attributes":protected]=>
  array(7) {
    ["id"]=>
    int(1)
    ["business_id"]=>
    int(1)
    ["type"]=>
    string(3) "614"
    ["starts_at"]=>
    NULL
    ["ends_at"]=>
    NULL
    ["created_at"]=>
    string(19) "0000-00-00 00:00:00"
    ["updated_at"]=>
    string(19) "0000-00-00 00:00:00"
  }
  ["original":protected]=>
  array(7) {
    ["id"]=>
    int(1)
    ["business_id"]=>
    int(1)
    ["type"]=>
    string(3) "614"
    ["starts_at"]=>
    NULL
    ["ends_at"]=>
    NULL
    ["created_at"]=>
    string(19) "0000-00-00 00:00:00"
    ["updated_at"]=>
    string(19) "0000-00-00 00:00:00"
  }
  ["relations":protected]=>
  array(0) {
  }
  ["hidden":protected]=>
  array(0) {
  }
  ["visible":protected]=>
  array(0) {
  }
  ["fillable":protected]=>
  array(0) {
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["touches":protected]=>
  array(0) {
  }
  ["with":protected]=>
  array(0) {
  }
  ["exists"]=>
  bool(true)
  ["softDelete":protected]=>
  bool(false)
}

如果我试着做$subscription->type,我什么也得不到。对如何让这件事奏效有什么想法吗?

这是我的商业模式的开始

代码语言:javascript
复制
class Business extends Eloquent 
{
    public function subscriptions()
    {
        return $this->hasMany('Subscription');
    }
}

这是我的订阅模式

代码语言:javascript
复制
class Subscription extends Eloquent 
{
    public function businesses()
    {
        return $this->belongsTo('Business');
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-12 18:18:24

根据dd()输出,订阅对象没有名为"type“的属性。这就解释了为什么你没有从$订阅->类型得到任何东西。

根据dd()输出,订阅对象do有一个名为"attributes“的受保护属性,它是一个数组。数组的一个键是"type“,所以我假设这就是您要达到的值。

由于“属性”数组是受保护的,所以不能从外部类访问它。我假设您的订阅类有一个名为getAttributes()的getter函数,它返回受保护的数组。如果是这样..。你唯一需要的就是:

代码语言:javascript
复制
public function check_for_subscription($type)
{
    if($this->subscriptions->isEmpty() === false)
    {
        foreach($this->subscriptions as $subscription)
        {
            $attributes = $this->subscriptions->getAttributes();
            if($attributes['type'] == $type)
            {
                return true;
            }
        }
    }
    return false;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18236209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档