PHP code example of jaffran / laravel-tools

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

    

jaffran / laravel-tools example snippets


use Spatie\QueryBuilder\QueryBuilder;

QueryBuilder::for(Model::class)
    ->allowedFilters('name')
    ->get();

use Jaffran\LaravelTools\Queries\PaginatedQuery;

class UserQuery extends PaginatedQuery
{
    //you have to override the __construct method
    public function __construct()
    {
        parent::__construct(User::query()); //which model to be queried for
    }

    //set filters that are allowed like you usually do when using spatie query builder
    protected function getAllowedFilters(): array
    {
        return [
            AllowedFilter::partial('name'),
            AllowedFilter::partial('email'),
        ];
    }

    //set sorts that are allowed like you usually do when using spatie query builder
    protected function getAllowedSorts(): array
    {
        return [
            AllowedSort::field('created_at'),
            AllowedSort::field('name'),
        ];
    }

    //set 


//specify the attributes that can be appended
protected array $append = [
    'phone',
    'employer',
];

function index(UserQuery $query){
    return $query->ly filter
}

//or you could simplify it even more with
function index(UserQuery $query){
    return $query->filterSortPaginateWithAppend();
}

use Jaffran\LaravelTools\Actions\GenerateOTPAction;

(new GenerateOTPAction)->execute(
    4, //number of seed
    1, //user_id
);

function login(LoginRequest $request, GenerateOTPAction $action){
    //login stuff
    $action->execute(4,1);
}

$article->getFirstMedia()->getUrl();

$article->addMediaFromRequest("image")->toMediaCollection("image");

use Jaffran\jaffranLaravel\Tools\HasSingleImage;
use Spatie\MediaLibrary\HasMedia;

class Article implements HasMedia{
    use HasSingleImage;
}

class Article implements HasMedia{
    use HasSingleImage;

    $defaultThumnailName = "thumbnail";
    $defaultSingleImageName = "Banner";
}

public string $defaultSingleImage = "https://image.com/pepega.png";
public string $defaultThumbnail = "https://image.com/pepega-sm.png";


class ArticleController extends Controller{
    function store(ArticleStoreRequest $request){
        $article = Article::create($request->validated());
        $article->addSingleImageFromRequest(); //THIS!
    }
}

    $article->addSingleImageFromRequest("my_image_key");
    //this will make the package to use "my_image_key" to find the image

$article->getSingleImage();
//or to get the thumbnail
$article->getSingleImageThumbnail();

'available_permissions' => [
    [
        'name' => 'post.create', //permission name
        'roles' => ['super-admin', 'user'] // role that are allowed to access said permission
    ],
    [
        'name' => 'post.view',
        'roles' => ['user','guest','super-admin']
    ]
],

namespace App\Actions;

class VerifyOtpAction
{
    public function execute(string $otp): bool
    {
       //some code
    }
}

class AuthController{
    function verifyOtp(Request $request, VerifyOtpAction $action){
        //you can use it like this
        $isOtpValid = $action->execute($request->otp);
    }
}

$isOtpValid = (new VerifyOtpAction)->execute($otp);

namespace App\Enums;

enum EmployeeAccomodation: int
{
    case WALKING = 1;
    case DRIVING = 2;
    case PUBLIC_TRANSPORTATION = 3;
}

EmployeeAccomodation::from(1); //WALKING

namespace App\Enums;

enum EmployeeAccomodation: int
{
    case WALKING = 1;
    case DRIVING = 2;
    case PUBLIC_TRANSPORTATION = 3;

    function toString(){
        return Str::title(Str::replace('_', ' ', Str::snake($this->name)));
    }

    //
    EmployeeAccomodation::from(3)->toString(); //Public Transportation
}

php artisan vendor:publish --tag=jaffran-laravel-tools-config

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"

php artisan vendor:publish --tag=jaffran-laravel-tools-config

php artisan migrate

php artisan jaffran-permission-role:sync

php artisan install:api