1. Go to this page and download the library: Download aros/omnifetch-lumen 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\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Class Author
* @package App\Models
*
* @property integer $id
* @property integer $publisher_id
* @property string $first_name
* @property string $last_name
* @property float $rating
* @property string $created_at
* @property string $modified_at
* @property integer $status_id
*/
class Author extends Model
{
protected $table = 'authors';
public $timestamps = false;
/**
* @return BelongsTo
*/
public function publisher()
{
return $this->belongsTo(Publisher::class, 'publisher_id');
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use OmniFetch\HasJoinWith;
/**
* Class Post
* @package App\Models
*
* @property integer $id
* @property integer $author_id
* @property string $title
* @property string $content
* @property float $rating
* @property integer $likes
* @property string $created_at
* @property string $modified_at
* @property integer status_id
*/
class Post extends Model
{
use HasJoinWith;
protected $table = 'posts';
public $timestamps = false;
/**
* @return BelongsTo
*/
public function author()
{
return $this->belongsTo(Author::class, 'author_id');
}
}
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
use OmniFetch\OmniFetch;
class ExampleController extends Controller
{
public function fetchAllPosts(Request $request)
{
$data = (new OmniFetch())->paginate(Post::query(), $request->query());
return response()->json($data);
}
public function fetchOnePost(Request $request, $post_id)
{
$post = (new OmniFetch())->getSingle(Post::where('id', $post_id), $request->query());
return response()->json($post->toArray());
}
}