Controller Essentials: Understanding Laravel’s Create/Store Methods How do you insert a new record into the database? You can use tinker like we’ve been doing so far, but realistically we need to use a form. The user should see a blank form first and then click on the Submit button. The data should be sent over to another method in the controller that will help validate the data and send it to our PersonalCar model for insertion into the database. https://medium.com/geekculture/laravel-p41-controller-show-method-cmp-d503973018e7 Create The first thing that you need to see is a form. We have a route for it: it’s the route to the create method. Route::prefix(‘/personalcars’)->group(function() { Route::get(‘/create’,

Understanding the Show Method for Data Display The last article ended with us showing all of the resources in the personal_cars table. We need to be able to click on the resource and pull up the details for it. This is what the show method will do. It accepts a resource $id and then pulls the information from it. https://dinocajic.medium.com/laravel-p40-controller-index-method-cmp-f0c2b29861fc Adding the Link to Our Index View Before we can start working on the show method, we need to have a way to get to it. The route exists, so that’s one thing already cross off the list. Route::prefix(‘/personalcars’)->group(function() { // … Route::get(‘/{id}’, [PersonalCarController::class, ‘show’]); }); The user

Laravel Index Method: A Guide to Controller Essentials The controller architecture and the routes are setup and ready for us to start tackling. The first one on the list is index. We’re going to pull all of the data out of the table, put it into a view, and return it back to the user. https://medium.com/geekculture/laravel-p39-routes-and-controllers-high-level-setup-cmp-8b8cd8d0c46c The Route and the Controller Method We currently have the following code already created. Route::prefix(‘/personalcars’)->group(function() { Route::get(‘/’, [PersonalCarController::class, ‘index’]); // … }); <?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PersonalCarController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response

Streamlining Laravel: Routes and Controllers High-Level Overview Now that our Model relationships are defined, we can start setting up the initial infrastructure for our routes and controllers. We’ll list out what kind of data will be sent, what we’re expecting, and then we’ll worry about creating views in the next article. https://medium.com/geekculture/laravel-p38-many-to-many-relationship-cmp-bd8847de9114 Brainstorming I’ve purposefully not created any of this beforehand. I’m writing this code as it seems natural to me. What I want to do is brainstorm on what we need to create next. We know that we have the following models: PersonalCarBrand PersonalCarModel PersonalCar Image The question is,

Laravel Relationships Unlocked: Using belongsToMany, attach, and detach The many-to-many relationship is slightly more complicated than the relationships that we’ve looked at so far. For our example, we have a PersonalCar and that PersonalCar has images. If we looked at the inverse of it, the Image can have one or more PersonalCars. How do you store the relationship? Does PersonalCar need to have an image_id (foreign id) inside it and does Image also need a personal_car_id inside it? This is where the concept of pivot tables come in. It’s a table that stores the foreign id of each table. The Pivot Table Migration We have already looked at the pivot table migration when we

Mastering Models in Laravel Our tables have been created and populated. We need to do some tweaking to our models in order to get them behaving like we want them to. Primarily, we need to make sure that data can be inserted and that relationships are setup. What kind of relationships? Remember that we have foreign id’s in a couple of our tables. We need to let Laravel know how to access data from the referenced tables. Laravel — P36: Seeders and Factories (CMP) Fillable In order to add data inside of our tables when we submit our forms, we

Master Data Generation with Laravel Seeders and Factories Our database has been created, so we’ll work on seeding our tables. I normally like to take care of the database architecture first before I move into the routes/controllers/models, but it’s all preference. If you’re doing TDD (test driven development), it won’t work quite like this. You’ll start with a route, then move to your controller, then your model/migration, etc. We’ll cover TDD later in the series, but for now, let’s look at seeding our tables. Laravel — P35: Database Setup (CMP) PersonalCarBrand Seeder Our PersonalCarBrand model contains a name and a slug. Whenever we’re seeding the

Setting the Stage: Database Configuration for the Car Management Project Time to get our hands dirty and start working on this project. What we want to do is have a Car Management system for our car collection. We’ll add items like the image of our car, the year, make, model, estimated value, etc. Laravel — P34: Introduction to the Car Management Project Database Schema Let’s see what this system schema needs to look like. To generate the content, we’ll need to create 5 different migrations. To help speed up the process, we know that we’ll need 4 models along with

An Introduction to a Laravel Car Management Project We’ve made it past the introductory phase and are ready to put our knowledge to the test. In the next few articles, we’re going to be making a car management system. Laravel — P33: Seeders If you haven’t taken a look at the first 33 articles, we’ve gone through a lot. We covered the installation process, directory structure basics, basic routing, views (both blade and component based), controllers, models, and database operations, which include Models, migrations, Factories, and Seeders. Most of these have been on a pure introductory level and we’ll come

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