DatabaseEloquentIntegrationTest.php 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use DateTimeInterface;
  4. use Exception;
  5. use Illuminate\Database\Capsule\Manager as DB;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Database\Eloquent\Collection;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Database\Eloquent\Model as Eloquent;
  10. use Illuminate\Database\Eloquent\ModelNotFoundException;
  11. use Illuminate\Database\Eloquent\Relations\MorphPivot;
  12. use Illuminate\Database\Eloquent\Relations\Pivot;
  13. use Illuminate\Database\Eloquent\Relations\Relation;
  14. use Illuminate\Database\Eloquent\SoftDeletes;
  15. use Illuminate\Database\Eloquent\SoftDeletingScope;
  16. use Illuminate\Database\QueryException;
  17. use Illuminate\Pagination\AbstractPaginator as Paginator;
  18. use Illuminate\Pagination\Cursor;
  19. use Illuminate\Pagination\CursorPaginator;
  20. use Illuminate\Pagination\LengthAwarePaginator;
  21. use Illuminate\Support\Carbon;
  22. use Illuminate\Support\Facades\Date;
  23. use Illuminate\Tests\Integration\Database\Fixtures\Post;
  24. use Illuminate\Tests\Integration\Database\Fixtures\User;
  25. use PHPUnit\Framework\TestCase;
  26. class DatabaseEloquentIntegrationTest extends TestCase
  27. {
  28. /**
  29. * Setup the database schema.
  30. *
  31. * @return void
  32. */
  33. protected function setUp(): void
  34. {
  35. $db = new DB;
  36. $db->addConnection([
  37. 'driver' => 'sqlite',
  38. 'database' => ':memory:',
  39. ]);
  40. $db->addConnection([
  41. 'driver' => 'sqlite',
  42. 'database' => ':memory:',
  43. ], 'second_connection');
  44. $db->bootEloquent();
  45. $db->setAsGlobal();
  46. $this->createSchema();
  47. }
  48. protected function createSchema()
  49. {
  50. $this->schema('default')->create('test_orders', function ($table) {
  51. $table->increments('id');
  52. $table->string('item_type');
  53. $table->integer('item_id');
  54. $table->timestamps();
  55. });
  56. $this->schema('default')->create('with_json', function ($table) {
  57. $table->increments('id');
  58. $table->text('json')->default(json_encode([]));
  59. });
  60. $this->schema('second_connection')->create('test_items', function ($table) {
  61. $table->increments('id');
  62. $table->timestamps();
  63. });
  64. $this->schema('default')->create('users_with_space_in_colum_name', function ($table) {
  65. $table->increments('id');
  66. $table->string('name')->nullable();
  67. $table->string('email address');
  68. $table->timestamps();
  69. });
  70. foreach (['default', 'second_connection'] as $connection) {
  71. $this->schema($connection)->create('users', function ($table) {
  72. $table->increments('id');
  73. $table->string('name')->nullable();
  74. $table->string('email');
  75. $table->timestamp('birthday', 6)->nullable();
  76. $table->timestamps();
  77. });
  78. $this->schema($connection)->create('friends', function ($table) {
  79. $table->integer('user_id');
  80. $table->integer('friend_id');
  81. $table->integer('friend_level_id')->nullable();
  82. });
  83. $this->schema($connection)->create('posts', function ($table) {
  84. $table->increments('id');
  85. $table->integer('user_id');
  86. $table->integer('parent_id')->nullable();
  87. $table->string('name');
  88. $table->timestamps();
  89. });
  90. $this->schema($connection)->create('comments', function ($table) {
  91. $table->increments('id');
  92. $table->integer('post_id');
  93. $table->string('content');
  94. $table->timestamps();
  95. });
  96. $this->schema($connection)->create('friend_levels', function ($table) {
  97. $table->increments('id');
  98. $table->string('level');
  99. $table->timestamps();
  100. });
  101. $this->schema($connection)->create('photos', function ($table) {
  102. $table->increments('id');
  103. $table->morphs('imageable');
  104. $table->string('name');
  105. $table->timestamps();
  106. });
  107. $this->schema($connection)->create('soft_deleted_users', function ($table) {
  108. $table->increments('id');
  109. $table->string('name')->nullable();
  110. $table->string('email');
  111. $table->timestamps();
  112. $table->softDeletes();
  113. });
  114. $this->schema($connection)->create('tags', function ($table) {
  115. $table->increments('id');
  116. $table->string('name');
  117. $table->timestamps();
  118. });
  119. $this->schema($connection)->create('taggables', function ($table) {
  120. $table->integer('tag_id');
  121. $table->morphs('taggable');
  122. $table->string('taxonomy')->nullable();
  123. });
  124. }
  125. $this->schema($connection)->create('non_incrementing_users', function ($table) {
  126. $table->string('name')->nullable();
  127. });
  128. }
  129. /**
  130. * Tear down the database schema.
  131. *
  132. * @return void
  133. */
  134. protected function tearDown(): void
  135. {
  136. foreach (['default', 'second_connection'] as $connection) {
  137. $this->schema($connection)->drop('users');
  138. $this->schema($connection)->drop('friends');
  139. $this->schema($connection)->drop('posts');
  140. $this->schema($connection)->drop('friend_levels');
  141. $this->schema($connection)->drop('photos');
  142. }
  143. Relation::morphMap([], false);
  144. Eloquent::unsetConnectionResolver();
  145. }
  146. /**
  147. * Tests...
  148. */
  149. public function testBasicModelRetrieval()
  150. {
  151. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  152. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  153. $this->assertEquals(2, EloquentTestUser::count());
  154. $this->assertFalse(EloquentTestUser::where('email', 'taylorotwell@gmail.com')->doesntExist());
  155. $this->assertTrue(EloquentTestUser::where('email', 'mohamed@laravel.com')->doesntExist());
  156. $model = EloquentTestUser::where('email', 'taylorotwell@gmail.com')->first();
  157. $this->assertSame('taylorotwell@gmail.com', $model->email);
  158. $this->assertTrue(isset($model->email));
  159. $this->assertTrue(isset($model->friends));
  160. $model = EloquentTestUser::find(1);
  161. $this->assertInstanceOf(EloquentTestUser::class, $model);
  162. $this->assertEquals(1, $model->id);
  163. $model = EloquentTestUser::find(2);
  164. $this->assertInstanceOf(EloquentTestUser::class, $model);
  165. $this->assertEquals(2, $model->id);
  166. $missing = EloquentTestUser::find(3);
  167. $this->assertNull($missing);
  168. $collection = EloquentTestUser::find([]);
  169. $this->assertInstanceOf(Collection::class, $collection);
  170. $this->assertCount(0, $collection);
  171. $collection = EloquentTestUser::find([1, 2, 3]);
  172. $this->assertInstanceOf(Collection::class, $collection);
  173. $this->assertCount(2, $collection);
  174. $models = EloquentTestUser::where('id', 1)->cursor();
  175. foreach ($models as $model) {
  176. $this->assertEquals(1, $model->id);
  177. $this->assertSame('default', $model->getConnectionName());
  178. }
  179. $records = DB::table('users')->where('id', 1)->cursor();
  180. foreach ($records as $record) {
  181. $this->assertEquals(1, $record->id);
  182. }
  183. $records = DB::cursor('select * from users where id = ?', [1]);
  184. foreach ($records as $record) {
  185. $this->assertEquals(1, $record->id);
  186. }
  187. }
  188. public function testBasicModelCollectionRetrieval()
  189. {
  190. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  191. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  192. $models = EloquentTestUser::oldest('id')->get();
  193. $this->assertCount(2, $models);
  194. $this->assertInstanceOf(Collection::class, $models);
  195. $this->assertInstanceOf(EloquentTestUser::class, $models[0]);
  196. $this->assertInstanceOf(EloquentTestUser::class, $models[1]);
  197. $this->assertSame('taylorotwell@gmail.com', $models[0]->email);
  198. $this->assertSame('abigailotwell@gmail.com', $models[1]->email);
  199. }
  200. public function testPaginatedModelCollectionRetrieval()
  201. {
  202. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  203. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  204. EloquentTestUser::create(['id' => 3, 'email' => 'foo@gmail.com']);
  205. Paginator::currentPageResolver(function () {
  206. return 1;
  207. });
  208. $models = EloquentTestUser::oldest('id')->paginate(2);
  209. $this->assertCount(2, $models);
  210. $this->assertInstanceOf(LengthAwarePaginator::class, $models);
  211. $this->assertInstanceOf(EloquentTestUser::class, $models[0]);
  212. $this->assertInstanceOf(EloquentTestUser::class, $models[1]);
  213. $this->assertSame('taylorotwell@gmail.com', $models[0]->email);
  214. $this->assertSame('abigailotwell@gmail.com', $models[1]->email);
  215. Paginator::currentPageResolver(function () {
  216. return 2;
  217. });
  218. $models = EloquentTestUser::oldest('id')->paginate(2);
  219. $this->assertCount(1, $models);
  220. $this->assertInstanceOf(LengthAwarePaginator::class, $models);
  221. $this->assertInstanceOf(EloquentTestUser::class, $models[0]);
  222. $this->assertSame('foo@gmail.com', $models[0]->email);
  223. }
  224. public function testPaginatedModelCollectionRetrievalWhenNoElements()
  225. {
  226. Paginator::currentPageResolver(function () {
  227. return 1;
  228. });
  229. $models = EloquentTestUser::oldest('id')->paginate(2);
  230. $this->assertCount(0, $models);
  231. $this->assertInstanceOf(LengthAwarePaginator::class, $models);
  232. Paginator::currentPageResolver(function () {
  233. return 2;
  234. });
  235. $models = EloquentTestUser::oldest('id')->paginate(2);
  236. $this->assertCount(0, $models);
  237. }
  238. public function testPaginatedModelCollectionRetrievalWhenNoElementsAndDefaultPerPage()
  239. {
  240. $models = EloquentTestUser::oldest('id')->paginate();
  241. $this->assertCount(0, $models);
  242. $this->assertInstanceOf(LengthAwarePaginator::class, $models);
  243. }
  244. public function testCountForPaginationWithGrouping()
  245. {
  246. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  247. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  248. EloquentTestUser::create(['id' => 3, 'email' => 'foo@gmail.com']);
  249. EloquentTestUser::create(['id' => 4, 'email' => 'foo@gmail.com']);
  250. $query = EloquentTestUser::groupBy('email')->getQuery();
  251. $this->assertEquals(3, $query->getCountForPagination());
  252. }
  253. public function testCountForPaginationWithGroupingAndSubSelects()
  254. {
  255. $user1 = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  256. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  257. EloquentTestUser::create(['id' => 3, 'email' => 'foo@gmail.com']);
  258. EloquentTestUser::create(['id' => 4, 'email' => 'foo@gmail.com']);
  259. $user1->friends()->create(['id' => 5, 'email' => 'friend@gmail.com']);
  260. $query = EloquentTestUser::select([
  261. 'id',
  262. 'friends_count' => EloquentTestUser::whereColumn('friend_id', 'user_id')->count(),
  263. ])->groupBy('email')->getQuery();
  264. $this->assertEquals(4, $query->getCountForPagination());
  265. }
  266. public function testCursorPaginatedModelCollectionRetrieval()
  267. {
  268. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  269. EloquentTestUser::create($secondParams = ['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  270. EloquentTestUser::create(['id' => 3, 'email' => 'foo@gmail.com']);
  271. CursorPaginator::currentCursorResolver(function () {
  272. return null;
  273. });
  274. $models = EloquentTestUser::oldest('id')->cursorPaginate(2);
  275. $this->assertCount(2, $models);
  276. $this->assertInstanceOf(CursorPaginator::class, $models);
  277. $this->assertInstanceOf(EloquentTestUser::class, $models[0]);
  278. $this->assertInstanceOf(EloquentTestUser::class, $models[1]);
  279. $this->assertSame('taylorotwell@gmail.com', $models[0]->email);
  280. $this->assertSame('abigailotwell@gmail.com', $models[1]->email);
  281. $this->assertTrue($models->hasMorePages());
  282. $this->assertTrue($models->hasPages());
  283. CursorPaginator::currentCursorResolver(function () use ($secondParams) {
  284. return new Cursor($secondParams);
  285. });
  286. $models = EloquentTestUser::oldest('id')->cursorPaginate(2);
  287. $this->assertCount(1, $models);
  288. $this->assertInstanceOf(CursorPaginator::class, $models);
  289. $this->assertInstanceOf(EloquentTestUser::class, $models[0]);
  290. $this->assertSame('foo@gmail.com', $models[0]->email);
  291. $this->assertFalse($models->hasMorePages());
  292. $this->assertTrue($models->hasPages());
  293. }
  294. public function testPreviousCursorPaginatedModelCollectionRetrieval()
  295. {
  296. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  297. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  298. EloquentTestUser::create($thirdParams = ['id' => 3, 'email' => 'foo@gmail.com']);
  299. CursorPaginator::currentCursorResolver(function () use ($thirdParams) {
  300. return new Cursor($thirdParams, false);
  301. });
  302. $models = EloquentTestUser::oldest('id')->cursorPaginate(2);
  303. $this->assertCount(2, $models);
  304. $this->assertInstanceOf(CursorPaginator::class, $models);
  305. $this->assertInstanceOf(EloquentTestUser::class, $models[0]);
  306. $this->assertInstanceOf(EloquentTestUser::class, $models[1]);
  307. $this->assertSame('taylorotwell@gmail.com', $models[0]->email);
  308. $this->assertSame('abigailotwell@gmail.com', $models[1]->email);
  309. $this->assertTrue($models->hasMorePages());
  310. $this->assertTrue($models->hasPages());
  311. }
  312. public function testCursorPaginatedModelCollectionRetrievalWhenNoElements()
  313. {
  314. CursorPaginator::currentCursorResolver(function () {
  315. return null;
  316. });
  317. $models = EloquentTestUser::oldest('id')->cursorPaginate(2);
  318. $this->assertCount(0, $models);
  319. $this->assertInstanceOf(CursorPaginator::class, $models);
  320. Paginator::currentPageResolver(function () {
  321. return new Cursor(['id' => 1]);
  322. });
  323. $models = EloquentTestUser::oldest('id')->cursorPaginate(2);
  324. $this->assertCount(0, $models);
  325. }
  326. public function testCursorPaginatedModelCollectionRetrievalWhenNoElementsAndDefaultPerPage()
  327. {
  328. $models = EloquentTestUser::oldest('id')->cursorPaginate();
  329. $this->assertCount(0, $models);
  330. $this->assertInstanceOf(CursorPaginator::class, $models);
  331. }
  332. public function testFirstOrNew()
  333. {
  334. $user1 = EloquentTestUser::firstOrNew(
  335. ['name' => 'Dries Vints'],
  336. ['name' => 'Nuno Maduro']
  337. );
  338. $this->assertSame('Nuno Maduro', $user1->name);
  339. }
  340. public function testFirstOrCreate()
  341. {
  342. $user1 = EloquentTestUser::firstOrCreate(['email' => 'taylorotwell@gmail.com']);
  343. $this->assertSame('taylorotwell@gmail.com', $user1->email);
  344. $this->assertNull($user1->name);
  345. $user2 = EloquentTestUser::firstOrCreate(
  346. ['email' => 'taylorotwell@gmail.com'],
  347. ['name' => 'Taylor Otwell']
  348. );
  349. $this->assertEquals($user1->id, $user2->id);
  350. $this->assertSame('taylorotwell@gmail.com', $user2->email);
  351. $this->assertNull($user2->name);
  352. $user3 = EloquentTestUser::firstOrCreate(
  353. ['email' => 'abigailotwell@gmail.com'],
  354. ['name' => 'Abigail Otwell']
  355. );
  356. $this->assertNotEquals($user3->id, $user1->id);
  357. $this->assertSame('abigailotwell@gmail.com', $user3->email);
  358. $this->assertSame('Abigail Otwell', $user3->name);
  359. $user4 = EloquentTestUser::firstOrCreate(
  360. ['name' => 'Dries Vints'],
  361. ['name' => 'Nuno Maduro', 'email' => 'nuno@laravel.com']
  362. );
  363. $this->assertSame('Nuno Maduro', $user4->name);
  364. }
  365. public function testUpdateOrCreate()
  366. {
  367. $user1 = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  368. $user2 = EloquentTestUser::updateOrCreate(
  369. ['email' => 'taylorotwell@gmail.com'],
  370. ['name' => 'Taylor Otwell']
  371. );
  372. $this->assertEquals($user1->id, $user2->id);
  373. $this->assertSame('taylorotwell@gmail.com', $user2->email);
  374. $this->assertSame('Taylor Otwell', $user2->name);
  375. $user3 = EloquentTestUser::updateOrCreate(
  376. ['email' => 'themsaid@gmail.com'],
  377. ['name' => 'Mohamed Said']
  378. );
  379. $this->assertSame('Mohamed Said', $user3->name);
  380. $this->assertEquals(2, EloquentTestUser::count());
  381. }
  382. public function testUpdateOrCreateOnDifferentConnection()
  383. {
  384. EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  385. EloquentTestUser::on('second_connection')->updateOrCreate(
  386. ['email' => 'taylorotwell@gmail.com'],
  387. ['name' => 'Taylor Otwell']
  388. );
  389. EloquentTestUser::on('second_connection')->updateOrCreate(
  390. ['email' => 'themsaid@gmail.com'],
  391. ['name' => 'Mohamed Said']
  392. );
  393. $this->assertEquals(1, EloquentTestUser::count());
  394. $this->assertEquals(2, EloquentTestUser::on('second_connection')->count());
  395. }
  396. public function testCheckAndCreateMethodsOnMultiConnections()
  397. {
  398. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  399. EloquentTestUser::on('second_connection')->find(
  400. EloquentTestUser::on('second_connection')->insert(['id' => 2, 'email' => 'themsaid@gmail.com'])
  401. );
  402. $user1 = EloquentTestUser::on('second_connection')->findOrNew(1);
  403. $user2 = EloquentTestUser::on('second_connection')->findOrNew(2);
  404. $this->assertFalse($user1->exists);
  405. $this->assertTrue($user2->exists);
  406. $this->assertSame('second_connection', $user1->getConnectionName());
  407. $this->assertSame('second_connection', $user2->getConnectionName());
  408. $user1 = EloquentTestUser::on('second_connection')->firstOrNew(['email' => 'taylorotwell@gmail.com']);
  409. $user2 = EloquentTestUser::on('second_connection')->firstOrNew(['email' => 'themsaid@gmail.com']);
  410. $this->assertFalse($user1->exists);
  411. $this->assertTrue($user2->exists);
  412. $this->assertSame('second_connection', $user1->getConnectionName());
  413. $this->assertSame('second_connection', $user2->getConnectionName());
  414. $this->assertEquals(1, EloquentTestUser::on('second_connection')->count());
  415. $user1 = EloquentTestUser::on('second_connection')->firstOrCreate(['email' => 'taylorotwell@gmail.com']);
  416. $user2 = EloquentTestUser::on('second_connection')->firstOrCreate(['email' => 'themsaid@gmail.com']);
  417. $this->assertSame('second_connection', $user1->getConnectionName());
  418. $this->assertSame('second_connection', $user2->getConnectionName());
  419. $this->assertEquals(2, EloquentTestUser::on('second_connection')->count());
  420. }
  421. public function testCreatingModelWithEmptyAttributes()
  422. {
  423. $model = EloquentTestNonIncrementing::create([]);
  424. $this->assertFalse($model->exists);
  425. $this->assertFalse($model->wasRecentlyCreated);
  426. }
  427. public function testChunkByIdWithNonIncrementingKey()
  428. {
  429. EloquentTestNonIncrementingSecond::create(['name' => ' First']);
  430. EloquentTestNonIncrementingSecond::create(['name' => ' Second']);
  431. EloquentTestNonIncrementingSecond::create(['name' => ' Third']);
  432. $i = 0;
  433. EloquentTestNonIncrementingSecond::query()->chunkById(2, function (Collection $users) use (&$i) {
  434. if (! $i) {
  435. $this->assertSame(' First', $users[0]->name);
  436. $this->assertSame(' Second', $users[1]->name);
  437. } else {
  438. $this->assertSame(' Third', $users[0]->name);
  439. }
  440. $i++;
  441. }, 'name');
  442. $this->assertEquals(2, $i);
  443. }
  444. public function testEachByIdWithNonIncrementingKey()
  445. {
  446. EloquentTestNonIncrementingSecond::create(['name' => ' First']);
  447. EloquentTestNonIncrementingSecond::create(['name' => ' Second']);
  448. EloquentTestNonIncrementingSecond::create(['name' => ' Third']);
  449. $users = [];
  450. EloquentTestNonIncrementingSecond::query()->eachById(
  451. function (EloquentTestNonIncrementingSecond $user, $i) use (&$users) {
  452. $users[] = [$user->name, $i];
  453. }, 2, 'name');
  454. $this->assertSame([[' First', 0], [' Second', 1], [' Third', 2]], $users);
  455. }
  456. public function testPluck()
  457. {
  458. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  459. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  460. $simple = EloquentTestUser::oldest('id')->pluck('users.email')->all();
  461. $keyed = EloquentTestUser::oldest('id')->pluck('users.email', 'users.id')->all();
  462. $this->assertEquals(['taylorotwell@gmail.com', 'abigailotwell@gmail.com'], $simple);
  463. $this->assertEquals([1 => 'taylorotwell@gmail.com', 2 => 'abigailotwell@gmail.com'], $keyed);
  464. }
  465. public function testPluckWithJoin()
  466. {
  467. $user1 = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  468. $user2 = EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  469. $user2->posts()->create(['id' => 1, 'name' => 'First post']);
  470. $user1->posts()->create(['id' => 2, 'name' => 'Second post']);
  471. $query = EloquentTestUser::join('posts', 'users.id', '=', 'posts.user_id');
  472. $this->assertEquals([1 => 'First post', 2 => 'Second post'], $query->pluck('posts.name', 'posts.id')->all());
  473. $this->assertEquals([2 => 'First post', 1 => 'Second post'], $query->pluck('posts.name', 'users.id')->all());
  474. $this->assertEquals(['abigailotwell@gmail.com' => 'First post', 'taylorotwell@gmail.com' => 'Second post'], $query->pluck('posts.name', 'users.email AS user_email')->all());
  475. }
  476. public function testPluckWithColumnNameContainingASpace()
  477. {
  478. EloquentTestUserWithSpaceInColumnName::create(['id' => 1, 'email address' => 'taylorotwell@gmail.com']);
  479. EloquentTestUserWithSpaceInColumnName::create(['id' => 2, 'email address' => 'abigailotwell@gmail.com']);
  480. $simple = EloquentTestUserWithSpaceInColumnName::oldest('id')->pluck('users_with_space_in_colum_name.email address')->all();
  481. $keyed = EloquentTestUserWithSpaceInColumnName::oldest('id')->pluck('email address', 'id')->all();
  482. $this->assertEquals(['taylorotwell@gmail.com', 'abigailotwell@gmail.com'], $simple);
  483. $this->assertEquals([1 => 'taylorotwell@gmail.com', 2 => 'abigailotwell@gmail.com'], $keyed);
  484. }
  485. public function testFindOrFail()
  486. {
  487. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  488. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  489. $single = EloquentTestUser::findOrFail(1);
  490. $multiple = EloquentTestUser::findOrFail([1, 2]);
  491. $this->assertInstanceOf(EloquentTestUser::class, $single);
  492. $this->assertSame('taylorotwell@gmail.com', $single->email);
  493. $this->assertInstanceOf(Collection::class, $multiple);
  494. $this->assertInstanceOf(EloquentTestUser::class, $multiple[0]);
  495. $this->assertInstanceOf(EloquentTestUser::class, $multiple[1]);
  496. }
  497. public function testFindOrFailWithSingleIdThrowsModelNotFoundException()
  498. {
  499. $this->expectException(ModelNotFoundException::class);
  500. $this->expectExceptionMessage('No query results for model [Illuminate\Tests\Database\EloquentTestUser] 1');
  501. EloquentTestUser::findOrFail(1);
  502. }
  503. public function testFindOrFailWithMultipleIdsThrowsModelNotFoundException()
  504. {
  505. $this->expectException(ModelNotFoundException::class);
  506. $this->expectExceptionMessage('No query results for model [Illuminate\Tests\Database\EloquentTestUser] 1, 2');
  507. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  508. EloquentTestUser::findOrFail([1, 2]);
  509. }
  510. public function testFindOrFailWithMultipleIdsUsingCollectionThrowsModelNotFoundException()
  511. {
  512. $this->expectException(ModelNotFoundException::class);
  513. $this->expectExceptionMessage('No query results for model [Illuminate\Tests\Database\EloquentTestUser] 1, 2');
  514. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  515. EloquentTestUser::findOrFail(new Collection([1, 2]));
  516. }
  517. public function testOneToOneRelationship()
  518. {
  519. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  520. $user->post()->create(['name' => 'First Post']);
  521. $post = $user->post;
  522. $user = $post->user;
  523. $this->assertTrue(isset($user->post->name));
  524. $this->assertInstanceOf(EloquentTestUser::class, $user);
  525. $this->assertInstanceOf(EloquentTestPost::class, $post);
  526. $this->assertSame('taylorotwell@gmail.com', $user->email);
  527. $this->assertSame('First Post', $post->name);
  528. }
  529. public function testIssetLoadsInRelationshipIfItIsntLoadedAlready()
  530. {
  531. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  532. $user->post()->create(['name' => 'First Post']);
  533. $this->assertTrue(isset($user->post->name));
  534. }
  535. public function testOneToManyRelationship()
  536. {
  537. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  538. $user->posts()->create(['name' => 'First Post']);
  539. $user->posts()->create(['name' => 'Second Post']);
  540. $posts = $user->posts;
  541. $post2 = $user->posts()->where('name', 'Second Post')->first();
  542. $this->assertInstanceOf(Collection::class, $posts);
  543. $this->assertCount(2, $posts);
  544. $this->assertInstanceOf(EloquentTestPost::class, $posts[0]);
  545. $this->assertInstanceOf(EloquentTestPost::class, $posts[1]);
  546. $this->assertInstanceOf(EloquentTestPost::class, $post2);
  547. $this->assertSame('Second Post', $post2->name);
  548. $this->assertInstanceOf(EloquentTestUser::class, $post2->user);
  549. $this->assertSame('taylorotwell@gmail.com', $post2->user->email);
  550. }
  551. public function testBasicModelHydration()
  552. {
  553. $user = new EloquentTestUser(['email' => 'taylorotwell@gmail.com']);
  554. $user->setConnection('second_connection');
  555. $user->save();
  556. $user = new EloquentTestUser(['email' => 'abigailotwell@gmail.com']);
  557. $user->setConnection('second_connection');
  558. $user->save();
  559. $models = EloquentTestUser::on('second_connection')->fromQuery('SELECT * FROM users WHERE email = ?', ['abigailotwell@gmail.com']);
  560. $this->assertInstanceOf(Collection::class, $models);
  561. $this->assertInstanceOf(EloquentTestUser::class, $models[0]);
  562. $this->assertSame('abigailotwell@gmail.com', $models[0]->email);
  563. $this->assertSame('second_connection', $models[0]->getConnectionName());
  564. $this->assertCount(1, $models);
  565. }
  566. public function testFirstOrNewOnHasOneRelationShip()
  567. {
  568. $user1 = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  569. $post1 = $user1->post()->firstOrNew(['name' => 'First Post'], ['name' => 'New Post']);
  570. $this->assertSame('New Post', $post1->name);
  571. $user2 = EloquentTestUser::create(['email' => 'abigailotwell@gmail.com']);
  572. $post = $user2->post()->create(['name' => 'First Post']);
  573. $post2 = $user2->post()->firstOrNew(['name' => 'First Post'], ['name' => 'New Post']);
  574. $this->assertSame('First Post', $post2->name);
  575. $this->assertSame($post->id, $post2->id);
  576. }
  577. public function testFirstOrCreateOnHasOneRelationShip()
  578. {
  579. $user1 = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  580. $post1 = $user1->post()->firstOrCreate(['name' => 'First Post'], ['name' => 'New Post']);
  581. $this->assertSame('New Post', $post1->name);
  582. $user2 = EloquentTestUser::create(['email' => 'abigailotwell@gmail.com']);
  583. $post = $user2->post()->create(['name' => 'First Post']);
  584. $post2 = $user2->post()->firstOrCreate(['name' => 'First Post'], ['name' => 'New Post']);
  585. $this->assertSame('First Post', $post2->name);
  586. $this->assertSame($post->id, $post2->id);
  587. }
  588. public function testHasOnSelfReferencingBelongsToManyRelationship()
  589. {
  590. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  591. $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  592. $this->assertTrue(isset($user->friends[0]->id));
  593. $results = EloquentTestUser::has('friends')->get();
  594. $this->assertCount(1, $results);
  595. $this->assertSame('taylorotwell@gmail.com', $results->first()->email);
  596. }
  597. public function testWhereHasOnSelfReferencingBelongsToManyRelationship()
  598. {
  599. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  600. $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  601. $results = EloquentTestUser::whereHas('friends', function ($query) {
  602. $query->where('email', 'abigailotwell@gmail.com');
  603. })->get();
  604. $this->assertCount(1, $results);
  605. $this->assertSame('taylorotwell@gmail.com', $results->first()->email);
  606. }
  607. public function testHasOnNestedSelfReferencingBelongsToManyRelationship()
  608. {
  609. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  610. $friend = $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  611. $friend->friends()->create(['email' => 'foo@gmail.com']);
  612. $results = EloquentTestUser::has('friends.friends')->get();
  613. $this->assertCount(1, $results);
  614. $this->assertSame('taylorotwell@gmail.com', $results->first()->email);
  615. }
  616. public function testWhereHasOnNestedSelfReferencingBelongsToManyRelationship()
  617. {
  618. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  619. $friend = $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  620. $friend->friends()->create(['email' => 'foo@gmail.com']);
  621. $results = EloquentTestUser::whereHas('friends.friends', function ($query) {
  622. $query->where('email', 'foo@gmail.com');
  623. })->get();
  624. $this->assertCount(1, $results);
  625. $this->assertSame('taylorotwell@gmail.com', $results->first()->email);
  626. }
  627. public function testHasOnSelfReferencingBelongsToManyRelationshipWithWherePivot()
  628. {
  629. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  630. $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  631. $results = EloquentTestUser::has('friendsOne')->get();
  632. $this->assertCount(1, $results);
  633. $this->assertSame('taylorotwell@gmail.com', $results->first()->email);
  634. }
  635. public function testHasOnNestedSelfReferencingBelongsToManyRelationshipWithWherePivot()
  636. {
  637. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  638. $friend = $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  639. $friend->friends()->create(['email' => 'foo@gmail.com']);
  640. $results = EloquentTestUser::has('friendsOne.friendsTwo')->get();
  641. $this->assertCount(1, $results);
  642. $this->assertSame('taylorotwell@gmail.com', $results->first()->email);
  643. }
  644. public function testHasOnSelfReferencingBelongsToRelationship()
  645. {
  646. $parentPost = EloquentTestPost::create(['name' => 'Parent Post', 'user_id' => 1]);
  647. EloquentTestPost::create(['name' => 'Child Post', 'parent_id' => $parentPost->id, 'user_id' => 2]);
  648. $results = EloquentTestPost::has('parentPost')->get();
  649. $this->assertCount(1, $results);
  650. $this->assertSame('Child Post', $results->first()->name);
  651. }
  652. public function testAggregatedValuesOfDatetimeField()
  653. {
  654. EloquentTestUser::create(['id' => 1, 'email' => 'test1@test.test', 'created_at' => '2016-08-10 09:21:00', 'updated_at' => Carbon::now()]);
  655. EloquentTestUser::create(['id' => 2, 'email' => 'test2@test.test', 'created_at' => '2016-08-01 12:00:00', 'updated_at' => Carbon::now()]);
  656. $this->assertSame('2016-08-10 09:21:00', EloquentTestUser::max('created_at'));
  657. $this->assertSame('2016-08-01 12:00:00', EloquentTestUser::min('created_at'));
  658. }
  659. public function testWhereHasOnSelfReferencingBelongsToRelationship()
  660. {
  661. $parentPost = EloquentTestPost::create(['name' => 'Parent Post', 'user_id' => 1]);
  662. EloquentTestPost::create(['name' => 'Child Post', 'parent_id' => $parentPost->id, 'user_id' => 2]);
  663. $results = EloquentTestPost::whereHas('parentPost', function ($query) {
  664. $query->where('name', 'Parent Post');
  665. })->get();
  666. $this->assertCount(1, $results);
  667. $this->assertSame('Child Post', $results->first()->name);
  668. }
  669. public function testHasOnNestedSelfReferencingBelongsToRelationship()
  670. {
  671. $grandParentPost = EloquentTestPost::create(['name' => 'Grandparent Post', 'user_id' => 1]);
  672. $parentPost = EloquentTestPost::create(['name' => 'Parent Post', 'parent_id' => $grandParentPost->id, 'user_id' => 2]);
  673. EloquentTestPost::create(['name' => 'Child Post', 'parent_id' => $parentPost->id, 'user_id' => 3]);
  674. $results = EloquentTestPost::has('parentPost.parentPost')->get();
  675. $this->assertCount(1, $results);
  676. $this->assertSame('Child Post', $results->first()->name);
  677. }
  678. public function testWhereHasOnNestedSelfReferencingBelongsToRelationship()
  679. {
  680. $grandParentPost = EloquentTestPost::create(['name' => 'Grandparent Post', 'user_id' => 1]);
  681. $parentPost = EloquentTestPost::create(['name' => 'Parent Post', 'parent_id' => $grandParentPost->id, 'user_id' => 2]);
  682. EloquentTestPost::create(['name' => 'Child Post', 'parent_id' => $parentPost->id, 'user_id' => 3]);
  683. $results = EloquentTestPost::whereHas('parentPost.parentPost', function ($query) {
  684. $query->where('name', 'Grandparent Post');
  685. })->get();
  686. $this->assertCount(1, $results);
  687. $this->assertSame('Child Post', $results->first()->name);
  688. }
  689. public function testHasOnSelfReferencingHasManyRelationship()
  690. {
  691. $parentPost = EloquentTestPost::create(['name' => 'Parent Post', 'user_id' => 1]);
  692. EloquentTestPost::create(['name' => 'Child Post', 'parent_id' => $parentPost->id, 'user_id' => 2]);
  693. $results = EloquentTestPost::has('childPosts')->get();
  694. $this->assertCount(1, $results);
  695. $this->assertSame('Parent Post', $results->first()->name);
  696. }
  697. public function testWhereHasOnSelfReferencingHasManyRelationship()
  698. {
  699. $parentPost = EloquentTestPost::create(['name' => 'Parent Post', 'user_id' => 1]);
  700. EloquentTestPost::create(['name' => 'Child Post', 'parent_id' => $parentPost->id, 'user_id' => 2]);
  701. $results = EloquentTestPost::whereHas('childPosts', function ($query) {
  702. $query->where('name', 'Child Post');
  703. })->get();
  704. $this->assertCount(1, $results);
  705. $this->assertSame('Parent Post', $results->first()->name);
  706. }
  707. public function testHasOnNestedSelfReferencingHasManyRelationship()
  708. {
  709. $grandParentPost = EloquentTestPost::create(['name' => 'Grandparent Post', 'user_id' => 1]);
  710. $parentPost = EloquentTestPost::create(['name' => 'Parent Post', 'parent_id' => $grandParentPost->id, 'user_id' => 2]);
  711. EloquentTestPost::create(['name' => 'Child Post', 'parent_id' => $parentPost->id, 'user_id' => 3]);
  712. $results = EloquentTestPost::has('childPosts.childPosts')->get();
  713. $this->assertCount(1, $results);
  714. $this->assertSame('Grandparent Post', $results->first()->name);
  715. }
  716. public function testWhereHasOnNestedSelfReferencingHasManyRelationship()
  717. {
  718. $grandParentPost = EloquentTestPost::create(['name' => 'Grandparent Post', 'user_id' => 1]);
  719. $parentPost = EloquentTestPost::create(['name' => 'Parent Post', 'parent_id' => $grandParentPost->id, 'user_id' => 2]);
  720. EloquentTestPost::create(['name' => 'Child Post', 'parent_id' => $parentPost->id, 'user_id' => 3]);
  721. $results = EloquentTestPost::whereHas('childPosts.childPosts', function ($query) {
  722. $query->where('name', 'Child Post');
  723. })->get();
  724. $this->assertCount(1, $results);
  725. $this->assertSame('Grandparent Post', $results->first()->name);
  726. }
  727. public function testHasWithNonWhereBindings()
  728. {
  729. $user = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  730. $user->posts()->create(['name' => 'Post 2'])
  731. ->photos()->create(['name' => 'photo.jpg']);
  732. $query = EloquentTestUser::has('postWithPhotos');
  733. $bindingsCount = count($query->getBindings());
  734. $questionMarksCount = substr_count($query->toSql(), '?');
  735. $this->assertEquals($questionMarksCount, $bindingsCount);
  736. }
  737. public function testHasOnMorphToRelationship()
  738. {
  739. $post = EloquentTestPost::create(['name' => 'Morph Post', 'user_id' => 1]);
  740. (new EloquentTestPhoto)->imageable()->associate($post)->fill(['name' => 'Morph Photo'])->save();
  741. $photos = EloquentTestPhoto::has('imageable')->get();
  742. $this->assertEquals(1, $photos->count());
  743. }
  744. public function testBelongsToManyRelationshipModelsAreProperlyHydratedWithSoleQuery()
  745. {
  746. $user = EloquentTestUserWithCustomFriendPivot::create(['email' => 'taylorotwell@gmail.com']);
  747. $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  748. $user->friends()->get()->each(function ($friend) {
  749. $this->assertInstanceOf(EloquentTestFriendPivot::class, $friend->pivot);
  750. });
  751. $soleFriend = $user->friends()->where('email', 'abigailotwell@gmail.com')->sole();
  752. $this->assertInstanceOf(EloquentTestFriendPivot::class, $soleFriend->pivot);
  753. }
  754. public function testBelongsToManyRelationshipMissingModelExceptionWithSoleQueryWorks()
  755. {
  756. $this->expectException(ModelNotFoundException::class);
  757. $user = EloquentTestUserWithCustomFriendPivot::create(['email' => 'taylorotwell@gmail.com']);
  758. $user->friends()->where('email', 'abigailotwell@gmail.com')->sole();
  759. }
  760. public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverChunkedRequest()
  761. {
  762. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  763. $friend = $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  764. EloquentTestUser::first()->friends()->chunk(2, function ($friends) use ($user, $friend) {
  765. $this->assertCount(1, $friends);
  766. $this->assertSame('abigailotwell@gmail.com', $friends->first()->email);
  767. $this->assertEquals($user->id, $friends->first()->pivot->user_id);
  768. $this->assertEquals($friend->id, $friends->first()->pivot->friend_id);
  769. });
  770. }
  771. public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverEachRequest()
  772. {
  773. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  774. $friend = $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  775. EloquentTestUser::first()->friends()->each(function ($result) use ($user, $friend) {
  776. $this->assertSame('abigailotwell@gmail.com', $result->email);
  777. $this->assertEquals($user->id, $result->pivot->user_id);
  778. $this->assertEquals($friend->id, $result->pivot->friend_id);
  779. });
  780. }
  781. public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverCursorRequest()
  782. {
  783. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  784. $friend = $user->friends()->create(['email' => 'abigailotwell@gmail.com']);
  785. foreach (EloquentTestUser::first()->friends()->cursor() as $result) {
  786. $this->assertSame('abigailotwell@gmail.com', $result->email);
  787. $this->assertEquals($user->id, $result->pivot->user_id);
  788. $this->assertEquals($friend->id, $result->pivot->friend_id);
  789. }
  790. }
  791. public function testBasicHasManyEagerLoading()
  792. {
  793. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  794. $user->posts()->create(['name' => 'First Post']);
  795. $user = EloquentTestUser::with('posts')->where('email', 'taylorotwell@gmail.com')->first();
  796. $this->assertSame('First Post', $user->posts->first()->name);
  797. $post = EloquentTestPost::with('user')->where('name', 'First Post')->get();
  798. $this->assertSame('taylorotwell@gmail.com', $post->first()->user->email);
  799. }
  800. public function testBasicNestedSelfReferencingHasManyEagerLoading()
  801. {
  802. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  803. $post = $user->posts()->create(['name' => 'First Post']);
  804. $post->childPosts()->create(['name' => 'Child Post', 'user_id' => $user->id]);
  805. $user = EloquentTestUser::with('posts.childPosts')->where('email', 'taylorotwell@gmail.com')->first();
  806. $this->assertNotNull($user->posts->first());
  807. $this->assertSame('First Post', $user->posts->first()->name);
  808. $this->assertNotNull($user->posts->first()->childPosts->first());
  809. $this->assertSame('Child Post', $user->posts->first()->childPosts->first()->name);
  810. $post = EloquentTestPost::with('parentPost.user')->where('name', 'Child Post')->get();
  811. $this->assertNotNull($post->first()->parentPost);
  812. $this->assertNotNull($post->first()->parentPost->user);
  813. $this->assertSame('taylorotwell@gmail.com', $post->first()->parentPost->user->email);
  814. }
  815. public function testBasicMorphManyRelationship()
  816. {
  817. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  818. $user->photos()->create(['name' => 'Avatar 1']);
  819. $user->photos()->create(['name' => 'Avatar 2']);
  820. $post = $user->posts()->create(['name' => 'First Post']);
  821. $post->photos()->create(['name' => 'Hero 1']);
  822. $post->photos()->create(['name' => 'Hero 2']);
  823. $this->assertInstanceOf(Collection::class, $user->photos);
  824. $this->assertInstanceOf(EloquentTestPhoto::class, $user->photos[0]);
  825. $this->assertInstanceOf(Collection::class, $post->photos);
  826. $this->assertInstanceOf(EloquentTestPhoto::class, $post->photos[0]);
  827. $this->assertCount(2, $user->photos);
  828. $this->assertCount(2, $post->photos);
  829. $this->assertSame('Avatar 1', $user->photos[0]->name);
  830. $this->assertSame('Avatar 2', $user->photos[1]->name);
  831. $this->assertSame('Hero 1', $post->photos[0]->name);
  832. $this->assertSame('Hero 2', $post->photos[1]->name);
  833. $photos = EloquentTestPhoto::orderBy('name')->get();
  834. $this->assertInstanceOf(Collection::class, $photos);
  835. $this->assertCount(4, $photos);
  836. $this->assertInstanceOf(EloquentTestUser::class, $photos[0]->imageable);
  837. $this->assertInstanceOf(EloquentTestPost::class, $photos[2]->imageable);
  838. $this->assertSame('taylorotwell@gmail.com', $photos[1]->imageable->email);
  839. $this->assertSame('First Post', $photos[3]->imageable->name);
  840. }
  841. public function testMorphMapIsUsedForCreatingAndFetchingThroughRelation()
  842. {
  843. Relation::morphMap([
  844. 'user' => EloquentTestUser::class,
  845. 'post' => EloquentTestPost::class,
  846. ]);
  847. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  848. $user->photos()->create(['name' => 'Avatar 1']);
  849. $user->photos()->create(['name' => 'Avatar 2']);
  850. $post = $user->posts()->create(['name' => 'First Post']);
  851. $post->photos()->create(['name' => 'Hero 1']);
  852. $post->photos()->create(['name' => 'Hero 2']);
  853. $this->assertInstanceOf(Collection::class, $user->photos);
  854. $this->assertInstanceOf(EloquentTestPhoto::class, $user->photos[0]);
  855. $this->assertInstanceOf(Collection::class, $post->photos);
  856. $this->assertInstanceOf(EloquentTestPhoto::class, $post->photos[0]);
  857. $this->assertCount(2, $user->photos);
  858. $this->assertCount(2, $post->photos);
  859. $this->assertSame('Avatar 1', $user->photos[0]->name);
  860. $this->assertSame('Avatar 2', $user->photos[1]->name);
  861. $this->assertSame('Hero 1', $post->photos[0]->name);
  862. $this->assertSame('Hero 2', $post->photos[1]->name);
  863. $this->assertSame('user', $user->photos[0]->imageable_type);
  864. $this->assertSame('user', $user->photos[1]->imageable_type);
  865. $this->assertSame('post', $post->photos[0]->imageable_type);
  866. $this->assertSame('post', $post->photos[1]->imageable_type);
  867. }
  868. public function testMorphMapIsUsedWhenFetchingParent()
  869. {
  870. Relation::morphMap([
  871. 'user' => EloquentTestUser::class,
  872. 'post' => EloquentTestPost::class,
  873. ]);
  874. $user = EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  875. $user->photos()->create(['name' => 'Avatar 1']);
  876. $photo = EloquentTestPhoto::first();
  877. $this->assertSame('user', $photo->imageable_type);
  878. $this->assertInstanceOf(EloquentTestUser::class, $photo->imageable);
  879. }
  880. public function testMorphMapIsMergedByDefault()
  881. {
  882. $map1 = [
  883. 'user' => EloquentTestUser::class,
  884. ];
  885. $map2 = [
  886. 'post' => EloquentTestPost::class,
  887. ];
  888. Relation::morphMap($map1);
  889. Relation::morphMap($map2);
  890. $this->assertEquals(array_merge($map1, $map2), Relation::morphMap());
  891. }
  892. public function testMorphMapOverwritesCurrentMap()
  893. {
  894. $map1 = [
  895. 'user' => EloquentTestUser::class,
  896. ];
  897. $map2 = [
  898. 'post' => EloquentTestPost::class,
  899. ];
  900. Relation::morphMap($map1, false);
  901. $this->assertEquals($map1, Relation::morphMap());
  902. Relation::morphMap($map2, false);
  903. $this->assertEquals($map2, Relation::morphMap());
  904. }
  905. public function testEmptyMorphToRelationship()
  906. {
  907. $photo = new EloquentTestPhoto;
  908. $this->assertNull($photo->imageable);
  909. }
  910. public function testSaveOrFail()
  911. {
  912. $date = '1970-01-01';
  913. $post = new EloquentTestPost([
  914. 'user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date,
  915. ]);
  916. $this->assertTrue($post->saveOrFail());
  917. $this->assertEquals(1, EloquentTestPost::count());
  918. }
  919. public function testSavingJSONFields()
  920. {
  921. $model = EloquentTestWithJSON::create(['json' => ['x' => 0]]);
  922. $this->assertEquals(['x' => 0], $model->json);
  923. $model->fillable(['json->y', 'json->a->b']);
  924. $model->update(['json->y' => '1']);
  925. $this->assertArrayNotHasKey('json->y', $model->toArray());
  926. $this->assertEquals(['x' => 0, 'y' => 1], $model->json);
  927. $model->update(['json->a->b' => '3']);
  928. $this->assertArrayNotHasKey('json->a->b', $model->toArray());
  929. $this->assertEquals(['x' => 0, 'y' => 1, 'a' => ['b' => 3]], $model->json);
  930. }
  931. public function testSaveOrFailWithDuplicatedEntry()
  932. {
  933. $this->expectException(QueryException::class);
  934. $this->expectExceptionMessage('SQLSTATE[23000]:');
  935. $date = '1970-01-01';
  936. EloquentTestPost::create([
  937. 'id' => 1, 'user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date,
  938. ]);
  939. $post = new EloquentTestPost([
  940. 'id' => 1, 'user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date,
  941. ]);
  942. $post->saveOrFail();
  943. }
  944. public function testMultiInsertsWithDifferentValues()
  945. {
  946. $date = '1970-01-01';
  947. $result = EloquentTestPost::insert([
  948. ['user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date],
  949. ['user_id' => 2, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date],
  950. ]);
  951. $this->assertTrue($result);
  952. $this->assertEquals(2, EloquentTestPost::count());
  953. }
  954. public function testMultiInsertsWithSameValues()
  955. {
  956. $date = '1970-01-01';
  957. $result = EloquentTestPost::insert([
  958. ['user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date],
  959. ['user_id' => 1, 'name' => 'Post', 'created_at' => $date, 'updated_at' => $date],
  960. ]);
  961. $this->assertTrue($result);
  962. $this->assertEquals(2, EloquentTestPost::count());
  963. }
  964. public function testNestedTransactions()
  965. {
  966. $user = EloquentTestUser::create(['email' => 'taylor@laravel.com']);
  967. $this->connection()->transaction(function () use ($user) {
  968. try {
  969. $this->connection()->transaction(function () use ($user) {
  970. $user->email = 'otwell@laravel.com';
  971. $user->save();
  972. throw new Exception;
  973. });
  974. } catch (Exception $e) {
  975. // ignore the exception
  976. }
  977. $user = EloquentTestUser::first();
  978. $this->assertSame('taylor@laravel.com', $user->email);
  979. });
  980. }
  981. public function testNestedTransactionsUsingSaveOrFailWillSucceed()
  982. {
  983. $user = EloquentTestUser::create(['email' => 'taylor@laravel.com']);
  984. $this->connection()->transaction(function () use ($user) {
  985. try {
  986. $user->email = 'otwell@laravel.com';
  987. $user->saveOrFail();
  988. } catch (Exception $e) {
  989. // ignore the exception
  990. }
  991. $user = EloquentTestUser::first();
  992. $this->assertSame('otwell@laravel.com', $user->email);
  993. $this->assertEquals(1, $user->id);
  994. });
  995. }
  996. public function testNestedTransactionsUsingSaveOrFailWillFails()
  997. {
  998. $user = EloquentTestUser::create(['email' => 'taylor@laravel.com']);
  999. $this->connection()->transaction(function () use ($user) {
  1000. try {
  1001. $user->id = 'invalid';
  1002. $user->email = 'otwell@laravel.com';
  1003. $user->saveOrFail();
  1004. } catch (Exception $e) {
  1005. // ignore the exception
  1006. }
  1007. $user = EloquentTestUser::first();
  1008. $this->assertSame('taylor@laravel.com', $user->email);
  1009. $this->assertEquals(1, $user->id);
  1010. });
  1011. }
  1012. public function testToArrayIncludesDefaultFormattedTimestamps()
  1013. {
  1014. $model = new EloquentTestUser;
  1015. $model->setRawAttributes([
  1016. 'created_at' => '2012-12-04',
  1017. 'updated_at' => '2012-12-05',
  1018. ]);
  1019. $array = $model->toArray();
  1020. $this->assertSame('2012-12-04T00:00:00.000000Z', $array['created_at']);
  1021. $this->assertSame('2012-12-05T00:00:00.000000Z', $array['updated_at']);
  1022. }
  1023. public function testToArrayIncludesCustomFormattedTimestamps()
  1024. {
  1025. $model = new EloquentTestUserWithCustomDateSerialization;
  1026. $model->setRawAttributes([
  1027. 'created_at' => '2012-12-04',
  1028. 'updated_at' => '2012-12-05',
  1029. ]);
  1030. $array = $model->toArray();
  1031. $this->assertSame('04-12-12', $array['created_at']);
  1032. $this->assertSame('05-12-12', $array['updated_at']);
  1033. }
  1034. public function testIncrementingPrimaryKeysAreCastToIntegersByDefault()
  1035. {
  1036. EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  1037. $user = EloquentTestUser::first();
  1038. $this->assertIsInt($user->id);
  1039. }
  1040. public function testDefaultIncrementingPrimaryKeyIntegerCastCanBeOverwritten()
  1041. {
  1042. EloquentTestUserWithStringCastId::create(['email' => 'taylorotwell@gmail.com']);
  1043. $user = EloquentTestUserWithStringCastId::first();
  1044. $this->assertIsString($user->id);
  1045. }
  1046. public function testRelationsArePreloadedInGlobalScope()
  1047. {
  1048. $user = EloquentTestUserWithGlobalScope::create(['email' => 'taylorotwell@gmail.com']);
  1049. $user->posts()->create(['name' => 'My Post']);
  1050. $result = EloquentTestUserWithGlobalScope::first();
  1051. $this->assertCount(1, $result->getRelations());
  1052. }
  1053. public function testModelIgnoredByGlobalScopeCanBeRefreshed()
  1054. {
  1055. $user = EloquentTestUserWithOmittingGlobalScope::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1056. $this->assertNotNull($user->fresh());
  1057. }
  1058. public function testGlobalScopeCanBeRemovedByOtherGlobalScope()
  1059. {
  1060. $user = EloquentTestUserWithGlobalScopeRemovingOtherScope::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1061. $user->delete();
  1062. $this->assertNotNull(EloquentTestUserWithGlobalScopeRemovingOtherScope::find($user->id));
  1063. }
  1064. public function testForPageBeforeIdCorrectlyPaginates()
  1065. {
  1066. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1067. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  1068. $results = EloquentTestUser::forPageBeforeId(15, 2);
  1069. $this->assertInstanceOf(Builder::class, $results);
  1070. $this->assertEquals(1, $results->first()->id);
  1071. $results = EloquentTestUser::orderBy('id', 'desc')->forPageBeforeId(15, 2);
  1072. $this->assertInstanceOf(Builder::class, $results);
  1073. $this->assertEquals(1, $results->first()->id);
  1074. }
  1075. public function testForPageAfterIdCorrectlyPaginates()
  1076. {
  1077. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1078. EloquentTestUser::create(['id' => 2, 'email' => 'abigailotwell@gmail.com']);
  1079. $results = EloquentTestUser::forPageAfterId(15, 1);
  1080. $this->assertInstanceOf(Builder::class, $results);
  1081. $this->assertEquals(2, $results->first()->id);
  1082. $results = EloquentTestUser::orderBy('id', 'desc')->forPageAfterId(15, 1);
  1083. $this->assertInstanceOf(Builder::class, $results);
  1084. $this->assertEquals(2, $results->first()->id);
  1085. }
  1086. public function testMorphToRelationsAcrossDatabaseConnections()
  1087. {
  1088. $item = null;
  1089. EloquentTestItem::create(['id' => 1]);
  1090. EloquentTestOrder::create(['id' => 1, 'item_type' => EloquentTestItem::class, 'item_id' => 1]);
  1091. try {
  1092. $item = EloquentTestOrder::first()->item;
  1093. } catch (Exception $e) {
  1094. // ignore the exception
  1095. }
  1096. $this->assertInstanceOf(EloquentTestItem::class, $item);
  1097. }
  1098. public function testEagerLoadedMorphToRelationsOnAnotherDatabaseConnection()
  1099. {
  1100. EloquentTestPost::create(['id' => 1, 'name' => 'Default Connection Post', 'user_id' => 1]);
  1101. EloquentTestPhoto::create(['id' => 1, 'imageable_type' => EloquentTestPost::class, 'imageable_id' => 1, 'name' => 'Photo']);
  1102. EloquentTestPost::on('second_connection')
  1103. ->create(['id' => 1, 'name' => 'Second Connection Post', 'user_id' => 1]);
  1104. EloquentTestPhoto::on('second_connection')
  1105. ->create(['id' => 1, 'imageable_type' => EloquentTestPost::class, 'imageable_id' => 1, 'name' => 'Photo']);
  1106. $defaultConnectionPost = EloquentTestPhoto::with('imageable')->first()->imageable;
  1107. $secondConnectionPost = EloquentTestPhoto::on('second_connection')->with('imageable')->first()->imageable;
  1108. $this->assertSame('Default Connection Post', $defaultConnectionPost->name);
  1109. $this->assertSame('Second Connection Post', $secondConnectionPost->name);
  1110. }
  1111. public function testBelongsToManyCustomPivot()
  1112. {
  1113. $john = EloquentTestUserWithCustomFriendPivot::create(['id' => 1, 'name' => 'John Doe', 'email' => 'johndoe@example.com']);
  1114. $jane = EloquentTestUserWithCustomFriendPivot::create(['id' => 2, 'name' => 'Jane Doe', 'email' => 'janedoe@example.com']);
  1115. $jack = EloquentTestUserWithCustomFriendPivot::create(['id' => 3, 'name' => 'Jack Doe', 'email' => 'jackdoe@example.com']);
  1116. $jule = EloquentTestUserWithCustomFriendPivot::create(['id' => 4, 'name' => 'Jule Doe', 'email' => 'juledoe@example.com']);
  1117. EloquentTestFriendLevel::create(['id' => 1, 'level' => 'acquaintance']);
  1118. EloquentTestFriendLevel::create(['id' => 2, 'level' => 'friend']);
  1119. EloquentTestFriendLevel::create(['id' => 3, 'level' => 'bff']);
  1120. $john->friends()->attach($jane, ['friend_level_id' => 1]);
  1121. $john->friends()->attach($jack, ['friend_level_id' => 2]);
  1122. $john->friends()->attach($jule, ['friend_level_id' => 3]);
  1123. $johnWithFriends = EloquentTestUserWithCustomFriendPivot::with('friends')->find(1);
  1124. $this->assertCount(3, $johnWithFriends->friends);
  1125. $this->assertSame('friend', $johnWithFriends->friends->find(3)->pivot->level->level);
  1126. $this->assertSame('Jule Doe', $johnWithFriends->friends->find(4)->pivot->friend->name);
  1127. }
  1128. public function testIsAfterRetrievingTheSameModel()
  1129. {
  1130. $saved = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1131. $retrieved = EloquentTestUser::find(1);
  1132. $this->assertTrue($saved->is($retrieved));
  1133. }
  1134. public function testFreshMethodOnModel()
  1135. {
  1136. $now = Carbon::now();
  1137. $nowSerialized = $now->startOfSecond()->toJSON();
  1138. $nowWithFractionsSerialized = $now->toJSON();
  1139. Carbon::setTestNow($now);
  1140. $storedUser1 = EloquentTestUser::create([
  1141. 'id' => 1,
  1142. 'email' => 'taylorotwell@gmail.com',
  1143. 'birthday' => $now,
  1144. ]);
  1145. $storedUser1->newQuery()->update([
  1146. 'email' => 'dev@mathieutu.ovh',
  1147. 'name' => 'Mathieu TUDISCO',
  1148. ]);
  1149. $freshStoredUser1 = $storedUser1->fresh();
  1150. $storedUser2 = EloquentTestUser::create([
  1151. 'id' => 2,
  1152. 'email' => 'taylorotwell@gmail.com',
  1153. 'birthday' => $now,
  1154. ]);
  1155. $storedUser2->newQuery()->update(['email' => 'dev@mathieutu.ovh']);
  1156. $freshStoredUser2 = $storedUser2->fresh();
  1157. $notStoredUser = new EloquentTestUser([
  1158. 'id' => 3,
  1159. 'email' => 'taylorotwell@gmail.com',
  1160. 'birthday' => $now,
  1161. ]);
  1162. $freshNotStoredUser = $notStoredUser->fresh();
  1163. $this->assertEquals([
  1164. 'id' => 1,
  1165. 'email' => 'taylorotwell@gmail.com',
  1166. 'birthday' => $nowWithFractionsSerialized,
  1167. 'created_at' => $nowSerialized,
  1168. 'updated_at' => $nowSerialized,
  1169. ], $storedUser1->toArray());
  1170. $this->assertEquals([
  1171. 'id' => 1,
  1172. 'name' => 'Mathieu TUDISCO',
  1173. 'email' => 'dev@mathieutu.ovh',
  1174. 'birthday' => $nowWithFractionsSerialized,
  1175. 'created_at' => $nowSerialized,
  1176. 'updated_at' => $nowSerialized,
  1177. ], $freshStoredUser1->toArray());
  1178. $this->assertInstanceOf(EloquentTestUser::class, $storedUser1);
  1179. $this->assertEquals([
  1180. 'id' => 2,
  1181. 'email' => 'taylorotwell@gmail.com',
  1182. 'birthday' => $nowWithFractionsSerialized,
  1183. 'created_at' => $nowSerialized,
  1184. 'updated_at' => $nowSerialized,
  1185. ], $storedUser2->toArray());
  1186. $this->assertEquals([
  1187. 'id' => 2,
  1188. 'name' => null,
  1189. 'email' => 'dev@mathieutu.ovh',
  1190. 'birthday' => $nowWithFractionsSerialized,
  1191. 'created_at' => $nowSerialized,
  1192. 'updated_at' => $nowSerialized,
  1193. ], $freshStoredUser2->toArray());
  1194. $this->assertInstanceOf(EloquentTestUser::class, $storedUser2);
  1195. $this->assertEquals([
  1196. 'id' => 3,
  1197. 'email' => 'taylorotwell@gmail.com',
  1198. 'birthday' => $nowWithFractionsSerialized,
  1199. ], $notStoredUser->toArray());
  1200. $this->assertNull($freshNotStoredUser);
  1201. }
  1202. public function testFreshMethodOnCollection()
  1203. {
  1204. EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1205. EloquentTestUser::create(['id' => 2, 'email' => 'taylorotwell@gmail.com']);
  1206. $users = EloquentTestUser::all()
  1207. ->add(new EloquentTestUser(['id' => 3, 'email' => 'taylorotwell@gmail.com']));
  1208. EloquentTestUser::find(1)->update(['name' => 'Mathieu TUDISCO']);
  1209. EloquentTestUser::find(2)->update(['email' => 'dev@mathieutu.ovh']);
  1210. $this->assertCount(3, $users);
  1211. $this->assertNotSame('Mathieu TUDISCO', $users[0]->name);
  1212. $this->assertNotSame('dev@mathieutu.ovh', $users[1]->email);
  1213. $refreshedUsers = $users->fresh();
  1214. $this->assertCount(2, $refreshedUsers);
  1215. $this->assertSame('Mathieu TUDISCO', $refreshedUsers[0]->name);
  1216. $this->assertSame('dev@mathieutu.ovh', $refreshedUsers[1]->email);
  1217. }
  1218. public function testTimestampsUsingDefaultDateFormat()
  1219. {
  1220. $model = new EloquentTestUser;
  1221. $model->setDateFormat('Y-m-d H:i:s'); // Default MySQL/PostgreSQL/SQLite date format
  1222. $model->setRawAttributes([
  1223. 'created_at' => '2017-11-14 08:23:19',
  1224. ]);
  1225. $this->assertSame('2017-11-14 08:23:19', $model->fromDateTime($model->getAttribute('created_at')));
  1226. }
  1227. public function testTimestampsUsingDefaultSqlServerDateFormat()
  1228. {
  1229. $model = new EloquentTestUser;
  1230. $model->setDateFormat('Y-m-d H:i:s.v'); // Default SQL Server date format
  1231. $model->setRawAttributes([
  1232. 'created_at' => '2017-11-14 08:23:19.000',
  1233. 'updated_at' => '2017-11-14 08:23:19.734',
  1234. ]);
  1235. $this->assertSame('2017-11-14 08:23:19.000', $model->fromDateTime($model->getAttribute('created_at')));
  1236. $this->assertSame('2017-11-14 08:23:19.734', $model->fromDateTime($model->getAttribute('updated_at')));
  1237. }
  1238. public function testTimestampsUsingCustomDateFormat()
  1239. {
  1240. // Simulating using custom precisions with timestamps(4)
  1241. $model = new EloquentTestUser;
  1242. $model->setDateFormat('Y-m-d H:i:s.u'); // Custom date format
  1243. $model->setRawAttributes([
  1244. 'created_at' => '2017-11-14 08:23:19.0000',
  1245. 'updated_at' => '2017-11-14 08:23:19.7348',
  1246. ]);
  1247. // Note: when storing databases would truncate the value to the given precision
  1248. $this->assertSame('2017-11-14 08:23:19.000000', $model->fromDateTime($model->getAttribute('created_at')));
  1249. $this->assertSame('2017-11-14 08:23:19.734800', $model->fromDateTime($model->getAttribute('updated_at')));
  1250. }
  1251. public function testTimestampsUsingOldSqlServerDateFormat()
  1252. {
  1253. $model = new EloquentTestUser;
  1254. $model->setDateFormat('Y-m-d H:i:s.000'); // Old SQL Server date format
  1255. $model->setRawAttributes([
  1256. 'created_at' => '2017-11-14 08:23:19.000',
  1257. ]);
  1258. $this->assertSame('2017-11-14 08:23:19.000', $model->fromDateTime($model->getAttribute('created_at')));
  1259. }
  1260. public function testTimestampsUsingOldSqlServerDateFormatFallbackToDefaultParsing()
  1261. {
  1262. $model = new EloquentTestUser;
  1263. $model->setDateFormat('Y-m-d H:i:s.000'); // Old SQL Server date format
  1264. $model->setRawAttributes([
  1265. 'updated_at' => '2017-11-14 08:23:19.734',
  1266. ]);
  1267. $date = $model->getAttribute('updated_at');
  1268. $this->assertSame('2017-11-14 08:23:19.734', $date->format('Y-m-d H:i:s.v'), 'the date should contains the precision');
  1269. $this->assertSame('2017-11-14 08:23:19.000', $model->fromDateTime($date), 'the format should trims it');
  1270. // No longer throwing exception since Laravel 7,
  1271. // but Date::hasFormat() can be used instead to check date formatting:
  1272. $this->assertTrue(Date::hasFormat('2017-11-14 08:23:19.000', $model->getDateFormat()));
  1273. $this->assertFalse(Date::hasFormat('2017-11-14 08:23:19.734', $model->getDateFormat()));
  1274. }
  1275. public function testSpecialFormats()
  1276. {
  1277. $model = new EloquentTestUser;
  1278. $model->setDateFormat('!Y-d-m \\Y');
  1279. $model->setRawAttributes([
  1280. 'updated_at' => '2017-05-11 Y',
  1281. ]);
  1282. $date = $model->getAttribute('updated_at');
  1283. $this->assertSame('2017-11-05 00:00:00.000000', $date->format('Y-m-d H:i:s.u'), 'the date should respect the whole format');
  1284. $model->setDateFormat('Y d m|');
  1285. $model->setRawAttributes([
  1286. 'updated_at' => '2020 11 09',
  1287. ]);
  1288. $date = $model->getAttribute('updated_at');
  1289. $this->assertSame('2020-09-11 00:00:00.000000', $date->format('Y-m-d H:i:s.u'), 'the date should respect the whole format');
  1290. $model->setDateFormat('Y d m|*');
  1291. $model->setRawAttributes([
  1292. 'updated_at' => '2020 11 09 foo',
  1293. ]);
  1294. $date = $model->getAttribute('updated_at');
  1295. $this->assertSame('2020-09-11 00:00:00.000000', $date->format('Y-m-d H:i:s.u'), 'the date should respect the whole format');
  1296. }
  1297. public function testUpdatingChildModelTouchesParent()
  1298. {
  1299. $before = Carbon::now();
  1300. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1301. $post = EloquentTouchingPost::create(['name' => 'Parent Post', 'user_id' => 1]);
  1302. $this->assertTrue($before->isSameDay($user->updated_at));
  1303. $this->assertTrue($before->isSameDay($post->updated_at));
  1304. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1305. $post->update(['name' => 'Updated']);
  1306. $this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching model own timestamps.');
  1307. $this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.');
  1308. Carbon::setTestNow($before);
  1309. }
  1310. public function testMultiLevelTouchingWorks()
  1311. {
  1312. $before = Carbon::now();
  1313. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1314. $post = EloquentTouchingPost::create(['id' => 1, 'name' => 'Parent Post', 'user_id' => 1]);
  1315. $this->assertTrue($before->isSameDay($user->updated_at));
  1316. $this->assertTrue($before->isSameDay($post->updated_at));
  1317. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1318. EloquentTouchingComment::create(['content' => 'Comment content', 'post_id' => 1]);
  1319. $this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching models related timestamps.');
  1320. $this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.');
  1321. Carbon::setTestNow($before);
  1322. }
  1323. public function testDeletingChildModelTouchesParentTimestamps()
  1324. {
  1325. $before = Carbon::now();
  1326. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1327. $post = EloquentTouchingPost::create(['name' => 'Parent Post', 'user_id' => 1]);
  1328. $this->assertTrue($before->isSameDay($user->updated_at));
  1329. $this->assertTrue($before->isSameDay($post->updated_at));
  1330. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1331. $post->delete();
  1332. $this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.');
  1333. Carbon::setTestNow($before);
  1334. }
  1335. public function testTouchingChildModelUpdatesParentsTimestamps()
  1336. {
  1337. $before = Carbon::now();
  1338. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1339. $post = EloquentTouchingPost::create(['id' => 1, 'name' => 'Parent Post', 'user_id' => 1]);
  1340. $this->assertTrue($before->isSameDay($user->updated_at));
  1341. $this->assertTrue($before->isSameDay($post->updated_at));
  1342. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1343. $post->touch();
  1344. $this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching model own timestamps.');
  1345. $this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is not touching models related timestamps.');
  1346. Carbon::setTestNow($before);
  1347. }
  1348. public function testTouchingChildModelRespectsParentNoTouching()
  1349. {
  1350. $before = Carbon::now();
  1351. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1352. $post = EloquentTouchingPost::create(['id' => 1, 'name' => 'Parent Post', 'user_id' => 1]);
  1353. $this->assertTrue($before->isSameDay($user->updated_at));
  1354. $this->assertTrue($before->isSameDay($post->updated_at));
  1355. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1356. EloquentTouchingUser::withoutTouching(function () use ($post) {
  1357. $post->touch();
  1358. });
  1359. $this->assertTrue(
  1360. $future->isSameDay($post->fresh()->updated_at),
  1361. 'It is not touching model own timestamps in withoutTouching scope.'
  1362. );
  1363. $this->assertTrue(
  1364. $before->isSameDay($user->fresh()->updated_at),
  1365. 'It is touching model own timestamps in withoutTouching scope, when it should not.'
  1366. );
  1367. Carbon::setTestNow($before);
  1368. }
  1369. public function testUpdatingChildPostRespectsNoTouchingDefinition()
  1370. {
  1371. $before = Carbon::now();
  1372. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1373. $post = EloquentTouchingPost::create(['name' => 'Parent Post', 'user_id' => 1]);
  1374. $this->assertTrue($before->isSameDay($user->updated_at));
  1375. $this->assertTrue($before->isSameDay($post->updated_at));
  1376. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1377. EloquentTouchingUser::withoutTouching(function () use ($post) {
  1378. $post->update(['name' => 'Updated']);
  1379. });
  1380. $this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is not touching model own timestamps when it should.');
  1381. $this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models relationships when it should be disabled.');
  1382. Carbon::setTestNow($before);
  1383. }
  1384. public function testUpdatingModelInTheDisabledScopeTouchesItsOwnTimestamps()
  1385. {
  1386. $before = Carbon::now();
  1387. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1388. $post = EloquentTouchingPost::create(['name' => 'Parent Post', 'user_id' => 1]);
  1389. $this->assertTrue($before->isSameDay($user->updated_at));
  1390. $this->assertTrue($before->isSameDay($post->updated_at));
  1391. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1392. Model::withoutTouching(function () use ($post) {
  1393. $post->update(['name' => 'Updated']);
  1394. });
  1395. $this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1396. $this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1397. Carbon::setTestNow($before);
  1398. }
  1399. public function testDeletingChildModelRespectsTheNoTouchingRule()
  1400. {
  1401. $before = Carbon::now();
  1402. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1403. $post = EloquentTouchingPost::create(['name' => 'Parent Post', 'user_id' => 1]);
  1404. $this->assertTrue($before->isSameDay($user->updated_at));
  1405. $this->assertTrue($before->isSameDay($post->updated_at));
  1406. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1407. EloquentTouchingUser::withoutTouching(function () use ($post) {
  1408. $post->delete();
  1409. });
  1410. $this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1411. Carbon::setTestNow($before);
  1412. }
  1413. public function testRespectedMultiLevelTouchingChain()
  1414. {
  1415. $before = Carbon::now();
  1416. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1417. $post = EloquentTouchingPost::create(['id' => 1, 'name' => 'Parent Post', 'user_id' => 1]);
  1418. $this->assertTrue($before->isSameDay($user->updated_at));
  1419. $this->assertTrue($before->isSameDay($post->updated_at));
  1420. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1421. EloquentTouchingUser::withoutTouching(function () {
  1422. EloquentTouchingComment::create(['content' => 'Comment content', 'post_id' => 1]);
  1423. });
  1424. $this->assertTrue($future->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1425. $this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1426. Carbon::setTestNow($before);
  1427. }
  1428. public function testTouchesGreatParentEvenWhenParentIsInNoTouchScope()
  1429. {
  1430. $before = Carbon::now();
  1431. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1432. $post = EloquentTouchingPost::create(['id' => 1, 'name' => 'Parent Post', 'user_id' => 1]);
  1433. $this->assertTrue($before->isSameDay($user->updated_at));
  1434. $this->assertTrue($before->isSameDay($post->updated_at));
  1435. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1436. EloquentTouchingPost::withoutTouching(function () {
  1437. EloquentTouchingComment::create(['content' => 'Comment content', 'post_id' => 1]);
  1438. });
  1439. $this->assertTrue($before->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1440. $this->assertTrue($future->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1441. Carbon::setTestNow($before);
  1442. }
  1443. public function testCanNestCallsOfNoTouching()
  1444. {
  1445. $before = Carbon::now();
  1446. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1447. $post = EloquentTouchingPost::create(['id' => 1, 'name' => 'Parent Post', 'user_id' => 1]);
  1448. $this->assertTrue($before->isSameDay($user->updated_at));
  1449. $this->assertTrue($before->isSameDay($post->updated_at));
  1450. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1451. EloquentTouchingUser::withoutTouching(function () {
  1452. EloquentTouchingPost::withoutTouching(function () {
  1453. EloquentTouchingComment::create(['content' => 'Comment content', 'post_id' => 1]);
  1454. });
  1455. });
  1456. $this->assertTrue($before->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1457. $this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1458. Carbon::setTestNow($before);
  1459. }
  1460. public function testCanPassArrayOfModelsToIgnore()
  1461. {
  1462. $before = Carbon::now();
  1463. $user = EloquentTouchingUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1464. $post = EloquentTouchingPost::create(['id' => 1, 'name' => 'Parent Post', 'user_id' => 1]);
  1465. $this->assertTrue($before->isSameDay($user->updated_at));
  1466. $this->assertTrue($before->isSameDay($post->updated_at));
  1467. Carbon::setTestNow($future = $before->copy()->addDays(3));
  1468. Model::withoutTouchingOn([EloquentTouchingUser::class, EloquentTouchingPost::class], function () {
  1469. EloquentTouchingComment::create(['content' => 'Comment content', 'post_id' => 1]);
  1470. });
  1471. $this->assertTrue($before->isSameDay($post->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1472. $this->assertTrue($before->isSameDay($user->fresh()->updated_at), 'It is touching models when it should be disabled.');
  1473. Carbon::setTestNow($before);
  1474. }
  1475. public function testWhenBaseModelIsIgnoredAllChildModelsAreIgnored()
  1476. {
  1477. $this->assertFalse(Model::isIgnoringTouch());
  1478. $this->assertFalse(User::isIgnoringTouch());
  1479. Model::withoutTouching(function () {
  1480. $this->assertTrue(Model::isIgnoringTouch());
  1481. $this->assertTrue(User::isIgnoringTouch());
  1482. });
  1483. $this->assertFalse(User::isIgnoringTouch());
  1484. $this->assertFalse(Model::isIgnoringTouch());
  1485. }
  1486. public function testChildModelsAreIgnored()
  1487. {
  1488. $this->assertFalse(Model::isIgnoringTouch());
  1489. $this->assertFalse(User::isIgnoringTouch());
  1490. $this->assertFalse(Post::isIgnoringTouch());
  1491. User::withoutTouching(function () {
  1492. $this->assertFalse(Model::isIgnoringTouch());
  1493. $this->assertFalse(Post::isIgnoringTouch());
  1494. $this->assertTrue(User::isIgnoringTouch());
  1495. });
  1496. $this->assertFalse(Post::isIgnoringTouch());
  1497. $this->assertFalse(User::isIgnoringTouch());
  1498. $this->assertFalse(Model::isIgnoringTouch());
  1499. }
  1500. public function testPivotsCanBeRefreshed()
  1501. {
  1502. EloquentTestFriendLevel::create(['id' => 1, 'level' => 'acquaintance']);
  1503. EloquentTestFriendLevel::create(['id' => 2, 'level' => 'friend']);
  1504. $user = EloquentTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  1505. $user->friends()->create(['id' => 2, 'email' => 'abigailotwell@gmail.com'], ['friend_level_id' => 1]);
  1506. $pivot = $user->friends[0]->pivot;
  1507. // Simulate a change that happened externally
  1508. DB::table('friends')->where('user_id', 1)->where('friend_id', 2)->update([
  1509. 'friend_level_id' => 2,
  1510. ]);
  1511. $this->assertInstanceOf(Pivot::class, $freshPivot = $pivot->fresh());
  1512. $this->assertEquals(2, $freshPivot->friend_level_id);
  1513. $this->assertSame($pivot, $pivot->refresh());
  1514. $this->assertEquals(2, $pivot->friend_level_id);
  1515. }
  1516. public function testMorphPivotsCanBeRefreshed()
  1517. {
  1518. $post = EloquentTestPost::create(['name' => 'MorphToMany Post', 'user_id' => 1]);
  1519. $post->tags()->create(['id' => 1, 'name' => 'News']);
  1520. $pivot = $post->tags[0]->pivot;
  1521. // Simulate a change that happened externally
  1522. DB::table('taggables')
  1523. ->where([
  1524. 'taggable_type' => EloquentTestPost::class,
  1525. 'taggable_id' => 1,
  1526. 'tag_id' => 1,
  1527. ])
  1528. ->update([
  1529. 'taxonomy' => 'primary',
  1530. ]);
  1531. $this->assertInstanceOf(MorphPivot::class, $freshPivot = $pivot->fresh());
  1532. $this->assertSame('primary', $freshPivot->taxonomy);
  1533. $this->assertSame($pivot, $pivot->refresh());
  1534. $this->assertSame('primary', $pivot->taxonomy);
  1535. }
  1536. /**
  1537. * Helpers...
  1538. */
  1539. /**
  1540. * Get a database connection instance.
  1541. *
  1542. * @return \Illuminate\Database\Connection
  1543. */
  1544. protected function connection($connection = 'default')
  1545. {
  1546. return Eloquent::getConnectionResolver()->connection($connection);
  1547. }
  1548. /**
  1549. * Get a schema builder instance.
  1550. *
  1551. * @return \Illuminate\Database\Schema\Builder
  1552. */
  1553. protected function schema($connection = 'default')
  1554. {
  1555. return $this->connection($connection)->getSchemaBuilder();
  1556. }
  1557. }
  1558. /**
  1559. * Eloquent Models...
  1560. */
  1561. class EloquentTestUser extends Eloquent
  1562. {
  1563. protected $table = 'users';
  1564. protected $casts = ['birthday' => 'datetime'];
  1565. protected $guarded = [];
  1566. public function friends()
  1567. {
  1568. return $this->belongsToMany(self::class, 'friends', 'user_id', 'friend_id');
  1569. }
  1570. public function friendsOne()
  1571. {
  1572. return $this->belongsToMany(self::class, 'friends', 'user_id', 'friend_id')->wherePivot('user_id', 1);
  1573. }
  1574. public function friendsTwo()
  1575. {
  1576. return $this->belongsToMany(self::class, 'friends', 'user_id', 'friend_id')->wherePivot('user_id', 2);
  1577. }
  1578. public function posts()
  1579. {
  1580. return $this->hasMany(EloquentTestPost::class, 'user_id');
  1581. }
  1582. public function post()
  1583. {
  1584. return $this->hasOne(EloquentTestPost::class, 'user_id');
  1585. }
  1586. public function photos()
  1587. {
  1588. return $this->morphMany(EloquentTestPhoto::class, 'imageable');
  1589. }
  1590. public function postWithPhotos()
  1591. {
  1592. return $this->post()->join('photo', function ($join) {
  1593. $join->on('photo.imageable_id', 'post.id');
  1594. $join->where('photo.imageable_type', 'EloquentTestPost');
  1595. });
  1596. }
  1597. }
  1598. class EloquentTestUserWithCustomFriendPivot extends EloquentTestUser
  1599. {
  1600. public function friends()
  1601. {
  1602. return $this->belongsToMany(EloquentTestUser::class, 'friends', 'user_id', 'friend_id')
  1603. ->using(EloquentTestFriendPivot::class)->withPivot('user_id', 'friend_id', 'friend_level_id');
  1604. }
  1605. }
  1606. class EloquentTestUserWithSpaceInColumnName extends EloquentTestUser
  1607. {
  1608. protected $table = 'users_with_space_in_colum_name';
  1609. }
  1610. class EloquentTestNonIncrementing extends Eloquent
  1611. {
  1612. protected $table = 'non_incrementing_users';
  1613. protected $guarded = [];
  1614. public $incrementing = false;
  1615. public $timestamps = false;
  1616. }
  1617. class EloquentTestNonIncrementingSecond extends EloquentTestNonIncrementing
  1618. {
  1619. protected $connection = 'second_connection';
  1620. }
  1621. class EloquentTestUserWithGlobalScope extends EloquentTestUser
  1622. {
  1623. public static function boot()
  1624. {
  1625. parent::boot();
  1626. static::addGlobalScope(function ($builder) {
  1627. $builder->with('posts');
  1628. });
  1629. }
  1630. }
  1631. class EloquentTestUserWithOmittingGlobalScope extends EloquentTestUser
  1632. {
  1633. public static function boot()
  1634. {
  1635. parent::boot();
  1636. static::addGlobalScope(function ($builder) {
  1637. $builder->where('email', '!=', 'taylorotwell@gmail.com');
  1638. });
  1639. }
  1640. }
  1641. class EloquentTestUserWithGlobalScopeRemovingOtherScope extends Eloquent
  1642. {
  1643. use SoftDeletes;
  1644. protected $table = 'soft_deleted_users';
  1645. protected $guarded = [];
  1646. public static function boot()
  1647. {
  1648. static::addGlobalScope(function ($builder) {
  1649. $builder->withoutGlobalScope(SoftDeletingScope::class);
  1650. });
  1651. parent::boot();
  1652. }
  1653. }
  1654. class EloquentTestPost extends Eloquent
  1655. {
  1656. protected $table = 'posts';
  1657. protected $guarded = [];
  1658. public function user()
  1659. {
  1660. return $this->belongsTo(EloquentTestUser::class, 'user_id');
  1661. }
  1662. public function photos()
  1663. {
  1664. return $this->morphMany(EloquentTestPhoto::class, 'imageable');
  1665. }
  1666. public function childPosts()
  1667. {
  1668. return $this->hasMany(self::class, 'parent_id');
  1669. }
  1670. public function parentPost()
  1671. {
  1672. return $this->belongsTo(self::class, 'parent_id');
  1673. }
  1674. public function tags()
  1675. {
  1676. return $this->morphToMany(EloquentTestTag::class, 'taggable', null, null, 'tag_id')->withPivot('taxonomy');
  1677. }
  1678. }
  1679. class EloquentTestTag extends Eloquent
  1680. {
  1681. protected $table = 'tags';
  1682. protected $guarded = [];
  1683. }
  1684. class EloquentTestFriendLevel extends Eloquent
  1685. {
  1686. protected $table = 'friend_levels';
  1687. protected $guarded = [];
  1688. }
  1689. class EloquentTestPhoto extends Eloquent
  1690. {
  1691. protected $table = 'photos';
  1692. protected $guarded = [];
  1693. public function imageable()
  1694. {
  1695. return $this->morphTo();
  1696. }
  1697. }
  1698. class EloquentTestUserWithStringCastId extends EloquentTestUser
  1699. {
  1700. protected $casts = [
  1701. 'id' => 'string',
  1702. ];
  1703. }
  1704. class EloquentTestUserWithCustomDateSerialization extends EloquentTestUser
  1705. {
  1706. protected function serializeDate(DateTimeInterface $date)
  1707. {
  1708. return $date->format('d-m-y');
  1709. }
  1710. }
  1711. class EloquentTestOrder extends Eloquent
  1712. {
  1713. protected $guarded = [];
  1714. protected $table = 'test_orders';
  1715. protected $with = ['item'];
  1716. public function item()
  1717. {
  1718. return $this->morphTo();
  1719. }
  1720. }
  1721. class EloquentTestItem extends Eloquent
  1722. {
  1723. protected $guarded = [];
  1724. protected $table = 'test_items';
  1725. protected $connection = 'second_connection';
  1726. }
  1727. class EloquentTestWithJSON extends Eloquent
  1728. {
  1729. protected $guarded = [];
  1730. protected $table = 'with_json';
  1731. public $timestamps = false;
  1732. protected $casts = [
  1733. 'json' => 'array',
  1734. ];
  1735. }
  1736. class EloquentTestFriendPivot extends Pivot
  1737. {
  1738. protected $table = 'friends';
  1739. protected $guarded = [];
  1740. public function user()
  1741. {
  1742. return $this->belongsTo(EloquentTestUser::class);
  1743. }
  1744. public function friend()
  1745. {
  1746. return $this->belongsTo(EloquentTestUser::class);
  1747. }
  1748. public function level()
  1749. {
  1750. return $this->belongsTo(EloquentTestFriendLevel::class, 'friend_level_id');
  1751. }
  1752. }
  1753. class EloquentTouchingUser extends Eloquent
  1754. {
  1755. protected $table = 'users';
  1756. protected $guarded = [];
  1757. }
  1758. class EloquentTouchingPost extends Eloquent
  1759. {
  1760. protected $table = 'posts';
  1761. protected $guarded = [];
  1762. protected $touches = [
  1763. 'user',
  1764. ];
  1765. public function user()
  1766. {
  1767. return $this->belongsTo(EloquentTouchingUser::class, 'user_id');
  1768. }
  1769. }
  1770. class EloquentTouchingComment extends Eloquent
  1771. {
  1772. protected $table = 'comments';
  1773. protected $guarded = [];
  1774. protected $touches = [
  1775. 'post',
  1776. ];
  1777. public function post()
  1778. {
  1779. return $this->belongsTo(EloquentTouchingPost::class, 'post_id');
  1780. }
  1781. }