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
Month: August 2024
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
