Reputation: 720
Say for instance I have a status model and I want to create constants from the status table using the status specific field records
class Status extends BaseModel
{
protected $table = 'Status';
protected $primaryKey = 'id';
//here or in the constructor, I wants to query records for the same model then declare constants like
foreach($records as $key => val){
const $key = $val;
}
}
Upvotes: 1
Views: 1442
Reputation: 2800
You lack in basic programming knowledge.
A const variable is supposed to never change
A static variable is supposed to be accessed as class member without initializating class (using a new
keyword)
A non-static non-const variable is accessible only after initializating a class with new
keyword
You probably want to replace a bad
const $key = $val;
with
$this->$key = $val;
Which will work in Laravel
class Status extends BaseModel
{
protected $table = 'Status';
public function __construct(array $attributes) {
parent::__construct($attributes);
$records = [
'some_reflected_field_1' = 100,
'some_reflected_field_2' = 101,
'some_reflected_field_3' = 102
];
foreach($records as $key => val) {
$this->$key = $val;
}
}
}
Execution:
$foo = new Status([
'name' => 'active'
]);
echo $foo->some_reflected_field_2; // 101
Upvotes: 2