![]() ![]() Let's get Laravel to use SQLite to avoid needing a MySQL database. (Hint: we would also want an admin-based endpoint for editing and one for customers to only view.) Pop open app/Http/Controllers/ReservationController.php and give it a good ole update like the following. To keep things simple, we're just going to load all reservations with all relations so we can get a big picture. Next, we need to fetch models in our ReservationController::index method. ![]() Edit routes/api.php and add the following entry after the default code. Next, we need to make a route and update the controller to retrieve our models in a serialized JSON format. That's more than enough to create a sample schema that we will import into Stoplight Studio for fine-tuning. \App\Models\Reservation::factory(2)->create() įor testing, we'll generate four different users and two reservations. ![]() Note! We need to import users first since our reservation factory randomly selects user relationships. Then, let's update the database seeder database/seeders/DatabaseSeeder.php so we get some generated models. We will also randomly load a user and add it as the reference for our reservation owner and approval user. We need to update the definition to use some random data from the Faker library. Edit the database/factories/ReservationFactory.php that was generated. Now that we have the schema and model relationships defined, let's update the factory to generate models with sweet sample data. ![]() Return $this->belongsTo(User::class, 'reserved_for', 'id') belongsTo(User::class, 'approved_by', 'id') Edit app/Models/Reservation.php and add the following methods so that the ORM understands our desired relationships to the User model. Next, we need to edit the model so that the Eloquent ORM understands the relationships we desire on approved_by and reserved_for. $table->boolean('confirmed')->default(false)
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |