如何从雄辩的请求

时间:2015-09-07 11:30:32

标签: php laravel-4 eloquent

我的要求:

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

我很难弄清楚如何访问我的Ipam请求附带的客户属性。包含属性的数组受到保护,因此我无法访问它们。

我可以使用

访问Ipam表的原始属性
$ipam->getOriginal();

方法,但不幸的是,它不会返回任何客户属性(这是预期的)。

我尝试查看Model文档,但没有找到任何可以帮助我访问客户属性的内容。

任何提示都将不胜感激。

相关信息:

Ipam模型类:

class Ipam extends Eloquent
{
    public function container() {

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

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

客户模型类:

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

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

来自查询响应的数据(我删除了空字段和数据以减少混乱):

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 个答案:

答案 0 :(得分:1)

终于明白了。

我应该写的是:

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

返回我正在请求的数据数组。