This commit is contained in:
2023-10-31 23:21:29 +02:00
parent b595e00861
commit 7ac59fc0fe
4 changed files with 194 additions and 1 deletions
View File
+92
View File
@@ -0,0 +1,92 @@
---
gitea: none
include_toc: true
---
# Queries
## Graph or Tree
Queries can return results in 2 formats. A graph or a tree.
Graphs results are a collection of records (nodes) and a collection of edges. This format is more useful for network visualization.
The tree format is more straightforward as it returns a collection of records. Each record has a **_children** and a **_parents** field and the tree can continue upwards or downwards according to the depth you have requested.
For example to request records with their children and their children's children:
```php
$query->childrenDepth(2);
```
Maybe you only want to get a specific type of relationship:
```php
$query->childrenDepth(2)->childrenFields(["categories"]);
```
## Filters
You can filter your query with the following format:
```php
$query->filter(["field_operator" => "value"]);
// example:
$query->filter(["date_lt" => "2020-09-15"]);
```
Or filters are also available:
```php
$query
->filter(["price_eqn" => 10])
->orFilter(["title_regex" => "search", "slug_regex" => "search"])
;
```
## Operator List
- regex
- eq
- ne
- eqnum
- neqnum
- filter
- eqtrue
- eqfalse
- netrue
- nefalse
- in:
- innum
- nin
- ninnum
- lt
- lte
- gt
- gte
- null
- nnull
- exists
- nexists
## Example
Get 10 posts from the sports category as a tree
```php
$posts = $query
->filter([
"schema" => "posts",
"children.categories.data.slug" => "sports",
]
)
->childrenDepth(1)
->limit(10)
->tree();
$posts->map(...)->toArray();
```
+101
View File
@@ -0,0 +1,101 @@
# Website Setup
This is an opinionated website setup. It's irrelevant to both Lucent and Laravel. It is just the way I like to organize everything.
## Summary
- Create a Lucent folder with Schemas, Image Filters and Pages
- Pages are similar to controllers, but will also get used to statically generate the website
- A Context singleton class that acts as a container for global state
- A middleware to initiate the Context class and set the application state for the request
- Some helper functions to make our life easier
## Context Class
```php
<?php namespace App\Lucent;
class Context
{
public string $website = "";
public string $title;
public string $description = "";
public string $url = "";
public string $mode = "static";
public string $locale = "el";
public array $data = [];
public array $locales = ["el","en"];
protected array $pages = [
"homepage" => Homepage::class,
];
public function __construct(
public Application $app,
public Query $query
){}
public function render(string $pageName, ...$args): string
{
$page = $this->app->make($this->pages[$pageName]);
return $page->render(...$args);
}
public function getTitle()
{
return empty($this->title) ? $this->website : $this->title . " | " . $this->website;
}
public function getFullUrl(string $path = "")
{
return config("app.url") . $this->generateLocaleUrl($path, $this->locale);
}
public function localeUrl(string $path = ""): string
{
return $this->generateLocaleUrl($path, $this->locale);
}
public function switchLocale(string $locale, $path = ""): string
{
return $this->generateLocaleUrl($path, $locale);
}
private function generateLocaleUrl(string $path, string $locale): string
{
if ($this->mode == "preview") {
return config("app.url") . "/preview/" . $locale . "/" . trim($path, "/");
}
return config("app.url") . "/" . $locale . "/" . trim($path, "/");
}
}
```
Add this class to `AppServiceProvider` as a singleton
```php
$this->app->singleton(Context::class);
```
## Context Middleware
```php
<?php namespace App\Http\Middleware;
class Context
{
public function __construct(public Context $context){}
public function handle(Request $request, Closure $next, string $mode = "static"): Response
{
$this->context->locale = $request->route("locale");
App::setLocale($this->meta->locale);
$this->context->mode = $mode;
$this->context->loadData();
return $next($request);
}
}
```
Add the middleware inside the HTTP Kernel file.