laravel 5 - Accessing object properties in PHP -


i trying access properties of custom object in php:

<?php  namespace app\classes;  use illuminate\database\eloquent\model;  class aed extends model {      protected $table = 'aeds';     protected $fillable = ['owner', 'street', 'postal_code', 'locality', 'latitude', 'longitude', 'annotation_type'];     public $timestamps = true;      public $id;     public $owner;     public $object;     public $street;     public $postalcode;     public $locality;     public $latitude;     public $longitude;     public $annotation_type;     public $distance;      public function set($data) {         foreach ($data $key => $value) {             if(property_exists($this, $key)) {                 $this->$key = $value;             }         }     } } 

the code access these properties:

<?php namespace app\transformer;  use app\classes\aed; use league\fractal\transformerabstract;   class aedtransformer extends transformerabstract {     public function transform(aed $aed) {         return [             'data' => $aed->owner         ];     } } 

when call function, response:

{ data: [ { data: null } ], meta: "testmeta" } 

the strange thing is, when var_dump object full info:

...  protected 'original' =>      array (size=11)       'id' => int 1       'owner' => string 'owner 1' (length=7)       'object' => string 'object 1' (length=8)       'street' => string 'street 1' (length=8)       'postal_code' => string '11111' (length=5)       'locality' => string 'locality 1' (length=10)       'latitude' => float 100       'longitude' => float 100       'annotation_type' => string '1' (length=1)       'created_at' => string '0000-00-00 00:00:00' (length=19)       'updated_at' => string '0000-00-00 00:00:00' (length=19) ... 

so data can taken database expected , being received well. why accessing not work , receive "null".

i use "set" method inside custom function here:

class aedhelper {     static public function searchaed($position) {         $var1 = $position['latitude'];         $var2 = $position['longitude'];         $var3 = $position['latitude'];          $queryresults = db::select(db::raw("sqlcode"));         $results = [];          foreach ($queryresults $results) {             $aed = new aed();             $aed->set($results);             $results[] = $aed;         }          return $results;      } 

here create new aed() instance. guess need define object properties therefore not eloquent used custom aed class needs instantiated displaying sql results.

best

you don't have define fields in model. eloquent makes them available dynamically.

if want fill fields can without having them in model. because field available if try set value it.

here how

$aed = new aed; $aed->owner =  "the owner"; $aed->object = "the object"; .... .... ....  $aed->save(); 

or work well

aed::create([   'owner' => "the owner",   'object' => "the object",   .....   .....   ..... ]); 

or if want update existing model.

$aed = aed::find(1); // change owner $aed->owner= "new owner";  $aed->save(); 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -