douxie1957 2015-09-07 11:30
浏览 32
已采纳

如何从雄辩的请求中访问受保护的属性

My request:

 $ipam = Ipam::with('Customer')->where('id','=', x)->take(1)->first();

I am having a hard time figuring out how to access the customer attributes that come with my Ipam request. The array containing the attributes is protected so I am unable to access them.

I can access the original attributes of the Ipam table by using the

$ipam->getOriginal();

method but unfortunately that does not return any of the customer attributes(which is to be expected).

I have tried looking through the Model documentation and haven't found anything that helps me access the customer attributes.

Any tips would be greatly appreciated.

Relevant information:

Ipam model class:

class Ipam extends Eloquent
{
    public function container() {

        return $this->belongsToMany('Container');
    }

    public function customer() {
        return $this->hasOne('Customer','customer_id','customer_id');
    }
}

Customer model class:

class Customer extends Eloquent
{
    protected $primaryKey = 'customer_id';

    public function nets() {
        return $this->hasMany('Ipam');
    }
}

Data from the query response(I removed empty fields and data to reduce the clutter):

object(Ipam)#444 (26) {
  ["table":protected]=>
  string(8) "ipam_net"
  ["primaryKey":protected]=>
  string(2) "id"
  ["perPage":protected]=>
  int(15)
  ["incrementing"]=>
  bool(true)
  ["attributes":protected]=>
  array(12) {
    ["id"]=>
    int(x)
    ["customer_id"]=>
    int(SomeId)

        /*******************
        Some more table data
        *******************/
  }
  ["original":protected]=>
  array(12) {
    ["id"]=>
    int(x)
    ["customer_id"]=>
    int(SomeId)

        /*******************
        Some more table data
        *******************/
  }
  ["relations":protected]=>
  array(1) {
    ["Customer"]=>
    object(Customer)#446 (26) {
      ["table":protected]=>
      string(8) "customer"
      ["primaryKey":protected]=>
      string(11) "customer_id"
      ["perPage":protected]=>
      int(15)
      ["incrementing"]=>
      bool(true)
      ["attributes":protected]=>
      array(14) {
        ["customer_id"]=>
        int(SomeId)
        ["customer_name"]=>
        string(27) "SomeCustomerName"

        /*******************
        Some more table data
        *******************/
      }
      ["original":protected]=>
      array(14) {
        ["customer_id"]=>
        int(SomeId)
        ["customer_name"]=>
        string(27) "SomeCustomerName"

        /*******************
        Some more table data
        *******************/
      }
      ["guarded":protected]=>
      array(1) {
        [0]=>
        string(1) "*"
      }
      ["exists"]=>
      bool(true)
    }
  }
  ["guarded":protected]=>
  array(1) {
    [0]=>
    string(1) "*"
  }
  ["exists"]=>
  bool(true)
}
  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 R语言绘制火山图对数据使用case when函数报错
      • ¥15 关于对一篇论文的统计图作图产生好奇
      • ¥15 如何使用RISC-V对斐波那契递归算法进行优化
      • ¥15 OBS 虚拟摄像头和真实摄像头区别
      • ¥15 yolov5运行detect 过程中的报错一直无法解决
      • ¥15 jsx写法 插槽不生效的问题
      • ¥15 MATLAB 调用comsol如何加速设计?
      • ¥15 echarts地图添加点击事件
      • ¥15 openpyxl库无法全部写入文本到单元格
      • ¥15 不知道带什么标题好随便搞一个吧