Bli
Bli

Reputation: 71

How to access a protected attribute from a eloquent request

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)
}

Upvotes: 1

Views: 5307

Answers (1)

Bli
Bli

Reputation: 71

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.

Upvotes: 3

Related Questions