PHP code example of sheikhheera / requent

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

    

sheikhheera / requent example snippets


User::select('email')
->with(['posts' => function($query) {
    $query
    ->orderByDesc('id')
    ->select('title')
    ->with(['comments' => function($query) {
        $query->select('body');
    }])
}]);

Route::get('users', 'UserController@index');



namespace App\Http\Controllers;

use Requent;
use App\User;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function index()
    {
        return Requent::resource(User::class)->get();
    }
}

$resource = Requent::resource(User::class);

$resource = Requent::resource(new User);

$resource = Requent::resource(app(User::class)->where('role', 'admin'));

$result = Requent::resource(
    app(User::class)->where('role', 'admin')
)
->transformBy(UserTransformer::class)
->keyBy('users')
->get();

return Requent::resource(User::class)->get();

return Requent::resource(User::class)->paginate(); // or paginate(10)

return Requent::resource(User::class)->simplePaginate(); // or simplePaginate(10)

return Requent::resource(User::class)->find($id);

return Requent::resource(User::class)->first();


// In Controller

public function fetch($id = null)
{
    return Requent::resource(User::class)->fetch($id);
}

http://example.com/users?fields=posts{comments}

http://example.com/users?fields=posts{comments}&paginate=simple&per_page=5

http://example.com/users/1?fields=posts{comments}

return Requent::resource(User::class)->keyBy('users')->get();

return Requent::resource(User::class)->keyBy('user')->find(1);

public function fetch($id = null)
{
    return Requent::resource(User::class)->keyBy($id ? 'user' : 'users')->fetch($id);
}



namespace App\Http\Transformers;

use Requent\Transformer\Transformer;

class UserTransformer extends Transformer
{
    public function transform($model)
    {
        return [
            'id' => $model->id,
            'name' => $model->name,
            'email' => $model->email,
        ];
    }
}

return Requent::resource(User::class, UserTransformer::class)->fetch($id);

return Requent::resource(User::class, new UserTransformer)->fetch($id);

return Requent::resource(User::class)->transformBy(UserTransformer::class)->fetch($id);

return Requent::resource(User::class)->transformBy(new UserTransformer)->fetch($id);

class UserTransformer extends Transformer
{
    public function transform($model)
    {
        return [
            'id' => $model->id,
            'name' => $model->name,
            'email' => $model->email,
        ];
    }
    
    // To allow inclussion of posts
    public function posts($model)
    {
        return $this->items($model, PostTransformer::class);
    }
}

namespace App\Http\Transformers;

use Requent\Transformer\Transformer;

class PostTransformer extends Transformer
{
    public function transform($model)
    {
        return [
            'post_id' => $model->id,
            'post_title' => $model->title,
            'post_body' => $model->body,
        ];
    }

    // User can select related user for each Post model
    public function user($model)
    {
        return $this->item($model, new UserTransformer);
    }

    // User can select related comments for each Post model
    public function comments($collection)
    {
        return $this->items($collection, new CommentTransformer);
    }
}

namespace App;

use App\User;
use App\Comment;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}


$result = Requent::resource(User::class)->raw()->fetch($id);


use Requent;
use App\User;
use App\Http\Controllers\Controller;
use Requent\Transformer\TransformerHelper;
use App\Http\Transformers\UserTransformer;

class HomeController extends Controller
{
    use TransformerHelper;
    
    public function index()
    {
        $result = Requent::resource(User::class)->raw()->get();
    
        return $this->transform($result, UserTransformer::class, 'users');
    }
}

public function transform($model)
{
    if(!$model->relationLoaded('comments')) {
        $model->load('comments');
    }
    
    return [
        'post_id' => $model->id,
        'post_title' => $model->title,
        'post_body' => $model->body,
    ];
}

http://blog54.dev/1?fields=posts{user,comments.orderBy(id){user}}

http://example.com/1?fields=posts{user,comments.orderByDesc(id){user}}

http://example.com/1?fields=posts{user,comments.offset(2).limit(1){user}}

http://example.com/1?fields=posts.orderBy(title).limit(3){user,comments.orderByDesc(id).skip(2).take(1){user}}

php artisan vendor:publish --provider="Requent\RequentServiceProvider" --tag="config"