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

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

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条回答 默认 最新

  • dtf579777 2015-09-09 10:19
    关注

    Finally figured it out.

    What I should have been writing was:

    $ipam = Ipam::where('id','=',130152)->take(1)->first();
    $ipam->customer->getOriginal();
    

    which returns me an array of the data I was requesting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?