PHP code example of sarala-io / laravel-companion

1. Go to this page and download the library: Download sarala-io/laravel-companion 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/ */

    

sarala-io / laravel-companion example snippets


namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Sarala\Filterable;

class Post extends Model
{
    use Filterable;

    ...
}


namespace App\Http\Controllers;

use App\Post;
use App\Filters\PostsFilter;
use App\Http\Transformers\PostTransformer;
use Sarala\JsonApiResponse;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index(Request $request, PostsFilter $filters)
    {
        $data = Post::filter($filters)->paginateOrGet($request);

        return new JsonApiResponse($data, new PostTransformer(), 'posts');
    }

    public function show(Post $post, PostsFilter $filter)
    {
        $data = Post::filter($filter)->find($post->id);

        return new JsonApiResponse($data, new PostTransformer(), 'posts');
    }
    
    ...
}
 php
namespace App\Filters;

use Sarala\FilterAbstract;

class PostsFilter extends FilterAbstract
{
    protected $lookup = [
        'my' => 'composedByMe'
    ];

    public function composedByMe()
    {
        return $this->builder->composedBy(auth()->user());
    }
    
    ...
}