Book Image

Laravel 5.x Cookbook

By : Terry Matula, Alfred Nutile
Book Image

Laravel 5.x Cookbook

By: Terry Matula, Alfred Nutile

Overview of this book

Laravel is a prominent member of a new generation of web frameworks. It is one of the most popular PHP frameworks and is also free and an open source. Laravel 5 is a substantial upgrade with a lot of new toys, at the same time retaining the features that made Laravel wildly successful. It comes with plenty of architectural as well as design-based changes. The book is a blend of numerous recipes that will give you all the necessary tips you need to build an application. It starts with basic installation and configuration tasks and will get you up-and-running in no time. You will learn to create and customize your PHP app and tweak and re-design your existing apps for better performance. You will learn to implement practical recipes to utilize Laravel’s modular structure, the latest method injection, route caching, and interfacing techniques to create responsive modern-day PHP apps that stand on their own against other apps. Efficient testing and deploying techniques will make you more confident with your Laravel skills as you move ahead with this book. Towards the end of the book, you will understand a number of add-ons and new features essential to finalize your application to make it ready for subscriptions. You will be empowered to get your application out to the world.
Table of Contents (17 chapters)
Laravel 5.x Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Using factories for migrations and tests


Factories are a newer feature in Laravel 5.1. It makes it very easy to populate models with Faker data: https://github.com/fzaninotto/Faker. I will cover using factories in tests. In the How it works… section of the same recipe, I will explain more about the process, including why we use it.

Getting ready

A fresh install of Laravel, Homestead setup for migrations is needed. Then follow the prior recipe for altering a migration:

How to do it…

Follow these steps to use factories:

  1. First, we will show this in a test so let's type:

    >php artisan make:test ExampleFactoryTest
    
  2. Then, open that file and we will create a user-related factory:

  3. Then we can run our test to watch if it fails. I will go into more details in How it works… section of the same recipe:

  4. Now I will open the factory file database/factories/ModelFactory.php and add that field:

  5. Then run the test again:

    > vendor/bin/phpunit --filter=should_have_twitter_name
    
  6. Then I will get a different error....