Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. # Laravel Cheatsheet
  2.  
  3. ## Artisan
  4.  
  5. ### Making a model with a controller, migration, as a resource
  6.  
  7. `php artisan make:model Name -mcr`
  8.  
  9. ## Factories
  10.  
  11. ### How do I use faker with enums?
  12.  
  13. ```
  14. 'something' => $faker->randomElement(['foo' ,'bar', 'baz'])
  15. ```
  16.  
  17. ### How do I conditionally set values with faker?
  18.  
  19. ```
  20. $factory->define(Model::class, function (Faker $faker) {
  21. /**
  22. * Set up the type of organisation.
  23. */
  24. $type = $faker->randomElement(['organisation', 'charity']);
  25.  
  26. /**
  27. * Set the charity number to null by default.
  28. */
  29. $charityNumber = null;
  30.  
  31. /**
  32. * Check the type of organisation, and if charity, create dummy number.
  33. */
  34. if($type == 'charity') {
  35. $charityNumber = $faker->numberBetween(100000,999999) . '-0';
  36. }
  37.  
  38. return [
  39. 'name' => $faker->company,
  40. 'type' => $type,
  41. 'charityNumber' => $charityNumber
  42. ];
  43. });
  44. ```
  45.  
  46. ### How do I ensure data generated by faker is unique?
  47.  
  48. You can ensure that any data randomly generated by faker is unique by using the `unique()` method, like so:
  49.  
  50. ```
  51. return [
  52. 'email' => $faker->unique()->email,
  53. ]
  54. ```
  55.  
  56. ## Testing
  57.  
  58. When using Laravel 5.8 and above, and you want to use the setUp method, you need to return it as : void. E.g.
  59.  
  60. ```
  61. class ProductTest extends TestCase
  62. {
  63. protected $product;
  64.  
  65. public function setUp() : void
  66. {
  67. $this->product = new Product();
  68. $this->product->manufacturer = 'Apple';
  69. $this->product->name = 'iPhone 11 Pro';
  70. $this->product->price = '4999';
  71. }
  72.  
  73. public function testProductHasName()
  74. {
  75. $this->assertEquals('iPhone 11 Pro', $this->product->name());
  76. }
  77. }
  78. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement