PHP code example of lexxsoft / odata
1. Go to this page and download the library: Download lexxsoft/odata 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/ */
lexxsoft / odata example snippets
...
use Lexxsoft\Odata\RequestQuery;
...
public function index(Request $request): AnonymousResourceCollection
{
$requestQuery = RequestQuery::make();
$perPage = $requestQuery->limit > 0 ? $requestQuery->limit : 10;
$builder = $requestQuery->noLimit()->builder(App::make(Document::class));
if (!$requestQuery->hasOrder()) {
$builder = $builder->orderBy('created_at', 'desc');
}
if ($requestQuery->search !== '') {
$builder = $builder->where('name', 'like', '%' . $requestQuery->search . '%');
}
if (isset($request->period)) {
$startFrom = now()->subtract($request->period . ' days');
$builder = $builder->where('created_at', '>=', $startFrom->format('Y-m-d'));
}
$docs = $builder->where('created_by', Auth::id())
->paginate($perPage)
->withQueryString();
return DocumentResource::collection($docs);
}