Model.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572
  1. <?php namespace Illuminate\Database\Eloquent;
  2. use Closure;
  3. use DateTime;
  4. use Carbon\Carbon;
  5. use ArrayAccess;
  6. use Illuminate\Events\Dispatcher;
  7. use Illuminate\Database\Connection;
  8. use Illuminate\Database\Eloquent\Collection;
  9. use Illuminate\Database\Eloquent\Relations\HasOne;
  10. use Illuminate\Database\Eloquent\Relations\HasMany;
  11. use Illuminate\Support\Contracts\JsonableInterface;
  12. use Illuminate\Support\Contracts\ArrayableInterface;
  13. use Illuminate\Database\Eloquent\Relations\MorphOne;
  14. use Illuminate\Database\Eloquent\Relations\MorphMany;
  15. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  16. use Illuminate\Database\Query\Builder as QueryBuilder;
  17. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  18. use Illuminate\Database\ConnectionResolverInterface as Resolver;
  19. abstract class Model implements ArrayAccess, ArrayableInterface, JsonableInterface {
  20. /**
  21. * The connection name for the model.
  22. *
  23. * @var string
  24. */
  25. protected $connection;
  26. /**
  27. * The table associated with the model.
  28. *
  29. * @var string
  30. */
  31. protected $table;
  32. /**
  33. * The primary key for the model.
  34. *
  35. * @var string
  36. */
  37. protected $primaryKey = 'id';
  38. /**
  39. * The number of models to return for pagination.
  40. *
  41. * @var int
  42. */
  43. protected $perPage = 15;
  44. /**
  45. * Indicates if the IDs are auto-incrementing.
  46. *
  47. * @var bool
  48. */
  49. public $incrementing = true;
  50. /**
  51. * Indicates if the model should be timestamped.
  52. *
  53. * @var bool
  54. */
  55. public $timestamps = true;
  56. /**
  57. * The model's attributes.
  58. *
  59. * @var array
  60. */
  61. protected $attributes = array();
  62. /**
  63. * The model attribute's original state.
  64. *
  65. * @var array
  66. */
  67. protected $original = array();
  68. /**
  69. * The loaded relationships for the model.
  70. *
  71. * @var array
  72. */
  73. protected $relations = array();
  74. /**
  75. * The attributes that should be hidden for arrays.
  76. *
  77. * @var array
  78. */
  79. protected $hidden = array();
  80. /**
  81. * The attributes that should be visible in arrays.
  82. *
  83. * @var array
  84. */
  85. protected $visible = array();
  86. /**
  87. * The accessors to append to the model's array form.
  88. *
  89. * @var array
  90. */
  91. protected $appends = array();
  92. /**
  93. * The attributes that are mass assignable.
  94. *
  95. * @var array
  96. */
  97. protected $fillable = array();
  98. /**
  99. * The attributes that aren't mass assignable.
  100. *
  101. * @var array
  102. */
  103. protected $guarded = array('*');
  104. /**
  105. * The relationships that should be touched on save.
  106. *
  107. * @var array
  108. */
  109. protected $touches = array();
  110. /**
  111. * The relations to eager load on every query.
  112. *
  113. * @var array
  114. */
  115. protected $with = array();
  116. /**
  117. * Indicates if the model exists.
  118. *
  119. * @var bool
  120. */
  121. public $exists = false;
  122. /**
  123. * Indicates if the model should soft delete.
  124. *
  125. * @var bool
  126. */
  127. protected $softDelete = false;
  128. /**
  129. * Indicates whether attributes are snake cased on arrays.
  130. *
  131. * @var bool
  132. */
  133. public static $snakeAttributes = true;
  134. /**
  135. * The connection resolver instance.
  136. *
  137. * @var \Illuminate\Database\ConnectionResolverInterface
  138. */
  139. protected static $resolver;
  140. /**
  141. * The event dispatcher instance.
  142. *
  143. * @var \Illuminate\Events\Dispatcher
  144. */
  145. protected static $dispatcher;
  146. /**
  147. * The array of booted models.
  148. *
  149. * @var array
  150. */
  151. protected static $booted = array();
  152. /**
  153. * Indicates if all mass assignment is enabled.
  154. *
  155. * @var bool
  156. */
  157. protected static $unguarded = false;
  158. /**
  159. * The cache of the mutated attributes for each class.
  160. *
  161. * @var array
  162. */
  163. protected static $mutatorCache = array();
  164. /**
  165. * The name of the "created at" column.
  166. *
  167. * @var string
  168. */
  169. const CREATED_AT = 'created_at';
  170. /**
  171. * The name of the "updated at" column.
  172. *
  173. * @var string
  174. */
  175. const UPDATED_AT = 'updated_at';
  176. /**
  177. * The name of the "deleted at" column.
  178. *
  179. * @var string
  180. */
  181. const DELETED_AT = 'deleted_at';
  182. /**
  183. * Create a new Eloquent model instance.
  184. *
  185. * @param array $attributes
  186. * @return void
  187. */
  188. public function __construct(array $attributes = array())
  189. {
  190. if ( ! isset(static::$booted[get_class($this)]))
  191. {
  192. static::boot();
  193. static::$booted[get_class($this)] = true;
  194. }
  195. $this->fill($attributes);
  196. }
  197. /**
  198. * The "booting" method of the model.
  199. *
  200. * @return void
  201. */
  202. protected static function boot()
  203. {
  204. $class = get_called_class();
  205. static::$mutatorCache[$class] = array();
  206. // Here we will extract all of the mutated attributes so that we can quickly
  207. // spin through them after we export models to their array form, which we
  208. // need to be fast. This will let us always know the attributes mutate.
  209. foreach (get_class_methods($class) as $method)
  210. {
  211. if (preg_match('/^get(.+)Attribute$/', $method, $matches))
  212. {
  213. if (static::$snakeAttributes) $matches[1] = snake_case($matches[1]);
  214. static::$mutatorCache[$class][] = lcfirst($matches[1]);
  215. }
  216. }
  217. }
  218. /**
  219. * Register an observer with the Model.
  220. *
  221. * @param object $class
  222. * @return void
  223. */
  224. public static function observe($class)
  225. {
  226. $instance = new static;
  227. $className = get_class($class);
  228. // When registering a model observer, we will spin through the possible events
  229. // and determine if this observer has that method. If it does, we will hook
  230. // it into the model's event system, making it convenient to watch these.
  231. foreach ($instance->getObservableEvents() as $event)
  232. {
  233. if (method_exists($class, $event))
  234. {
  235. static::registerModelEvent($event, $className.'@'.$event);
  236. }
  237. }
  238. }
  239. /**
  240. * Fill the model with an array of attributes.
  241. *
  242. * @param array $attributes
  243. * @return \Illuminate\Database\Eloquent\Model|static
  244. */
  245. public function fill(array $attributes)
  246. {
  247. foreach ($attributes as $key => $value)
  248. {
  249. $key = $this->removeTableFromKey($key);
  250. // The developers may choose to place some attributes in the "fillable"
  251. // array, which means only those attributes may be set through mass
  252. // assignment to the model, and all others will just be ignored.
  253. if ($this->isFillable($key))
  254. {
  255. $this->setAttribute($key, $value);
  256. }
  257. elseif ($this->totallyGuarded())
  258. {
  259. throw new MassAssignmentException($key);
  260. }
  261. }
  262. return $this;
  263. }
  264. /**
  265. * Create a new instance of the given model.
  266. *
  267. * @param array $attributes
  268. * @param bool $exists
  269. * @return \Illuminate\Database\Eloquent\Model|static
  270. */
  271. public function newInstance($attributes = array(), $exists = false)
  272. {
  273. // This method just provides a convenient way for us to generate fresh model
  274. // instances of this current model. It is particularly useful during the
  275. // hydration of new objects via the Eloquent query builder instances.
  276. $model = new static((array) $attributes);
  277. $model->exists = $exists;
  278. return $model;
  279. }
  280. /**
  281. * Create a new model instance that is existing.
  282. *
  283. * @param array $attributes
  284. * @return \Illuminate\Database\Eloquent\Model|static
  285. */
  286. public function newFromBuilder($attributes = array())
  287. {
  288. $instance = $this->newInstance(array(), true);
  289. $instance->setRawAttributes((array) $attributes, true);
  290. return $instance;
  291. }
  292. /**
  293. * Save a new model and return the instance.
  294. *
  295. * @param array $attributes
  296. * @return \Illuminate\Database\Eloquent\Model|static
  297. */
  298. public static function create(array $attributes)
  299. {
  300. $model = new static($attributes);
  301. $model->save();
  302. return $model;
  303. }
  304. /**
  305. * Begin querying the model.
  306. *
  307. * @return \Illuminate\Database\Eloquent\Builder|static
  308. */
  309. public static function query()
  310. {
  311. return with(new static)->newQuery();
  312. }
  313. /**
  314. * Begin querying the model on a given connection.
  315. *
  316. * @param string $connection
  317. * @return \Illuminate\Database\Eloquent\Builder|static
  318. */
  319. public static function on($connection = null)
  320. {
  321. // First we will just create a fresh instance of this model, and then we can
  322. // set the connection on the model so that it is be used for the queries
  323. // we execute, as well as being set on each relationship we retrieve.
  324. $instance = new static;
  325. $instance->setConnection($connection);
  326. return $instance->newQuery();
  327. }
  328. /**
  329. * Get all of the models from the database.
  330. *
  331. * @param array $columns
  332. * @return \Illuminate\Database\Eloquent\Collection|static[]
  333. */
  334. public static function all($columns = array('*'))
  335. {
  336. $instance = new static;
  337. return $instance->newQuery()->get($columns);
  338. }
  339. /**
  340. * Find a model by its primary key.
  341. *
  342. * @param mixed $id
  343. * @param array $columns
  344. * @return \Illuminate\Database\Eloquent\Model|Collection|static
  345. */
  346. public static function find($id, $columns = array('*'))
  347. {
  348. $instance = new static;
  349. if (is_array($id))
  350. {
  351. return $instance->newQuery()->whereIn($instance->getKeyName(), $id)->get($columns);
  352. }
  353. return $instance->newQuery()->find($id, $columns);
  354. }
  355. /**
  356. * Find a model by its primary key or throw an exception.
  357. *
  358. * @param mixed $id
  359. * @param array $columns
  360. * @return \Illuminate\Database\Eloquent\Model|Collection|static
  361. */
  362. public static function findOrFail($id, $columns = array('*'))
  363. {
  364. if ( ! is_null($model = static::find($id, $columns))) return $model;
  365. throw new ModelNotFoundException;
  366. }
  367. /**
  368. * Eager load relations on the model.
  369. *
  370. * @param array|string $relations
  371. * @return void
  372. */
  373. public function load($relations)
  374. {
  375. if (is_string($relations)) $relations = func_get_args();
  376. $query = $this->newQuery()->with($relations);
  377. $query->eagerLoadRelations(array($this));
  378. }
  379. /**
  380. * Being querying a model with eager loading.
  381. *
  382. * @param array|string $relations
  383. * @return \Illuminate\Database\Eloquent\Builder|static
  384. */
  385. public static function with($relations)
  386. {
  387. if (is_string($relations)) $relations = func_get_args();
  388. $instance = new static;
  389. return $instance->newQuery()->with($relations);
  390. }
  391. /**
  392. * Define a one-to-one relationship.
  393. *
  394. * @param string $related
  395. * @param string $foreignKey
  396. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  397. */
  398. public function hasOne($related, $foreignKey = null)
  399. {
  400. $foreignKey = $foreignKey ?: $this->getForeignKey();
  401. $instance = new $related;
  402. return new HasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey);
  403. }
  404. /**
  405. * Define a polymorphic one-to-one relationship.
  406. *
  407. * @param string $related
  408. * @param string $name
  409. * @param string $type
  410. * @param string $id
  411. * @return \Illuminate\Database\Eloquent\Relations\MorphOne
  412. */
  413. public function morphOne($related, $name, $type = null, $id = null)
  414. {
  415. $instance = new $related;
  416. list($type, $id) = $this->getMorphs($name, $type, $id);
  417. $table = $instance->getTable();
  418. return new MorphOne($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id);
  419. }
  420. /**
  421. * Define an inverse one-to-one or many relationship.
  422. *
  423. * @param string $related
  424. * @param string $foreignKey
  425. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  426. */
  427. public function belongsTo($related, $foreignKey = null)
  428. {
  429. list(, $caller) = debug_backtrace(false);
  430. // If no foreign key was supplied, we can use a backtrace to guess the proper
  431. // foreign key name by using the name of the relationship function, which
  432. // when combined with an "_id" should conventionally match the columns.
  433. $relation = $caller['function'];
  434. if (is_null($foreignKey))
  435. {
  436. $foreignKey = snake_case($relation).'_id';
  437. }
  438. // Once we have the foreign key names, we'll just create a new Eloquent query
  439. // for the related models and returns the relationship instance which will
  440. // actually be responsible for retrieving and hydrating every relations.
  441. $instance = new $related;
  442. $query = $instance->newQuery();
  443. return new BelongsTo($query, $this, $foreignKey, $relation);
  444. }
  445. /**
  446. * Define an polymorphic, inverse one-to-one or many relationship.
  447. *
  448. * @param string $name
  449. * @param string $type
  450. * @param string $id
  451. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  452. */
  453. public function morphTo($name = null, $type = null, $id = null)
  454. {
  455. // If no name is provided, we will use the backtrace to get the function name
  456. // since that is most likely the name of the polymorphic interface. We can
  457. // use that to get both the class and foreign key that will be utilized.
  458. if (is_null($name))
  459. {
  460. list(, $caller) = debug_backtrace(false);
  461. $name = snake_case($caller['function']);
  462. }
  463. // Next we will guess the type and ID if necessary. The type and IDs may also
  464. // be passed into the function so that the developers may manually specify
  465. // them on the relations. Otherwise, we will just make a great estimate.
  466. list($type, $id) = $this->getMorphs($name, $type, $id);
  467. $class = $this->$type;
  468. return $this->belongsTo($class, $id);
  469. }
  470. /**
  471. * Define a one-to-many relationship.
  472. *
  473. * @param string $related
  474. * @param string $foreignKey
  475. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  476. */
  477. public function hasMany($related, $foreignKey = null)
  478. {
  479. $foreignKey = $foreignKey ?: $this->getForeignKey();
  480. $instance = new $related;
  481. return new HasMany($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey);
  482. }
  483. /**
  484. * Define a polymorphic one-to-many relationship.
  485. *
  486. * @param string $related
  487. * @param string $name
  488. * @param string $type
  489. * @param string $id
  490. * @return \Illuminate\Database\Eloquent\Relations\MorphMany
  491. */
  492. public function morphMany($related, $name, $type = null, $id = null)
  493. {
  494. $instance = new $related;
  495. list($type, $id) = $this->getMorphs($name, $type, $id);
  496. $table = $instance->getTable();
  497. return new MorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id);
  498. }
  499. /**
  500. * Define a many-to-many relationship.
  501. *
  502. * @param string $related
  503. * @param string $table
  504. * @param string $foreignKey
  505. * @param string $otherKey
  506. * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  507. */
  508. public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null)
  509. {
  510. $caller = $this->getBelongsToManyCaller();
  511. // First, we'll need to determine the foreign key and "other key" for the
  512. // relationship. Once we have determined the keys we'll make the query
  513. // instances as well as the relationship instances we need for this.
  514. $foreignKey = $foreignKey ?: $this->getForeignKey();
  515. $instance = new $related;
  516. $otherKey = $otherKey ?: $instance->getForeignKey();
  517. // If no table name was provided, we can guess it by concatenating the two
  518. // models using underscores in alphabetical order. The two model names
  519. // are transformed to snake case from their default CamelCase also.
  520. if (is_null($table))
  521. {
  522. $table = $this->joiningTable($related);
  523. }
  524. // Now we're ready to create a new query builder for the related model and
  525. // the relationship instances for the relation. The relations will set
  526. // appropriate query constraint and entirely manages the hydrations.
  527. $query = $instance->newQuery();
  528. return new BelongsToMany($query, $this, $table, $foreignKey, $otherKey, $caller['function']);
  529. }
  530. /**
  531. * Get the relationship name of the belongs to many.
  532. *
  533. * @return string
  534. */
  535. protected function getBelongsToManyCaller()
  536. {
  537. $self = __FUNCTION__;
  538. return array_first(debug_backtrace(false), function($trace) use ($self)
  539. {
  540. $caller = $trace['function'];
  541. return $caller != 'belongsToMany' and $caller != $self;
  542. });
  543. }
  544. /**
  545. * Get the joining table name for a many-to-many relation.
  546. *
  547. * @param string $related
  548. * @return string
  549. */
  550. public function joiningTable($related)
  551. {
  552. // The joining table name, by convention, is simply the snake cased models
  553. // sorted alphabetically and concatenated with an underscore, so we can
  554. // just sort the models and join them together to get the table name.
  555. $base = snake_case(class_basename($this));
  556. $related = snake_case(class_basename($related));
  557. $models = array($related, $base);
  558. // Now that we have the model names in an array we can just sort them and
  559. // use the implode function to join them together with an underscores,
  560. // which is typically used by convention within the database system.
  561. sort($models);
  562. return strtolower(implode('_', $models));
  563. }
  564. /**
  565. * Destroy the models for the given IDs.
  566. *
  567. * @param array|int $ids
  568. * @return void
  569. */
  570. public static function destroy($ids)
  571. {
  572. $ids = is_array($ids) ? $ids : func_get_args();
  573. $instance = new static;
  574. // We will actually pull the models from the database table and call delete on
  575. // each of them individually so that their events get fired properly with a
  576. // correct set of attributes in case the developers wants to check these.
  577. $key = $instance->getKeyName();
  578. foreach ($instance->whereIn($key, $ids)->get() as $model)
  579. {
  580. $model->delete();
  581. }
  582. }
  583. /**
  584. * Delete the model from the database.
  585. *
  586. * @return bool|null
  587. */
  588. public function delete()
  589. {
  590. if ($this->exists)
  591. {
  592. if ($this->fireModelEvent('deleting') === false) return false;
  593. // Here, we'll touch the owning models, verifying these timestamps get updated
  594. // for the models. This will allow any caching to get broken on the parents
  595. // by the timestamp. Then we will go ahead and delete the model instance.
  596. $this->touchOwners();
  597. $this->performDeleteOnModel();
  598. $this->exists = false;
  599. // Once the model has been deleted, we will fire off the deleted event so that
  600. // the developers may hook into post-delete operations. We will then return
  601. // a boolean true as the delete is presumably successful on the database.
  602. $this->fireModelEvent('deleted', false);
  603. return true;
  604. }
  605. }
  606. /**
  607. * Force a hard delete on a soft deleted model.
  608. *
  609. * @return void
  610. */
  611. public function forceDelete()
  612. {
  613. $softDelete = $this->softDelete;
  614. // We will temporarily disable false delete to allow us to perform the real
  615. // delete operation against the model. We will then restore the deleting
  616. // state to what this was prior to this given hard deleting operation.
  617. $this->softDelete = false;
  618. $this->delete();
  619. $this->softDelete = $softDelete;
  620. }
  621. /**
  622. * Perform the actual delete query on this model instance.
  623. *
  624. * @return void
  625. */
  626. protected function performDeleteOnModel()
  627. {
  628. $query = $this->newQuery()->where($this->getKeyName(), $this->getKey());
  629. if ($this->softDelete)
  630. {
  631. $this->{static::DELETED_AT} = $time = $this->freshTimestamp();
  632. $query->update(array(static::DELETED_AT => $this->fromDateTime($time)));
  633. }
  634. else
  635. {
  636. $query->delete();
  637. }
  638. }
  639. /**
  640. * Restore a soft-deleted model instance.
  641. *
  642. * @return bool|null
  643. */
  644. public function restore()
  645. {
  646. if ($this->softDelete)
  647. {
  648. // If the restoring event does not return false, we will proceed with this
  649. // restore operation. Otherwise, we bail out so the developer will stop
  650. // the restore totally. We will clear the deleted timestamp and save.
  651. if ($this->fireModelEvent('restoring') === false)
  652. {
  653. return false;
  654. }
  655. $this->{static::DELETED_AT} = null;
  656. // Once we have saved the model, we will fire the "restored" event so this
  657. // developer will do anything they need to after a restore operation is
  658. // totally finished. Then we will return the result of the save call.
  659. $result = $this->save();
  660. $this->fireModelEvent('restored', false);
  661. return $result;
  662. }
  663. }
  664. /**
  665. * Register a saving model event with the dispatcher.
  666. *
  667. * @param \Closure|string $callback
  668. * @return void
  669. */
  670. public static function saving($callback)
  671. {
  672. static::registerModelEvent('saving', $callback);
  673. }
  674. /**
  675. * Register a saved model event with the dispatcher.
  676. *
  677. * @param \Closure|string $callback
  678. * @return void
  679. */
  680. public static function saved($callback)
  681. {
  682. static::registerModelEvent('saved', $callback);
  683. }
  684. /**
  685. * Register an updating model event with the dispatcher.
  686. *
  687. * @param \Closure|string $callback
  688. * @return void
  689. */
  690. public static function updating($callback)
  691. {
  692. static::registerModelEvent('updating', $callback);
  693. }
  694. /**
  695. * Register an updated model event with the dispatcher.
  696. *
  697. * @param \Closure|string $callback
  698. * @return void
  699. */
  700. public static function updated($callback)
  701. {
  702. static::registerModelEvent('updated', $callback);
  703. }
  704. /**
  705. * Register a creating model event with the dispatcher.
  706. *
  707. * @param \Closure|string $callback
  708. * @return void
  709. */
  710. public static function creating($callback)
  711. {
  712. static::registerModelEvent('creating', $callback);
  713. }
  714. /**
  715. * Register a created model event with the dispatcher.
  716. *
  717. * @param \Closure|string $callback
  718. * @return void
  719. */
  720. public static function created($callback)
  721. {
  722. static::registerModelEvent('created', $callback);
  723. }
  724. /**
  725. * Register a deleting model event with the dispatcher.
  726. *
  727. * @param \Closure|string $callback
  728. * @return void
  729. */
  730. public static function deleting($callback)
  731. {
  732. static::registerModelEvent('deleting', $callback);
  733. }
  734. /**
  735. * Register a deleted model event with the dispatcher.
  736. *
  737. * @param \Closure|string $callback
  738. * @return void
  739. */
  740. public static function deleted($callback)
  741. {
  742. static::registerModelEvent('deleted', $callback);
  743. }
  744. /**
  745. * Register a restoring model event with the dispatcher.
  746. *
  747. * @param \Closure|string $callback
  748. * @return void
  749. */
  750. public static function restoring($callback)
  751. {
  752. static::registerModelEvent('restoring', $callback);
  753. }
  754. /**
  755. * Register a restored model event with the dispatcher.
  756. *
  757. * @param \Closure|string $callback
  758. * @return void
  759. */
  760. public static function restored($callback)
  761. {
  762. static::registerModelEvent('restored', $callback);
  763. }
  764. /**
  765. * Remove all of the event listeners for the model.
  766. *
  767. * @return void
  768. */
  769. public static function flushEventListeners()
  770. {
  771. if ( ! isset(static::$dispatcher)) return;
  772. $instance = new static;
  773. foreach ($instance->getObservableEvents() as $event)
  774. {
  775. static::$dispatcher->forget("eloquent.{$event}: ".get_called_class());
  776. }
  777. }
  778. /**
  779. * Register a model event with the dispatcher.
  780. *
  781. * @param string $event
  782. * @param \Closure|string $callback
  783. * @return void
  784. */
  785. protected static function registerModelEvent($event, $callback)
  786. {
  787. if (isset(static::$dispatcher))
  788. {
  789. $name = get_called_class();
  790. static::$dispatcher->listen("eloquent.{$event}: {$name}", $callback);
  791. }
  792. }
  793. /**
  794. * Get the observable event names.
  795. *
  796. * @return array
  797. */
  798. public function getObservableEvents()
  799. {
  800. return array(
  801. 'creating', 'created', 'updating', 'updated',
  802. 'deleting', 'deleted', 'saving', 'saved',
  803. 'restoring', 'restored',
  804. );
  805. }
  806. /**
  807. * Increment a column's value by a given amount.
  808. *
  809. * @param string $column
  810. * @param int $amount
  811. * @return int
  812. */
  813. protected function increment($column, $amount = 1)
  814. {
  815. return $this->incrementOrDecrement($column, $amount, 'increment');
  816. }
  817. /**
  818. * Decrement a column's value by a given amount.
  819. *
  820. * @param string $column
  821. * @param int $amount
  822. * @return int
  823. */
  824. protected function decrement($column, $amount = 1)
  825. {
  826. return $this->incrementOrDecrement($column, $amount, 'decrement');
  827. }
  828. /**
  829. * Run the increment or decrement method on the model.
  830. *
  831. * @param string $column
  832. * @param int $amount
  833. * @param string $method
  834. * @return int
  835. */
  836. protected function incrementOrDecrement($column, $amount, $method)
  837. {
  838. $query = $this->newQuery();
  839. if ( ! $this->exists)
  840. {
  841. return $query->{$method}($column, $amount);
  842. }
  843. return $query->where($this->getKeyName(), $this->getKey())->{$method}($column, $amount);
  844. }
  845. /**
  846. * Update the model in the database.
  847. *
  848. * @param array $attributes
  849. * @return mixed
  850. */
  851. public function update(array $attributes = array())
  852. {
  853. if ( ! $this->exists)
  854. {
  855. return $this->newQuery()->update($attributes);
  856. }
  857. return $this->fill($attributes)->save();
  858. }
  859. /**
  860. * Save the model and all of its relationships.
  861. *
  862. * @return bool
  863. */
  864. public function push()
  865. {
  866. if ( ! $this->save()) return false;
  867. // To sync all of the relationships to the database, we will simply spin through
  868. // the relationships and save each model via this "push" method, which allows
  869. // us to recurse into all of these nested relations for the model instance.
  870. foreach ($this->relations as $models)
  871. {
  872. foreach (Collection::make($models) as $model)
  873. {
  874. if ( ! $model->push()) return false;
  875. }
  876. }
  877. return true;
  878. }
  879. /**
  880. * Save the model to the database.
  881. *
  882. * @param array $options
  883. * @return bool
  884. */
  885. public function save(array $options = array())
  886. {
  887. $query = $this->newQueryWithDeleted();
  888. // If the "saving" event returns false we'll bail out of the save and return
  889. // false, indicating that the save failed. This gives an opportunities to
  890. // listeners to cancel save operations if validations fail or whatever.
  891. if ($this->fireModelEvent('saving') === false)
  892. {
  893. return false;
  894. }
  895. // If the model already exists in the database we can just update our record
  896. // that is already in this database using the current IDs in this "where"
  897. // clause to only update this model. Otherwise, we'll just insert them.
  898. if ($this->exists)
  899. {
  900. $saved = $this->performUpdate($query);
  901. }
  902. // If the model is brand new, we'll insert it into our database and set the
  903. // ID attribute on the model to the value of the newly inserted row's ID
  904. // which is typically an auto-increment value managed by the database.
  905. else
  906. {
  907. $saved = $this->performInsert($query);
  908. }
  909. if ($saved) $this->finishSave($options);
  910. return $saved;
  911. }
  912. /**
  913. * Finish processing on a successful save operation.
  914. *
  915. * @return void
  916. */
  917. protected function finishSave(array $options)
  918. {
  919. $this->syncOriginal();
  920. $this->fireModelEvent('saved', false);
  921. if (array_get($options, 'touch', true)) $this->touchOwners();
  922. }
  923. /**
  924. * Perform a model update operation.
  925. *
  926. * @param \Illuminate\Database\Eloquent\Builder
  927. * @return bool
  928. */
  929. protected function performUpdate($query)
  930. {
  931. $dirty = $this->getDirty();
  932. if (count($dirty) > 0)
  933. {
  934. // If the updating event returns false, we will cancel the update operation so
  935. // developers can hook Validation systems into their models and cancel this
  936. // operation if the model does not pass validation. Otherwise, we update.
  937. if ($this->fireModelEvent('updating') === false)
  938. {
  939. return false;
  940. }
  941. // First we need to create a fresh query instance and touch the creation and
  942. // update timestamp on the model which are maintained by us for developer
  943. // convenience. Then we will just continue saving the model instances.
  944. if ($this->timestamps)
  945. {
  946. $this->updateTimestamps();
  947. $dirty = $this->getDirty();
  948. }
  949. // Once we have run the update operation, we will fire the "updated" event for
  950. // this model instance. This will allow developers to hook into these after
  951. // models are updated, giving them a chance to do any special processing.
  952. $this->setKeysForSaveQuery($query)->update($dirty);
  953. $this->fireModelEvent('updated', false);
  954. }
  955. return true;
  956. }
  957. /**
  958. * Perform a model insert operation.
  959. *
  960. * @param \Illuminate\Database\Eloquent\Builder
  961. * @return bool
  962. */
  963. protected function performInsert($query)
  964. {
  965. if ($this->fireModelEvent('creating') === false) return false;
  966. // First we'll need to create a fresh query instance and touch the creation and
  967. // update timestamps on this model, which are maintained by us for developer
  968. // convenience. After, we will just continue saving these model instances.
  969. if ($this->timestamps)
  970. {
  971. $this->updateTimestamps();
  972. }
  973. // If the model has an incrementing key, we can use the "insertGetId" method on
  974. // the query builder, which will give us back the final inserted ID for this
  975. // table from the database. Not all tables have to be incrementing though.
  976. $attributes = $this->attributes;
  977. if ($this->incrementing)
  978. {
  979. $this->insertAndSetId($query, $attributes);
  980. }
  981. // If the table is not incrementing we'll simply insert this attributes as they
  982. // are, as this attributes arrays must contain an "id" column already placed
  983. // there by the developer as the manually determined key for these models.
  984. else
  985. {
  986. $query->insert($attributes);
  987. }
  988. // We will go ahead and set the exists property to true, so that it is set when
  989. // the created event is fired, just in case the developer tries to update it
  990. // during the event. This will allow them to do so and run an update here.
  991. $this->exists = true;
  992. $this->fireModelEvent('created', false);
  993. return true;
  994. }
  995. /**
  996. * Insert the given attributes and set the ID on the model.
  997. *
  998. * @param \Illuminate\Database\Eloquent\Builder $query
  999. * @param array $attributes
  1000. * @return void
  1001. */
  1002. protected function insertAndSetId($query, $attributes)
  1003. {
  1004. $id = $query->insertGetId($attributes, $keyName = $this->getKeyName());
  1005. $this->setAttribute($keyName, $id);
  1006. }
  1007. /**
  1008. * Touch the owning relations of the model.
  1009. *
  1010. * @return void
  1011. */
  1012. public function touchOwners()
  1013. {
  1014. foreach ($this->touches as $relation)
  1015. {
  1016. $this->$relation()->touch();
  1017. }
  1018. }
  1019. /**
  1020. * Determine if the model touches a given relation.
  1021. *
  1022. * @param string $relation
  1023. * @return bool
  1024. */
  1025. public function touches($relation)
  1026. {
  1027. return in_array($relation, $this->touches);
  1028. }
  1029. /**
  1030. * Fire the given event for the model.
  1031. *
  1032. * @param string $event
  1033. * @param bool $halt
  1034. * @return mixed
  1035. */
  1036. protected function fireModelEvent($event, $halt = true)
  1037. {
  1038. if ( ! isset(static::$dispatcher)) return true;
  1039. // We will append the names of the class to the event to distinguish it from
  1040. // other model events that are fired, allowing us to listen on each model
  1041. // event set individually instead of catching event for all the models.
  1042. $event = "eloquent.{$event}: ".get_class($this);
  1043. $method = $halt ? 'until' : 'fire';
  1044. return static::$dispatcher->$method($event, $this);
  1045. }
  1046. /**
  1047. * Set the keys for a save update query.
  1048. *
  1049. * @param \Illuminate\Database\Eloquent\Builder
  1050. * @return \Illuminate\Database\Eloquent\Builder
  1051. */
  1052. protected function setKeysForSaveQuery($query)
  1053. {
  1054. $query->where($this->getKeyName(), '=', $this->getKey());
  1055. return $query;
  1056. }
  1057. /**
  1058. * Update the model's update timestamp.
  1059. *
  1060. * @return bool
  1061. */
  1062. public function touch()
  1063. {
  1064. $this->updateTimestamps();
  1065. return $this->save();
  1066. }
  1067. /**
  1068. * Update the creation and update timestamps.
  1069. *
  1070. * @return void
  1071. */
  1072. protected function updateTimestamps()
  1073. {
  1074. $time = $this->freshTimestamp();
  1075. if ( ! $this->isDirty(static::UPDATED_AT))
  1076. {
  1077. $this->setUpdatedAt($time);
  1078. }
  1079. if ( ! $this->exists and ! $this->isDirty(static::CREATED_AT))
  1080. {
  1081. $this->setCreatedAt($time);
  1082. }
  1083. }
  1084. /**
  1085. * Set the value of the "created at" attribute.
  1086. *
  1087. * @param mixed $value
  1088. * @return void
  1089. */
  1090. public function setCreatedAt($value)
  1091. {
  1092. $this->{static::CREATED_AT} = $value;
  1093. }
  1094. /**
  1095. * Set the value of the "updated at" attribute.
  1096. *
  1097. * @param mixed $value
  1098. * @return void
  1099. */
  1100. public function setUpdatedAt($value)
  1101. {
  1102. $this->{static::UPDATED_AT} = $value;
  1103. }
  1104. /**
  1105. * Get the name of the "created at" column.
  1106. *
  1107. * @return string
  1108. */
  1109. public function getCreatedAtColumn()
  1110. {
  1111. return static::CREATED_AT;
  1112. }
  1113. /**
  1114. * Get the name of the "updated at" column.
  1115. *
  1116. * @return string
  1117. */
  1118. public function getUpdatedAtColumn()
  1119. {
  1120. return static::UPDATED_AT;
  1121. }
  1122. /**
  1123. * Get the name of the "deleted at" column.
  1124. *
  1125. * @return string
  1126. */
  1127. public function getDeletedAtColumn()
  1128. {
  1129. return static::DELETED_AT;
  1130. }
  1131. /**
  1132. * Get the fully qualified "deleted at" column.
  1133. *
  1134. * @return string
  1135. */
  1136. public function getQualifiedDeletedAtColumn()
  1137. {
  1138. return $this->getTable().'.'.$this->getDeletedAtColumn();
  1139. }
  1140. /**
  1141. * Get a fresh timestamp for the model.
  1142. *
  1143. * @return DateTime
  1144. */
  1145. public function freshTimestamp()
  1146. {
  1147. return new DateTime;
  1148. }
  1149. /**
  1150. * Get a fresh timestamp for the model.
  1151. *
  1152. * @return DateTime
  1153. */
  1154. public function freshTimestampString()
  1155. {
  1156. return $this->fromDateTime($this->freshTimestamp());
  1157. }
  1158. /**
  1159. * Get a new query builder for the model's table.
  1160. *
  1161. * @param bool $excludeDeleted
  1162. * @return \Illuminate\Database\Eloquent\Builder|static
  1163. */
  1164. public function newQuery($excludeDeleted = true)
  1165. {
  1166. $builder = new Builder($this->newBaseQueryBuilder());
  1167. // Once we have the query builders, we will set the model instances so the
  1168. // builder can easily access any information it may need from the model
  1169. // while it is constructing and executing various queries against it.
  1170. $builder->setModel($this)->with($this->with);
  1171. if ($excludeDeleted and $this->softDelete)
  1172. {
  1173. $builder->whereNull($this->getQualifiedDeletedAtColumn());
  1174. }
  1175. return $builder;
  1176. }
  1177. /**
  1178. * Get a new query builder that includes soft deletes.
  1179. *
  1180. * @return \Illuminate\Database\Eloquent\Builder|static
  1181. */
  1182. public function newQueryWithDeleted()
  1183. {
  1184. return $this->newQuery(false);
  1185. }
  1186. /**
  1187. * Determine if the model instance has been soft-deleted.
  1188. *
  1189. * @return bool
  1190. */
  1191. public function trashed()
  1192. {
  1193. return $this->softDelete and ! is_null($this->{static::DELETED_AT});
  1194. }
  1195. /**
  1196. * Get a new query builder that includes soft deletes.
  1197. *
  1198. * @return \Illuminate\Database\Eloquent\Builder|static
  1199. */
  1200. public static function withTrashed()
  1201. {
  1202. return with(new static)->newQueryWithDeleted();
  1203. }
  1204. /**
  1205. * Get a new query builder that only includes soft deletes.
  1206. *
  1207. * @return \Illuminate\Database\Eloquent\Builder|static
  1208. */
  1209. public static function onlyTrashed()
  1210. {
  1211. $instance = new static;
  1212. $column = $instance->getQualifiedDeletedAtColumn();
  1213. return $instance->newQueryWithDeleted()->whereNotNull($column);
  1214. }
  1215. /**
  1216. * Get a new query builder instance for the connection.
  1217. *
  1218. * @return \Illuminate\Database\Query\Builder
  1219. */
  1220. protected function newBaseQueryBuilder()
  1221. {
  1222. $conn = $this->getConnection();
  1223. $grammar = $conn->getQueryGrammar();
  1224. return new QueryBuilder($conn, $grammar, $conn->getPostProcessor());
  1225. }
  1226. /**
  1227. * Create a new Eloquent Collection instance.
  1228. *
  1229. * @param array $models
  1230. * @return \Illuminate\Database\Eloquent\Collection
  1231. */
  1232. public function newCollection(array $models = array())
  1233. {
  1234. return new Collection($models);
  1235. }
  1236. /**
  1237. * Get the table associated with the model.
  1238. *
  1239. * @return string
  1240. */
  1241. public function getTable()
  1242. {
  1243. if (isset($this->table)) return $this->table;
  1244. return str_replace('\\', '', snake_case(str_plural(class_basename($this))));
  1245. }
  1246. /**
  1247. * Set the table associated with the model.
  1248. *
  1249. * @param string $table
  1250. * @return void
  1251. */
  1252. public function setTable($table)
  1253. {
  1254. $this->table = $table;
  1255. }
  1256. /**
  1257. * Get the value of the model's primary key.
  1258. *
  1259. * @return mixed
  1260. */
  1261. public function getKey()
  1262. {
  1263. return $this->getAttribute($this->getKeyName());
  1264. }
  1265. /**
  1266. * Get the primary key for the model.
  1267. *
  1268. * @return string
  1269. */
  1270. public function getKeyName()
  1271. {
  1272. return $this->primaryKey;
  1273. }
  1274. /**
  1275. * Get the table qualified key name.
  1276. *
  1277. * @return string
  1278. */
  1279. public function getQualifiedKeyName()
  1280. {
  1281. return $this->getTable().'.'.$this->getKeyName();
  1282. }
  1283. /**
  1284. * Determine if the model uses timestamps.
  1285. *
  1286. * @return bool
  1287. */
  1288. public function usesTimestamps()
  1289. {
  1290. return $this->timestamps;
  1291. }
  1292. /**
  1293. * Determine if the model instance uses soft deletes.
  1294. *
  1295. * @return bool
  1296. */
  1297. public function isSoftDeleting()
  1298. {
  1299. return $this->softDelete;
  1300. }
  1301. /**
  1302. * Set the soft deleting property on the model.
  1303. *
  1304. * @param bool $enabled
  1305. * @return void
  1306. */
  1307. public function setSoftDeleting($enabled)
  1308. {
  1309. $this->softDelete = $enabled;
  1310. }
  1311. /**
  1312. * Get the polymorphic relationship columns.
  1313. *
  1314. * @param string $name
  1315. * @param string $type
  1316. * @param string $id
  1317. * @return array
  1318. */
  1319. protected function getMorphs($name, $type, $id)
  1320. {
  1321. $type = $type ?: $name.'_type';
  1322. $id = $id ?: $name.'_id';
  1323. return array($type, $id);
  1324. }
  1325. /**
  1326. * Get the number of models to return per page.
  1327. *
  1328. * @return int
  1329. */
  1330. public function getPerPage()
  1331. {
  1332. return $this->perPage;
  1333. }
  1334. /**
  1335. * Set the number of models ot return per page.
  1336. *
  1337. * @param int $perPage
  1338. * @return void
  1339. */
  1340. public function setPerPage($perPage)
  1341. {
  1342. $this->perPage = $perPage;
  1343. }
  1344. /**
  1345. * Get the default foreign key name for the model.
  1346. *
  1347. * @return string
  1348. */
  1349. public function getForeignKey()
  1350. {
  1351. return snake_case(class_basename($this)).'_id';
  1352. }
  1353. /**
  1354. * Get the hidden attributes for the model.
  1355. *
  1356. * @return array
  1357. */
  1358. public function getHidden()
  1359. {
  1360. return $this->hidden;
  1361. }
  1362. /**
  1363. * Set the hidden attributes for the model.
  1364. *
  1365. * @param array $hidden
  1366. * @return void
  1367. */
  1368. public function setHidden(array $hidden)
  1369. {
  1370. $this->hidden = $hidden;
  1371. }
  1372. /**
  1373. * Set the visible attributes for the model.
  1374. *
  1375. * @param array $visible
  1376. * @return void
  1377. */
  1378. public function setVisible(array $visible)
  1379. {
  1380. $this->visible = $visible;
  1381. }
  1382. /**
  1383. * Set the accessors to append to model arrays.
  1384. *
  1385. * @param array $appends
  1386. * @return void
  1387. */
  1388. public function setAppends(array $appends)
  1389. {
  1390. $this->appends = $appends;
  1391. }
  1392. /**
  1393. * Get the fillable attributes for the model.
  1394. *
  1395. * @return array
  1396. */
  1397. public function getFillable()
  1398. {
  1399. return $this->fillable;
  1400. }
  1401. /**
  1402. * Set the fillable attributes for the model.
  1403. *
  1404. * @param array $fillable
  1405. * @return \Illuminate\Database\Eloquent\Model
  1406. */
  1407. public function fillable(array $fillable)
  1408. {
  1409. $this->fillable = $fillable;
  1410. return $this;
  1411. }
  1412. /**
  1413. * Set the guarded attributes for the model.
  1414. *
  1415. * @param array $guarded
  1416. * @return \Illuminate\Database\Eloquent\Model
  1417. */
  1418. public function guard(array $guarded)
  1419. {
  1420. $this->guarded = $guarded;
  1421. return $this;
  1422. }
  1423. /**
  1424. * Disable all mass assignable restrictions.
  1425. *
  1426. * @return void
  1427. */
  1428. public static function unguard()
  1429. {
  1430. static::$unguarded = true;
  1431. }
  1432. /**
  1433. * Enable the mass assignment restrictions.
  1434. *
  1435. * @return void
  1436. */
  1437. public static function reguard()
  1438. {
  1439. static::$unguarded = false;
  1440. }
  1441. /**
  1442. * Set "unguard" to a given state.
  1443. *
  1444. * @param bool $state
  1445. * @return void
  1446. */
  1447. public static function setUnguardState($state)
  1448. {
  1449. static::$unguarded = $state;
  1450. }
  1451. /**
  1452. * Determine if the given attribute may be mass assigned.
  1453. *
  1454. * @param string $key
  1455. * @return bool
  1456. */
  1457. public function isFillable($key)
  1458. {
  1459. if (static::$unguarded) return true;
  1460. // If the key is in the "fillable" array, we can of course assume tha it is
  1461. // a fillable attribute. Otherwise, we will check the guarded array when
  1462. // we need to determine if the attribute is black-listed on the model.
  1463. if (in_array($key, $this->fillable)) return true;
  1464. if ($this->isGuarded($key)) return false;
  1465. return empty($this->fillable) and ! starts_with($key, '_');
  1466. }
  1467. /**
  1468. * Determine if the given key is guarded.
  1469. *
  1470. * @param string $key
  1471. * @return bool
  1472. */
  1473. public function isGuarded($key)
  1474. {
  1475. return in_array($key, $this->guarded) or $this->guarded == array('*');
  1476. }
  1477. /**
  1478. * Determine if the model is totally guarded.
  1479. *
  1480. * @return bool
  1481. */
  1482. public function totallyGuarded()
  1483. {
  1484. return count($this->fillable) == 0 and $this->guarded == array('*');
  1485. }
  1486. /**
  1487. * Remove the table name from a given key.
  1488. *
  1489. * @param string $key
  1490. * @return string
  1491. */
  1492. protected function removeTableFromKey($key)
  1493. {
  1494. if ( ! str_contains($key, '.')) return $key;
  1495. return last(explode('.', $key));
  1496. }
  1497. /**
  1498. * Get the relationships that are touched on save.
  1499. *
  1500. * @return array
  1501. */
  1502. public function getTouchedRelations()
  1503. {
  1504. return $this->touches;
  1505. }
  1506. /**
  1507. * Set the relationships that are touched on save.
  1508. *
  1509. * @param array $touches
  1510. * @return void
  1511. */
  1512. public function setTouchedRelations(array $touches)
  1513. {
  1514. $this->touches = $touches;
  1515. }
  1516. /**
  1517. * Get the value indicating whether the IDs are incrementing.
  1518. *
  1519. * @return bool
  1520. */
  1521. public function getIncrementing()
  1522. {
  1523. return $this->incrementing;
  1524. }
  1525. /**
  1526. * Set whether IDs are incrementing.
  1527. *
  1528. * @param bool $value
  1529. * @return void
  1530. */
  1531. public function setIncrementing($value)
  1532. {
  1533. $this->incrementing = $value;
  1534. }
  1535. /**
  1536. * Convert the model instance to JSON.
  1537. *
  1538. * @param int $options
  1539. * @return string
  1540. */
  1541. public function toJson($options = 0)
  1542. {
  1543. return json_encode($this->toArray(), $options);
  1544. }
  1545. /**
  1546. * Convert the model instance to an array.
  1547. *
  1548. * @return array
  1549. */
  1550. public function toArray()
  1551. {
  1552. $attributes = $this->attributesToArray();
  1553. return array_merge($attributes, $this->relationsToArray());
  1554. }
  1555. /**
  1556. * Convert the model's attributes to an array.
  1557. *
  1558. * @return array
  1559. */
  1560. public function attributesToArray()
  1561. {
  1562. $attributes = $this->getArrayableAttributes();
  1563. // We want to spin through all the mutated attributes for this model and call
  1564. // the mutator for the attribute. We cache off every mutated attributes so
  1565. // we don't have to constantly check on attributes that actually change.
  1566. foreach ($this->getMutatedAttributes() as $key)
  1567. {
  1568. if ( ! array_key_exists($key, $attributes)) continue;
  1569. $attributes[$key] = $this->mutateAttribute(
  1570. $key, $attributes[$key]
  1571. );
  1572. }
  1573. // Here we will grab all of the appended, calculated attributes to this model
  1574. // as these attributes are not really in the attributes array, but are run
  1575. // when we need to array or JSON the model for convenience to the coder.
  1576. foreach ($this->appends as $key)
  1577. {
  1578. $attributes[$key] = $this->mutateAttribute($key, null);
  1579. }
  1580. return $attributes;
  1581. }
  1582. /**
  1583. * Get an attribute array of all arrayable attributes.
  1584. *
  1585. * @return array
  1586. */
  1587. protected function getArrayableAttributes()
  1588. {
  1589. return $this->getArrayableItems($this->attributes);
  1590. }
  1591. /**
  1592. * Get the model's relationships in array form.
  1593. *
  1594. * @return array
  1595. */
  1596. public function relationsToArray()
  1597. {
  1598. $attributes = array();
  1599. foreach ($this->getArrayableRelations() as $key => $value)
  1600. {
  1601. if (in_array($key, $this->hidden)) continue;
  1602. // If the values implements the Arrayable interface we can just call this
  1603. // toArray method on the instances which will convert both models and
  1604. // collections to their proper array form and we'll set the values.
  1605. if ($value instanceof ArrayableInterface)
  1606. {
  1607. $relation = $value->toArray();
  1608. }
  1609. // If the value is null, we'll still go ahead and set it in this list of
  1610. // attributes since null is used to represent empty relationships if
  1611. // if it a has one or belongs to type relationships on the models.
  1612. elseif (is_null($value))
  1613. {
  1614. $relation = $value;
  1615. }
  1616. // If the relationships snake-casing is enabled, we will snake case this
  1617. // key so that the relation attribute is snake cased in this returned
  1618. // array to the developer, making this consisntent with attributes.
  1619. if (static::$snakeAttributes)
  1620. {
  1621. $key = snake_case($key);
  1622. }
  1623. // If the relation value has been set, we will set it on this attributes
  1624. // list for returning. If it was not arrayable or null, we'll not set
  1625. // the value on the array because it is some type of invalid value.
  1626. if (isset($relation) or is_null($value))
  1627. {
  1628. $attributes[$key] = $relation;
  1629. }
  1630. }
  1631. return $attributes;
  1632. }
  1633. /**
  1634. * Get an attribute array of all arrayable relations.
  1635. *
  1636. * @return array
  1637. */
  1638. protected function getArrayableRelations()
  1639. {
  1640. return $this->getArrayableItems($this->relations);
  1641. }
  1642. /**
  1643. * Get an attribute array of all arrayable values.
  1644. *
  1645. * @param array $values
  1646. * @return array
  1647. */
  1648. protected function getArrayableItems(array $values)
  1649. {
  1650. if (count($this->visible) > 0)
  1651. {
  1652. return array_intersect_key($values, array_flip($this->visible));
  1653. }
  1654. return array_diff_key($values, array_flip($this->hidden));
  1655. }
  1656. /**
  1657. * Get an attribute from the model.
  1658. *
  1659. * @param string $key
  1660. * @return mixed
  1661. */
  1662. public function getAttribute($key)
  1663. {
  1664. $inAttributes = array_key_exists($key, $this->attributes);
  1665. // If the key references an attribute, we can just go ahead and return the
  1666. // plain attribute value from the model. This allows every attribute to
  1667. // be dynamically accessed through the _get method without accessors.
  1668. if ($inAttributes or $this->hasGetMutator($key))
  1669. {
  1670. return $this->getAttributeValue($key);
  1671. }
  1672. // If the key already exists in the relationships array, it just means the
  1673. // relationship has already been loaded, so we'll just return it out of
  1674. // here because there is no need to query within the relations twice.
  1675. if (array_key_exists($key, $this->relations))
  1676. {
  1677. return $this->relations[$key];
  1678. }
  1679. // If the "attribute" exists as a method on the model, we will just assume
  1680. // it is a relationship and will load and return results from the query
  1681. // and hydrate the relationship's value on the "relationships" array.
  1682. $camelKey = camel_case($key);
  1683. if (method_exists($this, $camelKey))
  1684. {
  1685. $relations = $this->$camelKey()->getResults();
  1686. return $this->relations[$key] = $relations;
  1687. }
  1688. }
  1689. /**
  1690. * Get a plain attribute (not a relationship).
  1691. *
  1692. * @param string $key
  1693. * @return mixed
  1694. */
  1695. protected function getAttributeValue($key)
  1696. {
  1697. $value = $this->getAttributeFromArray($key);
  1698. // If the attribute has a get mutator, we will call that then return what
  1699. // it returns as the value, which is useful for transforming values on
  1700. // retrieval from the model to a form that is more useful for usage.
  1701. if ($this->hasGetMutator($key))
  1702. {
  1703. return $this->mutateAttribute($key, $value);
  1704. }
  1705. // If the attribute is listed as a date, we will convert it to a DateTime
  1706. // instance on retrieval, which makes it quite convenient to work with
  1707. // date fields without having to create a mutator for each property.
  1708. elseif (in_array($key, $this->getDates()))
  1709. {
  1710. if ($value) return $this->asDateTime($value);
  1711. }
  1712. return $value;
  1713. }
  1714. /**
  1715. * Get an attribute from the $attributes array.
  1716. *
  1717. * @param string $key
  1718. * @return mixed
  1719. */
  1720. protected function getAttributeFromArray($key)
  1721. {
  1722. if (array_key_exists($key, $this->attributes))
  1723. {
  1724. return $this->attributes[$key];
  1725. }
  1726. }
  1727. /**
  1728. * Determine if a get mutator exists for an attribute.
  1729. *
  1730. * @param string $key
  1731. * @return bool
  1732. */
  1733. public function hasGetMutator($key)
  1734. {
  1735. return method_exists($this, 'get'.studly_case($key).'Attribute');
  1736. }
  1737. /**
  1738. * Get the value of an attribute using its mutator.
  1739. *
  1740. * @param string $key
  1741. * @param mixed $value
  1742. * @return mixed
  1743. */
  1744. protected function mutateAttribute($key, $value)
  1745. {
  1746. return $this->{'get'.studly_case($key).'Attribute'}($value);
  1747. }
  1748. /**
  1749. * Set a given attribute on the model.
  1750. *
  1751. * @param string $key
  1752. * @param mixed $value
  1753. * @return void
  1754. */
  1755. public function setAttribute($key, $value)
  1756. {
  1757. // First we will check for the presence of a mutator for the set operation
  1758. // which simply lets the developers tweak the attribute as it is set on
  1759. // the model, such as "json_encoding" an listing of data for storage.
  1760. if ($this->hasSetMutator($key))
  1761. {
  1762. $method = 'set'.studly_case($key).'Attribute';
  1763. return $this->{$method}($value);
  1764. }
  1765. // If an attribute is listed as a "date", we'll convert it from a DateTime
  1766. // instance into a form proper for storage on the database tables using
  1767. // the connection grammar's date format. We will auto set the values.
  1768. elseif (in_array($key, $this->getDates()))
  1769. {
  1770. if ($value)
  1771. {
  1772. $value = $this->fromDateTime($value);
  1773. }
  1774. }
  1775. $this->attributes[$key] = $value;
  1776. }
  1777. /**
  1778. * Determine if a set mutator exists for an attribute.
  1779. *
  1780. * @param string $key
  1781. * @return bool
  1782. */
  1783. public function hasSetMutator($key)
  1784. {
  1785. return method_exists($this, 'set'.studly_case($key).'Attribute');
  1786. }
  1787. /**
  1788. * Get the attributes that should be converted to dates.
  1789. *
  1790. * @return array
  1791. */
  1792. public function getDates()
  1793. {
  1794. return array(static::CREATED_AT, static::UPDATED_AT, static::DELETED_AT);
  1795. }
  1796. /**
  1797. * Convert a DateTime to a storable string.
  1798. *
  1799. * @param DateTime|int $value
  1800. * @return string
  1801. */
  1802. public function fromDateTime($value)
  1803. {
  1804. $format = $this->getDateFormat();
  1805. // If the value is already a DateTime instance, we will just skip the rest of
  1806. // these checks since they will be a waste of time, and hinder performance
  1807. // when checking the field. We will just return the DateTime right away.
  1808. if ($value instanceof DateTime)
  1809. {
  1810. //
  1811. }
  1812. // If the value is totally numeric, we will assume it is a UNIX timestamp and
  1813. // format the date as such. Once we have the date in DateTime form we will
  1814. // format it according to the proper format for the database connection.
  1815. elseif (is_numeric($value))
  1816. {
  1817. $value = Carbon::createFromTimestamp($value);
  1818. }
  1819. // If the value is in simple year, month, day format, we will format it using
  1820. // that setup. This is for simple "date" fields which do not have hours on
  1821. // the field. This conveniently picks up those dates and format correct.
  1822. elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value))
  1823. {
  1824. $value = Carbon::createFromFormat('Y-m-d', $value);
  1825. }
  1826. // If this value is some other type of string, we'll create the DateTime with
  1827. // the format used by the database connection. Once we get the instance we
  1828. // can return back the finally formatted DateTime instances to the devs.
  1829. elseif ( ! $value instanceof DateTime)
  1830. {
  1831. $value = Carbon::createFromFormat($format, $value);
  1832. }
  1833. return $value->format($format);
  1834. }
  1835. /**
  1836. * Return a timestamp as DateTime object.
  1837. *
  1838. * @param mixed $value
  1839. * @return DateTime
  1840. */
  1841. protected function asDateTime($value)
  1842. {
  1843. // If this value is an integer, we will assume it is a UNIX timestamp's value
  1844. // and format a Carbon object from this timestamp. This allows flexibility
  1845. // when defining your date fields as they might be UNIX timestamps here.
  1846. if (is_numeric($value))
  1847. {
  1848. return Carbon::createFromTimestamp($value);
  1849. }
  1850. // If the value is in simply year, month, day format, we will instantiate the
  1851. // Carbon instances from that fomrat. Again, this provides for simple date
  1852. // fields on the database, while still supporting Carbonized conversion.
  1853. elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value))
  1854. {
  1855. return Carbon::createFromFormat('Y-m-d', $value);
  1856. }
  1857. // Finally, we will just assume this date is in the format used by default on
  1858. // the database connection and use that format to create the Carbon object
  1859. // that is returned back out to the developers after we convert it here.
  1860. elseif ( ! $value instanceof DateTime)
  1861. {
  1862. $format = $this->getDateFormat();
  1863. return Carbon::createFromFormat($format, $value);
  1864. }
  1865. return Carbon::instance($value);
  1866. }
  1867. /**
  1868. * Get the format for database stored dates.
  1869. *
  1870. * @return string
  1871. */
  1872. protected function getDateFormat()
  1873. {
  1874. return $this->getConnection()->getQueryGrammar()->getDateFormat();
  1875. }
  1876. /**
  1877. * Clone the model into a new, non-existing instance.
  1878. *
  1879. * @return \Illuminate\Database\Eloquent\Model
  1880. */
  1881. public function replicate()
  1882. {
  1883. $attributes = array_except($this->attributes, array($this->getKeyName()));
  1884. with($instance = new static)->setRawAttributes($attributes);
  1885. return $instance->setRelations($this->relations);
  1886. }
  1887. /**
  1888. * Get all of the current attributes on the model.
  1889. *
  1890. * @return array
  1891. */
  1892. public function getAttributes()
  1893. {
  1894. return $this->attributes;
  1895. }
  1896. /**
  1897. * Set the array of model attributes. No checking is done.
  1898. *
  1899. * @param array $attributes
  1900. * @param bool $sync
  1901. * @return void
  1902. */
  1903. public function setRawAttributes(array $attributes, $sync = false)
  1904. {
  1905. $this->attributes = $attributes;
  1906. if ($sync) $this->syncOriginal();
  1907. }
  1908. /**
  1909. * Get the model's original attribute values.
  1910. *
  1911. * @param string $key
  1912. * @param mixed $default
  1913. * @return array
  1914. */
  1915. public function getOriginal($key = null, $default = null)
  1916. {
  1917. return array_get($this->original, $key, $default);
  1918. }
  1919. /**
  1920. * Sync the original attributes with the current.
  1921. *
  1922. * @return \Illuminate\Database\Eloquent\Model
  1923. */
  1924. public function syncOriginal()
  1925. {
  1926. $this->original = $this->attributes;
  1927. return $this;
  1928. }
  1929. /**
  1930. * Determine if a given attribute is dirty.
  1931. *
  1932. * @param string $attribute
  1933. * @return bool
  1934. */
  1935. public function isDirty($attribute)
  1936. {
  1937. return array_key_exists($attribute, $this->getDirty());
  1938. }
  1939. /**
  1940. * Get the attributes that have been changed since last sync.
  1941. *
  1942. * @return array
  1943. */
  1944. public function getDirty()
  1945. {
  1946. $dirty = array();
  1947. foreach ($this->attributes as $key => $value)
  1948. {
  1949. if ( ! array_key_exists($key, $this->original) or $value !== $this->original[$key])
  1950. {
  1951. $dirty[$key] = $value;
  1952. }
  1953. }
  1954. return $dirty;
  1955. }
  1956. /**
  1957. * Get all the loaded relations for the instance.
  1958. *
  1959. * @return array
  1960. */
  1961. public function getRelations()
  1962. {
  1963. return $this->relations;
  1964. }
  1965. /**
  1966. * Get a specified relationship.
  1967. *
  1968. * @param string $relation
  1969. * @return mixed
  1970. */
  1971. public function getRelation($relation)
  1972. {
  1973. return $this->relations[$relation];
  1974. }
  1975. /**
  1976. * Set the specific relationship in the model.
  1977. *
  1978. * @param string $relation
  1979. * @param mixed $value
  1980. * @return \Illuminate\Database\Eloquent\Model
  1981. */
  1982. public function setRelation($relation, $value)
  1983. {
  1984. $this->relations[$relation] = $value;
  1985. return $this;
  1986. }
  1987. /**
  1988. * Set the entire relations array on the model.
  1989. *
  1990. * @param array $relations
  1991. * @return \Illuminate\Database\Eloquent\Model
  1992. */
  1993. public function setRelations(array $relations)
  1994. {
  1995. $this->relations = $relations;
  1996. return $this;
  1997. }
  1998. /**
  1999. * Get the database connection for the model.
  2000. *
  2001. * @return \Illuminate\Database\Connection
  2002. */
  2003. public function getConnection()
  2004. {
  2005. return static::resolveConnection($this->connection);
  2006. }
  2007. /**
  2008. * Get the current connection name for the model.
  2009. *
  2010. * @return string
  2011. */
  2012. public function getConnectionName()
  2013. {
  2014. return $this->connection;
  2015. }
  2016. /**
  2017. * Set the connection associated with the model.
  2018. *
  2019. * @param string $name
  2020. * @return \Illuminate\Database\Eloquent\Model
  2021. */
  2022. public function setConnection($name)
  2023. {
  2024. $this->connection = $name;
  2025. return $this;
  2026. }
  2027. /**
  2028. * Resolve a connection instance.
  2029. *
  2030. * @param string $connection
  2031. * @return \Illuminate\Database\Connection
  2032. */
  2033. public static function resolveConnection($connection = null)
  2034. {
  2035. return static::$resolver->connection($connection);
  2036. }
  2037. /**
  2038. * Get the connection resolver instance.
  2039. *
  2040. * @return \Illuminate\Database\ConnectionResolverInterface
  2041. */
  2042. public static function getConnectionResolver()
  2043. {
  2044. return static::$resolver;
  2045. }
  2046. /**
  2047. * Set the connection resolver instance.
  2048. *
  2049. * @param \Illuminate\Database\ConnectionResolverInterface $resolver
  2050. * @return void
  2051. */
  2052. public static function setConnectionResolver(Resolver $resolver)
  2053. {
  2054. static::$resolver = $resolver;
  2055. }
  2056. /**
  2057. * Get the event dispatcher instance.
  2058. *
  2059. * @return \Illuminate\Events\Dispatcher
  2060. */
  2061. public static function getEventDispatcher()
  2062. {
  2063. return static::$dispatcher;
  2064. }
  2065. /**
  2066. * Set the event dispatcher instance.
  2067. *
  2068. * @param \Illuminate\Events\Dispatcher $dispatcher
  2069. * @return void
  2070. */
  2071. public static function setEventDispatcher(Dispatcher $dispatcher)
  2072. {
  2073. static::$dispatcher = $dispatcher;
  2074. }
  2075. /**
  2076. * Unset the event dispatcher for models.
  2077. *
  2078. * @return void
  2079. */
  2080. public static function unsetEventDispatcher()
  2081. {
  2082. static::$dispatcher = null;
  2083. }
  2084. /**
  2085. * Get the mutated attributes for a given instance.
  2086. *
  2087. * @return array
  2088. */
  2089. public function getMutatedAttributes()
  2090. {
  2091. $class = get_class($this);
  2092. if (isset(static::$mutatorCache[$class]))
  2093. {
  2094. return static::$mutatorCache[get_class($this)];
  2095. }
  2096. return array();
  2097. }
  2098. /**
  2099. * Dynamically retrieve attributes on the model.
  2100. *
  2101. * @param string $key
  2102. * @return mixed
  2103. */
  2104. public function __get($key)
  2105. {
  2106. return $this->getAttribute($key);
  2107. }
  2108. /**
  2109. * Dynamically set attributes on the model.
  2110. *
  2111. * @param string $key
  2112. * @param mixed $value
  2113. * @return void
  2114. */
  2115. public function __set($key, $value)
  2116. {
  2117. $this->setAttribute($key, $value);
  2118. }
  2119. /**
  2120. * Determine if the given attribute exists.
  2121. *
  2122. * @param mixed $offset
  2123. * @return bool
  2124. */
  2125. public function offsetExists($offset)
  2126. {
  2127. return isset($this->$offset);
  2128. }
  2129. /**
  2130. * Get the value for a given offset.
  2131. *
  2132. * @param mixed $offset
  2133. * @return mixed
  2134. */
  2135. public function offsetGet($offset)
  2136. {
  2137. return $this->$offset;
  2138. }
  2139. /**
  2140. * Set the value for a given offset.
  2141. *
  2142. * @param mixed $offset
  2143. * @param mixed $value
  2144. * @return void
  2145. */
  2146. public function offsetSet($offset, $value)
  2147. {
  2148. $this->$offset = $value;
  2149. }
  2150. /**
  2151. * Unset the value for a given offset.
  2152. *
  2153. * @param mixed $offset
  2154. * @return void
  2155. */
  2156. public function offsetUnset($offset)
  2157. {
  2158. unset($this->$offset);
  2159. }
  2160. /**
  2161. * Determine if an attribute exists on the model.
  2162. *
  2163. * @param string $key
  2164. * @return void
  2165. */
  2166. public function __isset($key)
  2167. {
  2168. return isset($this->attributes[$key]) or isset($this->relations[$key]);
  2169. }
  2170. /**
  2171. * Unset an attribute on the model.
  2172. *
  2173. * @param string $key
  2174. * @return void
  2175. */
  2176. public function __unset($key)
  2177. {
  2178. unset($this->attributes[$key]);
  2179. unset($this->relations[$key]);
  2180. }
  2181. /**
  2182. * Handle dynamic method calls into the method.
  2183. *
  2184. * @param string $method
  2185. * @param array $parameters
  2186. * @return mixed
  2187. */
  2188. public function __call($method, $parameters)
  2189. {
  2190. if (in_array($method, array('increment', 'decrement')))
  2191. {
  2192. return call_user_func_array(array($this, $method), $parameters);
  2193. }
  2194. $query = $this->newQuery();
  2195. return call_user_func_array(array($query, $method), $parameters);
  2196. }
  2197. /**
  2198. * Handle dynamic static method calls into the method.
  2199. *
  2200. * @param string $method
  2201. * @param array $parameters
  2202. * @return mixed
  2203. */
  2204. public static function __callStatic($method, $parameters)
  2205. {
  2206. $instance = new static;
  2207. return call_user_func_array(array($instance, $method), $parameters);
  2208. }
  2209. /**
  2210. * Convert the model to its string representation.
  2211. *
  2212. * @return string
  2213. */
  2214. public function __toString()
  2215. {
  2216. return $this->toJson();
  2217. }
  2218. }