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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器