PHP code example of underwear / laravel-translatable-json-resource

1. Go to this page and download the library: Download underwear/laravel-translatable-json-resource library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

underwear / laravel-translatable-json-resource example snippets




namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Underwear\LaravelTranslatableJsonResource\TranslatableJsonResource;

class ArticleResource extends TranslatableJsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'title' => $this->title, // translatable field,
            'body' => $this->body, // translatable field
            'created_at' => $this->created_at
        ];
    }
}

use App\Http\Resources\ArticleResource;
use App\Models\Articles;

Route::get('/article/{id}', function ($id) {
    $article = Article::findOrFail($id);
    $locale = 'en';
    return new ArticleResource($article, $locale);
});

use App\Http\Resources\ArticleResource;
use App\Models\Articles;

Route::get('/articles', function () {
    $articles = Article::all();
    $locale = 'ru';
    return ArticleResource::collection($articles, $locale);
});