Skip to content

Instantly share code, notes, and snippets.

@crishoj
Last active March 31, 2016 10:29
Show Gist options
  • Save crishoj/00d3547fc5123011012e292fc7ea5f16 to your computer and use it in GitHub Desktop.
Save crishoj/00d3547fc5123011012e292fc7ea5f16 to your computer and use it in GitHub Desktop.
Eloquent base model for schemas with camel-cased tables and fields
<?php
namespace app;
use Illuminate\Database\Eloquent\Model;
class CamelCasedModel extends Model
{
public function getForeignKey()
{
// Default foreign key name is camel-cased
return lcfirst(class_basename($this)) . 'Id';
}
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
{
if (is_null($foreignKey))
// Foreign keys are camel-cased by default
$foreignKey = lcfirst(class_basename($related).'Id');
return parent::belongsTo($related, $foreignKey, $otherKey, $relation);
}
public function getTable()
{
if (isset($this->table))
return $this->table;
// Table names are camel-cased by default
return lcfirst(str_plural(class_basename($this)));
}
public function joiningTable($related)
{
// Joining tables are camel-cased by default
return camel_case(parent::joiningTable($related));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment