Laravel Seeders: Planting the Seeds of Your Database The seeder is the last piece of the Factory/Faker/Seeder puzzle. We’ve covered the Factory and used Faker with it. Now it’s time to automate this whole process and use seeders. Laravel — P32: Factory with Faker The seeder will populate your database tables when you run the following command: php artisan db:seed That one command can call one or more seeders. You can also call an individual seeder: php artisan db:seed — class=UserSeeder Let’s create a seeder and see how this all ties in. Run the following command to create a User
Category: Web Development
The Ultimate Laravel Pair: Factories and Faker for Dynamic Data For the last two articles, we went through an introduction of the Faker library and the Factory concept in Laravel. We worked on the User model, which was already created for us, but we haven’t looked at actually creating our own Factory. Laravel — P31: Faker To create a Factory, we first need to create a model. We’ll create a new model and factory at the same time. # php artisan make:model Car -mf INFO Model [app/Models/Car.php] created successfully. INFO Factory [database/factories/CarFactory.php] created successfully. INFO Migration [database/migrations/2023_01_16_162646_create_cars_table.php] created successfully. This command will generate a model Car with
Enhancing Laravel Data with Faker: Realistic Testing Made Easy What’s a faker? It’s exactly as it sounds. You’re trying to fake something. Instead of coming up with the data yourself, you simply state, “Faker, give me a unique random email address,” and faker spits it out for you. In Laravel, Faker is a library that generates fake data for testing purposes. It can be used to generate random names, addresses, phone numbers, email addresses, and other types of fake data that can be used to populate databases and test applications. Laravel — P30: Factories You can start using the faker library automatically. The Faker
Efficient Testing in Laravel: The Role of Factories If you’re just starting out with Laravel, some of the first few questions that you’ll come across are: What is the difference between Seeders, Fakers, and Factories? Should I use them? Do I need to use all of them or can I use some of them? How do I create them? Laravel — P29: Models Intro Factories According to Laravel’s official documentation, “factories are classes that extend Laravel’s base factory class and define a model property and definition method.” Just think about it this way. If you’ve ever created a model, like
Laravel Models Unveiled: Your Gateway to Data Interaction After spending an entire article learning about migrations, it’s only natural that the next one is going to be focused on Models. Laravel — P28: Migrations What is a Model? A Model is just a class that ties into a table within the database. By convention, tables are plural and Models are singular versions of the table name. For example, the tests table should have a Test model associated with it. The Test model knows how to communicate with the tests table and can add, update, get, or delete data from the table. How does the Model know to interact
Evolving Your Laravel Projects: Mastering Migrations Migrations are a way for everyone on the team to easily share database schemas. Once you create a migration, everyone on the team will be able to run it and generate a new table. There is no more messaging others and telling them that they need to add a new column or change the column type. Upload the migrations and if something breaks, the team can run the migrations again to update the tables. How Do You Create a Migration? Generating a migration is just another artisan command. I told you that we would be using artisan a
Laravel Database Mastery: Starting Your Data Journey Why database and not a model intro you may ask. A model is just class that communicates with a specific table in the database. I’m a true believer that you should understand a topic at a high level before you jump into creating code for it. Let’s take a moment to discuss the database and how Laravel interacts with it. What is a Database? It’s not going to be that type of basic article, but just so that we’re all on the same page, a database is a place where you store data.
Simplifying Laravel: The Power of Single Action Controllers Controllers are there to make our lives simpler, not to make them more complex. However, even when we separate the logic out of our route files, sometimes the controllers themselves can also become cluttered. That’s one use case for a Single Action Controller. Laravel — P25: Passing URL Arguments to Controllers The Single Action Controller will contain a specifically named method: __invoke(). Naming the method __invoke() will eliminate the need to call it from our route. Instead, we can just call the Controller itself. Creating the Single Action Controller Creating the Single Action Controller is
Dynamic Data Handling: URL Arguments in Laravel Controllers Now that we’re familiar with creating controllers, we need to be able to pass arguments to it. This comes in form of URL parameters. Sometimes we need to pass an argument like a specific id. Laravel — P24: Controllers Intro If you haven’t created the TestController, it’s time to do so now. I’m going to be using the one from the previous article, but the artisan command to create it is: php artisan make:controller TestController. Creating the Route Our first task is to create the route. The URL will contain a parameter that will be passed
Navigating Laravel: An Introduction to Controllers For the past 23 articles, I’ve been screaming on the inside, since we haven’t been doing things the right way. Can all logic fall into your route? Sure it can. Should it? Absolutely not. This was my way easing you into Laravel. If you’ve ever read anything else on it, what’s usually the first example? “Alright guys, let’s create a to-do list.” That’s great, but it uses way too many concepts at the same time without going into enough detail about what everything else does. Enough of that, it’s time for controllers. Laravel —
