How can I pass variables from a child class into the methods of a parent constructors class? I am able to create an asset with this constructor without any problems.
I have a feeling that this has something to do with the way that eloquent is handling the protected videos. All three of the variables are making it to their respective methods before the error is being thrown.
//Parent Class
class Asset extends Model
{
protected $fillable = [
'type', 'title','origin',
];
// protected $author_id;
// protected $keywords = [];
// protected $proof_status;
// protected $description;
// protected $usageHistory;
public function __construct($type, $title, $origin)
{
$this->setType($type);
$this->setTitle($title);
$this->setOrigin($origin);
}
// Setters
public function setType($type){
$this->type = $type;
}
public function setTitle($title)
{
$this->title = $title;
}
public function setOrigin($origin)
{
$this->origin = $origin;
}
public function setAltTitle($alt_title)
{
$this->alt_title = $alt_title;
}
public function setDescription($description)
{
$this->description = $description;
}
}
// Child Class
class Recipe extends Asset
{
public function __construct($type, $title, $origin){
parent::__construct($type, $title, $origin);
}
}
I expected this to set $type, $origin, and $title but instead this is the error that I receive SQLSTATE[42S22]: Column not found: 1054 Unknown column 'type' in 'field list' (SQL: insert into `recipes` (`type`, `updated_at`, `created_at`) values (recipe, 2019-07-11 19:35:23, 2019-07-11 19:35:23))
You can resolve this issue by adding the following I believe:
// Child Class
class Recipe extends Asset
{
protected $table = 'assets';
}
However, I would highly recommend examining if what you are doing is an anti-pattern. Without knowing more information it sounds like in the long run you are going to want to create polymorphic relations on the assets table with foreign keys to other tables.
I would strongly consider not implementing the code above to resolve your issue. Using this will most likely continue to cause headaches going forward.