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/ */
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();
}
}
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);
}
}
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');
}
}