qertmission.blogg.se

Eloquent example
Eloquent example












eloquent example
  1. ELOQUENT EXAMPLE HOW TO
  2. ELOQUENT EXAMPLE INSTALL
  3. ELOQUENT EXAMPLE CODE
  4. ELOQUENT EXAMPLE SERIES

Sail artisan make:factory CategoryFactory -model=Category # Create a factory class for category model Sail artisan make:factory QuestFactory -model=Quest Run the following commands: # Create a factory class for quest model We need data to work with, but as developers we are too lazy to manually do it.įirst, we'll create the factory classes for both the quest and category model. Our database should be updated! Next we should put some data into our tables. With both the migrations and models ready, we can apply the changes to the database.

  • Make the appropriate fields editable by adding them to the $fillable array.
  • Next let's configure the quest model class. This way Laravel will automatically create a foreign key relationship between the tables categories and quests.

    eloquent example

    $table->foreignId('category_id')->constrained() Īs you can see, we declared the category_id to be a foreignId. This will create a model named Quest and a migration file for it. Run the command to make a model and migration file for quest: sail artisan make:model -m Quest You will have some errors concerning the Quest model, but no worries – we will handle that next.

    eloquent example

  • Define the relationship between category model and quest model.
  • Make the field title editable, so we will add it to our $fillable array.
  • Next let's configure the category model class. Our category migrations file should look like this: id() Our category will consist of four fields: This will create the Category model with it's migration file. Let's start with category model: # Create model with migrations This isn't a Laravel tutorial, so we'll quickly create the models with the appropriate migrations. This should create a GraphQL config file that we will use in config/graphql.php. Next we have to publish the GraphQL library like this: sail artisan vendor:publish -provider="Rebing\\GraphQL\\GraphQLServiceProvider" Sail composer require rebing/graphql-laravel # GraphQL library which we are going to use Sail composer require -dev barryvdh/laravel-ide-helper

    ELOQUENT EXAMPLE INSTALL

    Moving on, if you go to localhost you should see something like this: Default Laravel Home Pageīut before we move on, there are some packages that we need to install first: # IDE helper for laravel, always useful to have.

    ELOQUENT EXAMPLE CODE

    You can do that by adding this piece of code to your bashrc or zshrc: # in ~./zshrc or ~./bashrc Let's then run the containers using sail: # Run the containersĪt this point I suggest you alias sail to. If all goes well, you should now see a docker-compose.yml file in your project directory. It's gonna ask you which services to install. Next let's setup sail like this: # Move into the project This will create a new project in a new directory called quest_journal.

    ELOQUENT EXAMPLE HOW TO

    How to Initialize the ProjectĬreate a Laravel project using this command: composer create-project laravel/laravel quest_journal Database Schemaīy the end of this post we will create a CRUD GraphQL API for each model. The project will consist of only two models called Quests and Categories.

    ELOQUENT EXAMPLE SERIES

    I like RPG games like the Elder Scrolls Series or Final Fantasy, so of course our app will be about games. Knowledge of PHP (Syntax, OOP, and so on).Basic knowledge of Laravel (Eloquent, Migrations, MVC, Routes, and so on).

    eloquent example

    Docker-Compose 1.29.1 (Any other version should be fine).Docker 20.10.6 (Any other version should be fine).Prerequisitesīefore we begin, make sure to have these installed on your system: You can find the final project on GitHub here. It was actually easier than I thought, so I would like to share with you what I've learned about GraphQL by creating a simple demo project together. Words like Docker, Kubernetes, and GraphQL seemed quite scary.īut I mustered up the courage and started to learn them all one by one. And I was very intimidated by all the technology I didn't yet know. Two years ago, I started working professionally as a backend developer. In this article, I'll walk you through how to set up your own GraphQL API using PHP and Laravel.














    Eloquent example