This is a community collaborative translation of the article, has been translated, more information please click
Introduction to collaborative translation 。
I’ve been using Fractal for API development for the past two years.
If there’s one feature I’d most like Laravel to add, it’s easy data conversion in order to develop a better API.
Don’t get me wrong, Fractal is great, but I’ve always wanted to develop in a framework. I don’t use class libraries if I can! I don’t like using third party libraries to complicate development.
Over the past year, I’ve enjoyed using front-end frameworks such as Vue and React for development. Therefore, I chose to use only Laravel to build the API. When I need to build an API, Fractal is my library of choice. Now, things have changed.
In Laravel 5.5, we have API resources, which I’m really excited about.
Laravel 5.5 was released 2 hours ago while I was having coffee with a friend. When I read this tweet about half an hour ago, the first thought that came to my mind was to use API resources to publish my first blog post, which I did.
Laravel’s API resources are based on Fractal, so I didn’t spend much time learning how to use it. So, let’s begin to understand it…
2
retranslation
View the other two versions
Create the Laravel application
Use the common command line to create the Laravel application
composer create-project laravel/laravel Laravel55ApiCopy the code
.env.example
.env
php artisan key:generateCopy the code
Start the service
php artisan serveCopy the code
Good. What’s next?
0
retranslation
View the other version
Create a Product resource
API resources are a new feature for converting your models and collections of models to JSON in Laravel. Next let’s create a resource for Product.
php artisan make:resource ProductCopy the code
app/Http/Resources
Of course we also need the database migration, model, and controller for Product. We can create these quickly with this command.
php artisan make:model Product -mcCopy the code
up
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('price');
$table->timestamps();
});
}Copy the code
I beg you, never store your price data in floating point!
Be sure to store it as an integer!
Now connect to the database with your Laravel application and run the migration to generate the tables.
This is not a Laravel one-to-one tutorial post, so I won’t waste too much of your time on connecting to the database.
1
retranslation
The next?
So far, we have models, controllers, database migrations, and resource classes for converting models and model collections to JSON. So what happens next?
resources
With that in mind, let’s open the product.php resource class file.
There’s a toArray method, and that’s the method that returns the array of properties that need to be converted to JSON when we send the response.
Let’s modify it so we can have better ideas.
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'price' => $this->price,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}Copy the code
id, name, price, created_at
updated_at
If we remove the price field from the toArray method, there will be no price in the JSON returned. Isn’t that cool?
1
retranslation
useProduct
resources
toArray
product
product
<?php
namespace App\Http\Controllers;
use App\Product;
use App\Http\Resources\Product as ProductResource;
class ProductController extends Controller
{
public function show ($id)
{
return new ProductResource(Product::find($id));
}
}Copy the code
product
product
product
show
api.php
Route::get('/products/{id}', 'ProductController@show');Copy the code
Now, hand in your products list to add a new product, and then visit http://127.0.0.1:8000/api/products/1 to see a simple product.
Here’s what you should get:
Now let’s modify our resources a bit. If you don’t want to expose the price of your product, all you have to do is simply delete it from your toArray method. Once you remove the price from the toArray method, you should get something like this, excluding price of course:
1
retranslation
Is that all?
toArray
toArray
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'test' => 'This is just a test',
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}Copy the code
Here are the results:
price
integer
(int) $this->price
integer
create_at
updated_at
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'test' => 'This is just a test',
'created_at' => (string)$this->created_at,
'updated_at' => (string)$this->updated_at,
];
}Copy the code
Now the result is this:
1
retranslation
View the other version
conclusion
This is just a small example of using Laravel API resources.
If I keep writing, THIS article will never be finished.
So that’s it for this article, and as you know, there are many more topics to discuss, such as paging, resource collections, associations, and data wraps.
0
retranslation
All translations in this article are for study and communication purposes only. Please note the translator, source, and link to this article
Our translation work is in accordance with
CCIf our work has violated your rights and interests, please contact us immediately.