DatabaseEloquentFactoryTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Faker\Generator;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Contracts\Foundation\Application;
  6. use Illuminate\Database\Capsule\Manager as DB;
  7. use Illuminate\Database\Eloquent\Collection;
  8. use Illuminate\Database\Eloquent\Factories\CrossJoinSequence;
  9. use Illuminate\Database\Eloquent\Factories\Factory;
  10. use Illuminate\Database\Eloquent\Factories\HasFactory;
  11. use Illuminate\Database\Eloquent\Factories\Sequence;
  12. use Illuminate\Database\Eloquent\Model as Eloquent;
  13. use Illuminate\Tests\Database\Fixtures\Models\Money\Price;
  14. use Mockery;
  15. use PHPUnit\Framework\TestCase;
  16. class DatabaseEloquentFactoryTest extends TestCase
  17. {
  18. protected function setUp(): void
  19. {
  20. $container = Container::getInstance();
  21. $container->singleton(Generator::class, function ($app, $parameters) {
  22. return \Faker\Factory::create('en_US');
  23. });
  24. $container->instance(Application::class, $app = Mockery::mock(Application::class));
  25. $app->shouldReceive('getNamespace')->andReturn('App\\');
  26. $db = new DB;
  27. $db->addConnection([
  28. 'driver' => 'sqlite',
  29. 'database' => ':memory:',
  30. ]);
  31. $db->bootEloquent();
  32. $db->setAsGlobal();
  33. $this->createSchema();
  34. }
  35. /**
  36. * Setup the database schema.
  37. *
  38. * @return void
  39. */
  40. public function createSchema()
  41. {
  42. $this->schema()->create('users', function ($table) {
  43. $table->increments('id');
  44. $table->string('name');
  45. $table->string('options')->nullable();
  46. $table->timestamps();
  47. });
  48. $this->schema()->create('posts', function ($table) {
  49. $table->increments('id');
  50. $table->foreignId('user_id');
  51. $table->string('title');
  52. $table->timestamps();
  53. });
  54. $this->schema()->create('comments', function ($table) {
  55. $table->increments('id');
  56. $table->foreignId('commentable_id');
  57. $table->string('commentable_type');
  58. $table->string('body');
  59. $table->timestamps();
  60. });
  61. $this->schema()->create('roles', function ($table) {
  62. $table->increments('id');
  63. $table->string('name');
  64. $table->timestamps();
  65. });
  66. $this->schema()->create('role_user', function ($table) {
  67. $table->foreignId('role_id');
  68. $table->foreignId('user_id');
  69. $table->string('admin')->default('N');
  70. });
  71. }
  72. /**
  73. * Tear down the database schema.
  74. *
  75. * @return void
  76. */
  77. protected function tearDown(): void
  78. {
  79. Mockery::close();
  80. $this->schema()->drop('users');
  81. Container::setInstance(null);
  82. }
  83. public function test_basic_model_can_be_created()
  84. {
  85. $user = FactoryTestUserFactory::new()->create();
  86. $this->assertInstanceOf(Eloquent::class, $user);
  87. $user = FactoryTestUserFactory::new()->createOne();
  88. $this->assertInstanceOf(Eloquent::class, $user);
  89. $user = FactoryTestUserFactory::new()->create(['name' => 'Taylor Otwell']);
  90. $this->assertInstanceOf(Eloquent::class, $user);
  91. $this->assertSame('Taylor Otwell', $user->name);
  92. $users = FactoryTestUserFactory::new()->createMany([
  93. ['name' => 'Taylor Otwell'],
  94. ['name' => 'Jeffrey Way'],
  95. ]);
  96. $this->assertInstanceOf(Collection::class, $users);
  97. $this->assertCount(2, $users);
  98. $users = FactoryTestUserFactory::times(10)->create();
  99. $this->assertCount(10, $users);
  100. }
  101. public function test_expanded_closure_attributes_are_resolved_and_passed_to_closures()
  102. {
  103. $user = FactoryTestUserFactory::new()->create([
  104. 'name' => function () {
  105. return 'taylor';
  106. },
  107. 'options' => function ($attributes) {
  108. return $attributes['name'].'-options';
  109. },
  110. ]);
  111. $this->assertSame('taylor-options', $user->options);
  112. }
  113. public function test_make_creates_unpersisted_model_instance()
  114. {
  115. $user = FactoryTestUserFactory::new()->makeOne();
  116. $this->assertInstanceOf(Eloquent::class, $user);
  117. $user = FactoryTestUserFactory::new()->make(['name' => 'Taylor Otwell']);
  118. $this->assertInstanceOf(Eloquent::class, $user);
  119. $this->assertSame('Taylor Otwell', $user->name);
  120. $this->assertCount(0, FactoryTestUser::all());
  121. }
  122. public function test_basic_model_attributes_can_be_created()
  123. {
  124. $user = FactoryTestUserFactory::new()->raw();
  125. $this->assertIsArray($user);
  126. $user = FactoryTestUserFactory::new()->raw(['name' => 'Taylor Otwell']);
  127. $this->assertIsArray($user);
  128. $this->assertSame('Taylor Otwell', $user['name']);
  129. }
  130. public function test_expanded_model_attributes_can_be_created()
  131. {
  132. $post = FactoryTestPostFactory::new()->raw();
  133. $this->assertIsArray($post);
  134. $post = FactoryTestPostFactory::new()->raw(['title' => 'Test Title']);
  135. $this->assertIsArray($post);
  136. $this->assertIsInt($post['user_id']);
  137. $this->assertSame('Test Title', $post['title']);
  138. }
  139. public function test_lazy_model_attributes_can_be_created()
  140. {
  141. $userFunction = FactoryTestUserFactory::new()->lazy();
  142. $this->assertIsCallable($userFunction);
  143. $this->assertInstanceOf(Eloquent::class, $userFunction());
  144. $userFunction = FactoryTestUserFactory::new()->lazy(['name' => 'Taylor Otwell']);
  145. $this->assertIsCallable($userFunction);
  146. $user = $userFunction();
  147. $this->assertInstanceOf(Eloquent::class, $user);
  148. $this->assertSame('Taylor Otwell', $user->name);
  149. }
  150. public function test_multiple_model_attributes_can_be_created()
  151. {
  152. $posts = FactoryTestPostFactory::new()->times(10)->raw();
  153. $this->assertIsArray($posts);
  154. $this->assertCount(10, $posts);
  155. }
  156. public function test_after_creating_and_making_callbacks_are_called()
  157. {
  158. $user = FactoryTestUserFactory::new()
  159. ->afterMaking(function ($user) {
  160. $_SERVER['__test.user.making'] = $user;
  161. })
  162. ->afterCreating(function ($user) {
  163. $_SERVER['__test.user.creating'] = $user;
  164. })
  165. ->create();
  166. $this->assertSame($user, $_SERVER['__test.user.making']);
  167. $this->assertSame($user, $_SERVER['__test.user.creating']);
  168. unset($_SERVER['__test.user.making'], $_SERVER['__test.user.creating']);
  169. }
  170. public function test_has_many_relationship()
  171. {
  172. $users = FactoryTestUserFactory::times(10)
  173. ->has(
  174. FactoryTestPostFactory::times(3)
  175. ->state(function ($attributes, $user) {
  176. // Test parent is passed to child state mutations...
  177. $_SERVER['__test.post.state-user'] = $user;
  178. return [];
  179. })
  180. // Test parents passed to callback...
  181. ->afterCreating(function ($post, $user) {
  182. $_SERVER['__test.post.creating-post'] = $post;
  183. $_SERVER['__test.post.creating-user'] = $user;
  184. }),
  185. 'posts'
  186. )
  187. ->create();
  188. $this->assertCount(10, FactoryTestUser::all());
  189. $this->assertCount(30, FactoryTestPost::all());
  190. $this->assertCount(3, FactoryTestUser::latest()->first()->posts);
  191. $this->assertInstanceOf(Eloquent::class, $_SERVER['__test.post.creating-post']);
  192. $this->assertInstanceOf(Eloquent::class, $_SERVER['__test.post.creating-user']);
  193. $this->assertInstanceOf(Eloquent::class, $_SERVER['__test.post.state-user']);
  194. unset($_SERVER['__test.post.creating-post'], $_SERVER['__test.post.creating-user'], $_SERVER['__test.post.state-user']);
  195. }
  196. public function test_belongs_to_relationship()
  197. {
  198. $posts = FactoryTestPostFactory::times(3)
  199. ->for(FactoryTestUserFactory::new(['name' => 'Taylor Otwell']), 'user')
  200. ->create();
  201. $this->assertCount(3, $posts->filter(function ($post) {
  202. return $post->user->name === 'Taylor Otwell';
  203. }));
  204. $this->assertCount(1, FactoryTestUser::all());
  205. $this->assertCount(3, FactoryTestPost::all());
  206. }
  207. public function test_belongs_to_relationship_with_existing_model_instance()
  208. {
  209. $user = FactoryTestUserFactory::new(['name' => 'Taylor Otwell'])->create();
  210. $posts = FactoryTestPostFactory::times(3)
  211. ->for($user, 'user')
  212. ->create();
  213. $this->assertCount(3, $posts->filter(function ($post) use ($user) {
  214. return $post->user->is($user);
  215. }));
  216. $this->assertCount(1, FactoryTestUser::all());
  217. $this->assertCount(3, FactoryTestPost::all());
  218. }
  219. public function test_belongs_to_relationship_with_existing_model_instance_with_relationship_name_implied_from_model()
  220. {
  221. $user = FactoryTestUserFactory::new(['name' => 'Taylor Otwell'])->create();
  222. $posts = FactoryTestPostFactory::times(3)
  223. ->for($user)
  224. ->create();
  225. $this->assertCount(3, $posts->filter(function ($post) use ($user) {
  226. return $post->factoryTestUser->is($user);
  227. }));
  228. $this->assertCount(1, FactoryTestUser::all());
  229. $this->assertCount(3, FactoryTestPost::all());
  230. }
  231. public function test_morph_to_relationship()
  232. {
  233. $posts = FactoryTestCommentFactory::times(3)
  234. ->for(FactoryTestPostFactory::new(['title' => 'Test Title']), 'commentable')
  235. ->create();
  236. $this->assertSame('Test Title', FactoryTestPost::first()->title);
  237. $this->assertCount(3, FactoryTestPost::first()->comments);
  238. $this->assertCount(1, FactoryTestPost::all());
  239. $this->assertCount(3, FactoryTestComment::all());
  240. }
  241. public function test_morph_to_relationship_with_existing_model_instance()
  242. {
  243. $post = FactoryTestPostFactory::new(['title' => 'Test Title'])->create();
  244. $posts = FactoryTestCommentFactory::times(3)
  245. ->for($post, 'commentable')
  246. ->create();
  247. $this->assertSame('Test Title', FactoryTestPost::first()->title);
  248. $this->assertCount(3, FactoryTestPost::first()->comments);
  249. $this->assertCount(1, FactoryTestPost::all());
  250. $this->assertCount(3, FactoryTestComment::all());
  251. }
  252. public function test_belongs_to_many_relationship()
  253. {
  254. $users = FactoryTestUserFactory::times(3)
  255. ->hasAttached(
  256. FactoryTestRoleFactory::times(3)->afterCreating(function ($role, $user) {
  257. $_SERVER['__test.role.creating-role'] = $role;
  258. $_SERVER['__test.role.creating-user'] = $user;
  259. }),
  260. ['admin' => 'Y'],
  261. 'roles'
  262. )
  263. ->create();
  264. $this->assertCount(9, FactoryTestRole::all());
  265. $user = FactoryTestUser::latest()->first();
  266. $this->assertCount(3, $user->roles);
  267. $this->assertSame('Y', $user->roles->first()->pivot->admin);
  268. $this->assertInstanceOf(Eloquent::class, $_SERVER['__test.role.creating-role']);
  269. $this->assertInstanceOf(Eloquent::class, $_SERVER['__test.role.creating-user']);
  270. unset($_SERVER['__test.role.creating-role'], $_SERVER['__test.role.creating-user']);
  271. }
  272. public function test_belongs_to_many_relationship_with_existing_model_instances()
  273. {
  274. $roles = FactoryTestRoleFactory::times(3)
  275. ->afterCreating(function ($role) {
  276. $_SERVER['__test.role.creating-role'] = $role;
  277. })
  278. ->create();
  279. FactoryTestUserFactory::times(3)
  280. ->hasAttached($roles, ['admin' => 'Y'], 'roles')
  281. ->create();
  282. $this->assertCount(3, FactoryTestRole::all());
  283. $user = FactoryTestUser::latest()->first();
  284. $this->assertCount(3, $user->roles);
  285. $this->assertSame('Y', $user->roles->first()->pivot->admin);
  286. $this->assertInstanceOf(Eloquent::class, $_SERVER['__test.role.creating-role']);
  287. unset($_SERVER['__test.role.creating-role']);
  288. }
  289. public function test_belongs_to_many_relationship_with_existing_model_instances_with_relationship_name_implied_from_model()
  290. {
  291. $roles = FactoryTestRoleFactory::times(3)
  292. ->afterCreating(function ($role) {
  293. $_SERVER['__test.role.creating-role'] = $role;
  294. })
  295. ->create();
  296. FactoryTestUserFactory::times(3)
  297. ->hasAttached($roles, ['admin' => 'Y'])
  298. ->create();
  299. $this->assertCount(3, FactoryTestRole::all());
  300. $user = FactoryTestUser::latest()->first();
  301. $this->assertCount(3, $user->factoryTestRoles);
  302. $this->assertSame('Y', $user->factoryTestRoles->first()->pivot->admin);
  303. $this->assertInstanceOf(Eloquent::class, $_SERVER['__test.role.creating-role']);
  304. unset($_SERVER['__test.role.creating-role']);
  305. }
  306. public function test_sequences()
  307. {
  308. $users = FactoryTestUserFactory::times(2)->sequence(
  309. ['name' => 'Taylor Otwell'],
  310. ['name' => 'Abigail Otwell'],
  311. )->create();
  312. $this->assertSame('Taylor Otwell', $users[0]->name);
  313. $this->assertSame('Abigail Otwell', $users[1]->name);
  314. $user = FactoryTestUserFactory::new()
  315. ->hasAttached(
  316. FactoryTestRoleFactory::times(4),
  317. new Sequence(['admin' => 'Y'], ['admin' => 'N']),
  318. 'roles'
  319. )
  320. ->create();
  321. $this->assertCount(4, $user->roles);
  322. $this->assertCount(2, $user->roles->filter(function ($role) {
  323. return $role->pivot->admin === 'Y';
  324. }));
  325. $this->assertCount(2, $user->roles->filter(function ($role) {
  326. return $role->pivot->admin === 'N';
  327. }));
  328. $users = FactoryTestUserFactory::times(2)->sequence(function ($sequence) {
  329. return ['name' => 'index: '.$sequence->index];
  330. })->create();
  331. $this->assertSame('index: 0', $users[0]->name);
  332. $this->assertSame('index: 1', $users[1]->name);
  333. }
  334. public function test_cross_join_sequences()
  335. {
  336. $assert = function ($users) {
  337. $assertions = [
  338. ['first_name' => 'Thomas', 'last_name' => 'Anderson'],
  339. ['first_name' => 'Thomas', 'last_name' => 'Smith'],
  340. ['first_name' => 'Agent', 'last_name' => 'Anderson'],
  341. ['first_name' => 'Agent', 'last_name' => 'Smith'],
  342. ];
  343. foreach ($assertions as $key => $assertion) {
  344. $this->assertSame(
  345. $assertion,
  346. $users[$key]->only('first_name', 'last_name'),
  347. );
  348. }
  349. };
  350. $usersByClass = FactoryTestUserFactory::times(4)
  351. ->state(
  352. new CrossJoinSequence(
  353. [['first_name' => 'Thomas'], ['first_name' => 'Agent']],
  354. [['last_name' => 'Anderson'], ['last_name' => 'Smith']],
  355. ),
  356. )
  357. ->make();
  358. $assert($usersByClass);
  359. $usersByMethod = FactoryTestUserFactory::times(4)
  360. ->crossJoinSequence(
  361. [['first_name' => 'Thomas'], ['first_name' => 'Agent']],
  362. [['last_name' => 'Anderson'], ['last_name' => 'Smith']],
  363. )
  364. ->make();
  365. $assert($usersByMethod);
  366. }
  367. public function test_resolve_nested_model_factories()
  368. {
  369. Factory::useNamespace('Factories\\');
  370. $resolves = [
  371. 'App\\Foo' => 'Factories\\FooFactory',
  372. 'App\\Models\\Foo' => 'Factories\\FooFactory',
  373. 'App\\Models\\Nested\\Foo' => 'Factories\\Nested\\FooFactory',
  374. 'App\\Models\\Really\\Nested\\Foo' => 'Factories\\Really\\Nested\\FooFactory',
  375. ];
  376. foreach ($resolves as $model => $factory) {
  377. $this->assertEquals($factory, Factory::resolveFactoryName($model));
  378. }
  379. }
  380. public function test_resolve_nested_model_name_from_factory()
  381. {
  382. Container::getInstance()->instance(Application::class, $app = Mockery::mock(Application::class));
  383. $app->shouldReceive('getNamespace')->andReturn('Illuminate\\Tests\\Database\\Fixtures\\');
  384. Factory::useNamespace('Illuminate\\Tests\\Database\\Fixtures\\Factories\\');
  385. $factory = Price::factory();
  386. $this->assertSame(Price::class, $factory->modelName());
  387. }
  388. public function test_resolve_non_app_nested_model_factories()
  389. {
  390. Container::getInstance()->instance(Application::class, $app = Mockery::mock(Application::class));
  391. $app->shouldReceive('getNamespace')->andReturn('Foo\\');
  392. Factory::useNamespace('Factories\\');
  393. $resolves = [
  394. 'Foo\\Bar' => 'Factories\\BarFactory',
  395. 'Foo\\Models\\Bar' => 'Factories\\BarFactory',
  396. 'Foo\\Models\\Nested\\Bar' => 'Factories\\Nested\\BarFactory',
  397. 'Foo\\Models\\Really\\Nested\\Bar' => 'Factories\\Really\\Nested\\BarFactory',
  398. ];
  399. foreach ($resolves as $model => $factory) {
  400. $this->assertEquals($factory, Factory::resolveFactoryName($model));
  401. }
  402. }
  403. public function test_model_has_factory()
  404. {
  405. Factory::guessFactoryNamesUsing(function ($model) {
  406. return $model.'Factory';
  407. });
  408. $this->assertInstanceOf(FactoryTestUserFactory::class, FactoryTestUser::factory());
  409. }
  410. public function test_dynamic_has_and_for_methods()
  411. {
  412. Factory::guessFactoryNamesUsing(function ($model) {
  413. return $model.'Factory';
  414. });
  415. $user = FactoryTestUserFactory::new()->hasPosts(3)->create();
  416. $this->assertCount(3, $user->posts);
  417. $post = FactoryTestPostFactory::new()
  418. ->forAuthor(['name' => 'Taylor Otwell'])
  419. ->hasComments(2)
  420. ->create();
  421. $this->assertInstanceOf(FactoryTestUser::class, $post->author);
  422. $this->assertSame('Taylor Otwell', $post->author->name);
  423. $this->assertCount(2, $post->comments);
  424. }
  425. public function test_can_be_macroable()
  426. {
  427. $factory = FactoryTestUserFactory::new();
  428. $factory->macro('getFoo', function () {
  429. return 'Hello World';
  430. });
  431. $this->assertSame('Hello World', $factory->getFoo());
  432. }
  433. public function test_factory_can_conditionally_execute_code()
  434. {
  435. FactoryTestUserFactory::new()
  436. ->when(true, function () {
  437. $this->assertTrue(true);
  438. })
  439. ->when(false, function () {
  440. $this->fail('Unreachable code that has somehow been reached.');
  441. })
  442. ->unless(false, function () {
  443. $this->assertTrue(true);
  444. })
  445. ->unless(true, function () {
  446. $this->fail('Unreachable code that has somehow been reached.');
  447. });
  448. }
  449. /**
  450. * Get a database connection instance.
  451. *
  452. * @return \Illuminate\Database\ConnectionInterface
  453. */
  454. protected function connection()
  455. {
  456. return Eloquent::getConnectionResolver()->connection();
  457. }
  458. /**
  459. * Get a schema builder instance.
  460. *
  461. * @return \Illuminate\Database\Schema\Builder
  462. */
  463. protected function schema()
  464. {
  465. return $this->connection()->getSchemaBuilder();
  466. }
  467. }
  468. class FactoryTestUserFactory extends Factory
  469. {
  470. protected $model = FactoryTestUser::class;
  471. public function definition()
  472. {
  473. return [
  474. 'name' => $this->faker->name,
  475. 'options' => null,
  476. ];
  477. }
  478. }
  479. class FactoryTestUser extends Eloquent
  480. {
  481. use HasFactory;
  482. protected $table = 'users';
  483. public function posts()
  484. {
  485. return $this->hasMany(FactoryTestPost::class, 'user_id');
  486. }
  487. public function roles()
  488. {
  489. return $this->belongsToMany(FactoryTestRole::class, 'role_user', 'user_id', 'role_id')->withPivot('admin');
  490. }
  491. public function factoryTestRoles()
  492. {
  493. return $this->belongsToMany(FactoryTestRole::class, 'role_user', 'user_id', 'role_id')->withPivot('admin');
  494. }
  495. }
  496. class FactoryTestPostFactory extends Factory
  497. {
  498. protected $model = FactoryTestPost::class;
  499. public function definition()
  500. {
  501. return [
  502. 'user_id' => FactoryTestUserFactory::new(),
  503. 'title' => $this->faker->name,
  504. ];
  505. }
  506. }
  507. class FactoryTestPost extends Eloquent
  508. {
  509. protected $table = 'posts';
  510. public function user()
  511. {
  512. return $this->belongsTo(FactoryTestUser::class, 'user_id');
  513. }
  514. public function factoryTestUser()
  515. {
  516. return $this->belongsTo(FactoryTestUser::class, 'user_id');
  517. }
  518. public function author()
  519. {
  520. return $this->belongsTo(FactoryTestUser::class, 'user_id');
  521. }
  522. public function comments()
  523. {
  524. return $this->morphMany(FactoryTestComment::class, 'commentable');
  525. }
  526. }
  527. class FactoryTestCommentFactory extends Factory
  528. {
  529. protected $model = FactoryTestComment::class;
  530. public function definition()
  531. {
  532. return [
  533. 'commentable_id' => FactoryTestPostFactory::new(),
  534. 'commentable_type' => FactoryTestPost::class,
  535. 'body' => $this->faker->name,
  536. ];
  537. }
  538. }
  539. class FactoryTestComment extends Eloquent
  540. {
  541. protected $table = 'comments';
  542. public function commentable()
  543. {
  544. return $this->morphTo();
  545. }
  546. }
  547. class FactoryTestRoleFactory extends Factory
  548. {
  549. protected $model = FactoryTestRole::class;
  550. public function definition()
  551. {
  552. return [
  553. 'name' => $this->faker->name,
  554. ];
  555. }
  556. }
  557. class FactoryTestRole extends Eloquent
  558. {
  559. protected $table = 'roles';
  560. public function users()
  561. {
  562. return $this->belongsToMany(FactoryTestUser::class, 'role_user', 'role_id', 'user_id')->withPivot('admin');
  563. }
  564. }