PHP code example of jamosaur / foundation

1. Go to this page and download the library: Download jamosaur/foundation 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/ */

    

jamosaur / foundation example snippets




declare(strict_types=1);

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\JsonResponse;
use Jamosaur\Foundation\ApiController;

class UserController extends ApiController
{
    public function index(): JsonResponse
    {
        $users = User::all();
        
        // By default, this will attempt to transform the data with
        // `\App\Transformers\UserTransformer`. The package takes replaces
        // the `Controller` in the controller name with Transformer
        // and attempts to load it from the `\App\Transformers\ namespace.
        return $this->transformCollection('users', $users)
            ->respond();
    }
    
    public function differentNamespace(): JsonResponse
    {
        $users = User::all();
        
        // You can define a different namespace for transformers.
        return $this->setTransformerNamespace('\\App\\Domain\\Transformers')
            ->transformCollection('users', $users)
            ->respond();
    }
    
    public function chainingTransformers(): JsonResponse
    {
        $user = User::all();
        $posts = Post::all();
        $photo = Photo::query()->first();
        
        // You can also chain methods to transform multiple
        // sets of data.
        return $this->setTransformer(new UserTransformer())
            ->transformCollection('users', $users)
            ->setTransformer(new PostTransformer())
            ->transformCollection('posts', $posts)
            ->setTransformer(new PhotoTransformer())
            ->transformItem('photo', $photo)
            ->respond();
    }
    
    public function definedTransformer(): JsonResponse
    {
        $users = User::query()->with('profile')->get();
        
        // You can override which transformer is used, and also pass in
        // any