Laravel Backend
The Laravel Backend
Models
The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.
Controllers
Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes. Controllers can group related request handling logic into a single class. Controllers are stored in the app/Http/Controllers
directory.
Controller examples
This controller function returns all measurements sorted on temperature, humidity and movement on the requested roomnumber. getLocationData()
is a private function made in this class which returns one specific value (temperature, humidity or movement in this case) on the requested location.
The getLocationData()
function
getLocationData()
functionWith this function its easy to make other functions that have the same functionality as seen below.
Migrations
Migrations are like version control for your database, allowing your team to easily modify and share the application's database schema. Migrations are typically paired with Laravel's schema builder to easily build your application's database schema. If you have ever had to tell a teammate to manually add a column to their local database schema, you've faced the problem that database migrations solve.
Migration example (measurements)
Seeds
Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds
directory. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UsersTableSeeder
, etc. By default, a DatabaseSeeder
class is defined for you. From this class, you may use the call
method to run other seed classes, allowing you to control the seeding order.
Routes
All Laravel routes are defined in your route files, which are located in the routes
directory. These files are automatically loaded by the framework. The routes/web.php
file defines routes that are for your web interface. These routes are assigned the web
middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php
are stateless and are assigned the api
middleware group.
API Routes used for the project
You can see the routers are linked to the Controllers and a specific function that is made in the controller.
GraphQL API
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more.
Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results. Apps using GraphQL are fast and stable because they control the data they get, not the server.
GraphQL queries access not just the properties of one resource but also smoothly follow references between them. While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections
Rest API vs GraphQL API
Similar: The list of endpoints in a REST API is similar to the list of fields on the
Query
andMutation
types in a GraphQL API. They are both the entry points into the data.Similar: Both have a way to differentiate if an API request is meant to read data or write it.
Different: In GraphQL, you can traverse from the entry point to related data, following relationships defined in the schema, in a single request. In REST, you have to call multiple endpoints to fetch related resources.
Different: In GraphQL, there’s no difference between the fields on the
Query
type and the fields on any other type, except that only the query type is accessible at the root of a query. For example, you can have arguments in any field in a query. In REST, there’s no first-class concept of a nested URL.Different: In REST, you specify a write by changing the HTTP verb from
GET
to something else likePOST
. In GraphQL, you change a keyword in the query.
GraphQL API in our project
you can find the code of the laravel backend with the graphql api on the master branch of https://github.com/aythonhouttekier/smart-campus-laravel-backend
Installation
Version 1.0 is released. If you are upgrading from older version, you can check Upgrade to 1.0.
Dependencies:
Laravel 5.x
GraphQL PHP
1- Require the package via Composer in your composer.json
.
2- Run Composer to install or update the new requirement.
or
Laravel >= 5.5.x
1- Publish the configuration file
2- Review the configuration file
Laravel <= 5.4.x
1- Add the service provider to your config/app.php
file
2- Add the facade to your config/app.php
file
3- Publish the configuration file
4- Review the configuration file
you can define multiple schemas. Having multiple schemas can be useful if, for example, you want an endpoint that is public and another one that needs authentication.
You can define multiple schemas in the config/graphql.php:
Creating a query
In the graphQL api of our project we made a type and a query for all the models here I will give you the code from the query and type of the user model. You can find the code from the other models on github.
First we create a type.
?php
namespace App\GraphQL\Type;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Type as BaseType;
use GraphQL;
use App\User;
class UserType extends BaseType
{
protected $attributes = [
'name' => 'UserType',
'description' => 'A UserType'
];
public function fields()
{
return [
'id' => [
'type' => Type::int()
],
'name' => [
'type' => Type::string()
],
'email' => [
'type' => Type::string()
],
'password' => [
'type' => Type::string()
],
'remember_token' => [ 'type' => Type::string()
],
];
}}
Add the type to the config/graphql.php
configuration file
Then you need to define a query that returns this type (or a list). You can also specify arguments that you can use in the resolve method.
Add the query to the config/graphql.php
configuration file
And that's it. First use the command php artisan serve
You should get a response that the laravel development server is running on 127.0.0.1:8000 then we should be able to query GraphQL with a request to the url /graphiql.
Try a GET request with the following query
input
The response from graphiql is then
We can also put some relationships in the graphQL API you can see the code for implementing relationships on my github.
Here is an example query after the implementation of relationships
The response then will be
You can find the full code on my github under the master branch https://github.com/aythonhouttekier/smart-campus-laravel-backend
Last updated