定制 underwear/laravel-translatable-json-resource 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

underwear/laravel-translatable-json-resource

Composer 安装命令:

composer require underwear/laravel-translatable-json-resource

包简介

Spatie Laravel translatable + Laravel JsonResource

README 文档

README

Spatie laravel translatable package + Laravel JsonResource

Installation

composer require underwear/laravel-translatable-json-resource

Usage

Require this package in your project and use TranslatableJsonResource instead of JsonResource.

You don't need to worry about things, just keep using JsonResource as you're used to.

Have a look:

<?php

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
        ];
    }
}

You only need to pass one additional argument locale:

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);
});

It also works with anonymous collections:

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

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

If you don't pass locale argument, it will use default locale from config('app.locale')

统计信息

  • 总下载量: 830
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固