When I create a model, I want to check if the table exists in the DB. If it doesn't then I want to return a null. This is to prevent a record insert if the table doesn't exist yet.
In my model I have tried this. The Schema call returns false, but I'm still getting a model returned and not a null.
class SomeDataTable extends Model
{
public function __construct($id)
{
$this->setTable($id);
if (Schema::connection($this->connection)->hasTable($this->table) === false) {
return null;
}else{
parent::__construct();
}
}
public function setTable($id)
{
$this->table = $id.'_some_data_table';
}
}
nullin constructor, constructor is used to initialize the object and its properties by assigning. When you want a object to become nullable. that means you want to destroying the object or clean up resources.