Last active
March 31, 2016 10:29
-
-
Save crishoj/00d3547fc5123011012e292fc7ea5f16 to your computer and use it in GitHub Desktop.
Eloquent base model for schemas with camel-cased tables and fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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