-
app/Http/Resources/PackageResource.php
Open in GitHubuse App\Favorite; use App\Package; use Illuminate\Support\Str; class PackageResource extends ModelResource { public $model = Package::class; const CACHE_RATINGS_LENGTH = 5; public function toArray($package) { return [ 'id' => $package->id, 'name' => $package->name, 'composer_name' => $package->composer_name, 'packagist_namespace' => $package->composer_vendor, 'packagist_name' => $package->composer_package, 'abstract' => $package->abstract, 'is_disabled' => $package->is_disabled, 'icon_url' => $package->picture_url ?? 'https://api.adorable.io/avatars/285/'.Str::slug($package->name).'.png', 'url' => $package->url, 'average_rating' => $this->averageRating($package), 'rating_count' => $this->ratingCount($package), 'created_at' => $package->created_at->diffForHumans(), 'author' => [ 'id' => $package->author_id, 'name' => $package->author->name, 'url' => $package->author->url, 'avatar_url' => $package->author->avatar ?: 'https://api.adorable.io/avatars/285/'.Str::slug($package->author->name).'.png', 'github_username' => $package->author->github_username, ], ]; } // }
-
app/Http/Controllers/Api/PackagesController.php
Open in GitHubuse App\Http\Controllers\Controller; use App\Http\Resources\Package as PackageResource; use App\Package; use Illuminate\Http\Request; class PackagesController extends Controller { public function __invoke(Request $request) { return PackageResource::collection(Package::orderBy('created_at', 'desc')->with(['author', 'tags'])->paginate(10)); } }