|
| 1 | +# Prismic Eloquent |
| 2 | + |
| 3 | +Use the Prismic Api in a more (friendly) eloquent like way. |
| 4 | + |
| 5 | + |
| 6 | +## Requirement |
| 7 | +- Laravel 5.5+ |
| 8 | +- Prismic SDK 4+ |
| 9 | + |
| 10 | +## Installation |
| 11 | +Install through Composer: |
| 12 | + |
| 13 | +``` |
| 14 | +composer require robindrost/prismic-eloquent |
| 15 | +``` |
| 16 | + |
| 17 | +Download Zip: |
| 18 | + |
| 19 | +@todo: github url |
| 20 | + |
| 21 | +## Difference between default Prismic api kit |
| 22 | + |
| 23 | +#### How you normally use the prismic api kit |
| 24 | + |
| 25 | +``` |
| 26 | +class PageController extends Controller |
| 27 | +{ |
| 28 | + public function index() |
| 29 | + { |
| 30 | + $api = Api::get('https://your-repo-name.prismic.io/api/v2'); |
| 31 | + |
| 32 | + $response = $api->query( |
| 33 | + Predicates::at('document.type', 'page'), |
| 34 | + [ |
| 35 | + 'fetchLinks' => ['author.name', 'author.hair_color], |
| 36 | + 'orderings' => '[my.page.date desc]' |
| 37 | + ] |
| 38 | + ); |
| 39 | + |
| 40 | + $result = $response->getResults()[0]; |
| 41 | + |
| 42 | + return view('page', [ |
| 43 | + 'page' => $result, |
| 44 | + ]); |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +page.blade.php: |
| 50 | + |
| 51 | +``` |
| 52 | +<h1>{{ $page->date->title }}</h1> |
| 53 | +``` |
| 54 | + |
| 55 | +#### How to use it with Prismic eloquent |
| 56 | + |
| 57 | +``` |
| 58 | +class PageController extends Controller |
| 59 | +{ |
| 60 | + public function index() |
| 61 | + { |
| 62 | + $page = Page::with('author.name', 'author.hair_color') |
| 63 | + ->orderBy('date desc') |
| 64 | + ->get(); |
| 65 | + |
| 66 | + return view('page', [ |
| 67 | + 'page' => $page, |
| 68 | + ]); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +page.blade.php: |
| 74 | + |
| 75 | +``` |
| 76 | +<h1>{{ $page->title }}</h1> |
| 77 | +``` |
| 78 | + |
| 79 | +## Configuration |
| 80 | + |
| 81 | +Copy the default configuration |
| 82 | + |
| 83 | +``` |
| 84 | +php artisan vendor:publish |
| 85 | +``` |
| 86 | + |
| 87 | +Start with changing the values inside the config/prismic-eloquent.php. You will need. a prismic repository URL and an access token to use the Prismic api. |
| 88 | + |
| 89 | +## Creating models |
| 90 | + |
| 91 | +``` |
| 92 | +namespace App; |
| 93 | +
|
| 94 | +use RobinDrost\PrismicEloquent\Model; |
| 95 | +
|
| 96 | +class Page extends Model |
| 97 | +{ |
| 98 | + public function getTypeName() |
| 99 | + { |
| 100 | + return 'page'; |
| 101 | + } |
| 102 | +} |
| 103 | +
|
| 104 | +``` |
| 105 | + |
| 106 | +By default you are able to retrieve any property from the data type. |
| 107 | + |
| 108 | +``` |
| 109 | +App\Page::findByUid(1)->title; |
| 110 | +
|
| 111 | +``` |
| 112 | + |
| 113 | +Will return The response from the title field. |
| 114 | + |
| 115 | +Optional get methods: |
| 116 | + |
| 117 | +``` |
| 118 | +namespace App; |
| 119 | +
|
| 120 | +use RobinDrost\PrismicEloquent\Model; |
| 121 | +
|
| 122 | +class Page extends Model |
| 123 | +{ |
| 124 | + public function getTitle() |
| 125 | + { |
| 126 | + return $this->attribute('title') . ' my super suffix.'; |
| 127 | + } |
| 128 | +
|
| 129 | + public function getTypeName() |
| 130 | + { |
| 131 | + return 'page'; |
| 132 | + } |
| 133 | +} |
| 134 | +
|
| 135 | +``` |
| 136 | + |
| 137 | +Now calling the title property will use the defined get method. |
| 138 | + |
| 139 | +``` |
| 140 | +App\Page::find(1)->title; |
| 141 | +
|
| 142 | +``` |
| 143 | + |
| 144 | +Will return: |
| 145 | + |
| 146 | +Will return The response from the title field + ' my super suffix'. |
| 147 | + |
| 148 | +#### Field and attributes |
| 149 | + |
| 150 | +By default the model will look for a get{Name} method, or a field with the given name of a direct document attribute like uid. |
| 151 | + |
| 152 | +``` $model->uid ``` Will return the document's uid (**as long as there is no field with the name uid**). |
| 153 | + |
| 154 | + |
| 155 | +## Query the Api |
| 156 | +####Find by UID |
| 157 | + |
| 158 | +``` |
| 159 | +$page = Page::find('my-title'); |
| 160 | +
|
| 161 | +``` |
| 162 | + |
| 163 | +####Find by document ID |
| 164 | + |
| 165 | +``` |
| 166 | +$page = Page::findById('w6yHsaAw98a'); |
| 167 | +
|
| 168 | +``` |
| 169 | + |
| 170 | +#### Collection of pages |
| 171 | + |
| 172 | +``` |
| 173 | +Page::all(); |
| 174 | +``` |
| 175 | + |
| 176 | +#### Collection of pages **filtered** |
| 177 | + |
| 178 | +``` |
| 179 | +Page::where('color', 'red')->get(); |
| 180 | +``` |
| 181 | + |
| 182 | +#### Collection of pages filtered by **multiple values** |
| 183 | + |
| 184 | +``` |
| 185 | +Page::whereIn('color', ['red', 'blue])->get(); |
| 186 | +``` |
| 187 | + |
| 188 | +#### Collection of pages filtered by **tags** |
| 189 | + |
| 190 | +``` |
| 191 | +Page::tags('color', ['red', 'blue])->get(); |
| 192 | +``` |
| 193 | + |
| 194 | +#### Collection of pages filtered by **publication date** |
| 195 | + |
| 196 | +``` |
| 197 | +Page::wherePublicationDate('2018-07-03')->get(); |
| 198 | +``` |
| 199 | + |
| 200 | +``` |
| 201 | +Page::wherePublicationDate('2018', 'year')->get(); |
| 202 | +``` |
| 203 | + |
| 204 | +``` |
| 205 | +Page::wherePublicationDate('may', 'month')->get(); |
| 206 | +``` |
| 207 | + |
| 208 | +#### Ordering |
| 209 | + |
| 210 | +``` |
| 211 | +Page::orderBy('field')->get(); |
| 212 | +``` |
| 213 | + |
| 214 | +``` |
| 215 | +Page::orderBy('field asc')->get(); |
| 216 | +``` |
| 217 | + |
| 218 | +``` |
| 219 | +Page::orderBy('field desc')->get(); |
| 220 | +``` |
| 221 | + |
| 222 | +#### Language's |
| 223 | + |
| 224 | +``` |
| 225 | +Page::language('nl-NL')->find('test-slug'); |
| 226 | +``` |
| 227 | + |
| 228 | +#### Paginate |
| 229 | +You are also able to ti paginated queries. This works exactly the same as the normal paginate query you are used to. It takes a parameter from the url (by default: 'page') and runs a paginated query. The returned value is a LenghtAwarePaginator. |
| 230 | + |
| 231 | +``` |
| 232 | +Page::paginate(); |
| 233 | +``` |
| 234 | + |
| 235 | +You can either specify the amount of pages here or on your model's perPage property. |
| 236 | + |
| 237 | +``` |
| 238 | +Page::paginate(10); |
| 239 | +``` |
| 240 | + |
| 241 | +Custom URL parameter |
| 242 | + |
| 243 | +``` |
| 244 | +Page::paginate(10, [], 'myPageQueryParameter'); |
| 245 | +``` |
| 246 | + |
| 247 | +#### Specify fields |
| 248 | + |
| 249 | +The methods all, get, and paginate have an option to specify fields that should get returned. |
| 250 | + |
| 251 | +``` |
| 252 | +Page::all(['title', 'body']) |
| 253 | +Page::get(['title', 'body']) |
| 254 | +Page::paginate(10, ['title', 'body']) |
| 255 | +``` |
| 256 | + |
| 257 | +#### Chaining |
| 258 | +All methods are chainable e.g: |
| 259 | + |
| 260 | +``` |
| 261 | +Page::wherePublicationDate('may', 'month')->where('field', 'value')->get(); |
| 262 | +``` |
| 263 | + |
| 264 | +## Relationships |
| 265 | + |
| 266 | +Prismic offers an option to fetch linked content (throug content relation field). |
| 267 | +Link: https://prismic.io/docs/php/query-the-api/fetch-linked-document-fields |
| 268 | + |
| 269 | +Please note that you can only specify some fields as related fields as described in the documentation of Prismic (link above). |
| 270 | + |
| 271 | +#### Usage: |
| 272 | + |
| 273 | +Relations are a bit different then you are used to in Eloquent. Relations are defined in get methods (described above). Here is an example: |
| 274 | + |
| 275 | +``` |
| 276 | +class Page extends Model |
| 277 | +{ |
| 278 | + public function getMyAwesomeArticle() |
| 279 | + { |
| 280 | + return $this->relation(Article::class, 'my_awesome_article'); |
| 281 | + } |
| 282 | + |
| 283 | + public function getTypeName() |
| 284 | + { |
| 285 | + return 'page'; |
| 286 | + } |
| 287 | +} |
| 288 | +``` |
| 289 | +You have to specify the related model and field on the current model. |
| 290 | +In this case the related content is an article and the data is currently in the field my_awesome_article. |
| 291 | + |
| 292 | +``` |
| 293 | +$page = Page::with('article.title', 'article.body')->get(); |
| 294 | +
|
| 295 | +dump($page->article->title); |
| 296 | +dump($page->article->body); |
| 297 | +``` |
| 298 | + |
| 299 | +Note that you need to specify the "content type" in this case "article". |
| 300 | + |
| 301 | +#### Group with relational fields |
| 302 | + |
| 303 | + |
| 304 | +``` |
| 305 | +class Page extends Model |
| 306 | +{ |
| 307 | + public function getMyAwesomeArticles() |
| 308 | + { |
| 309 | + return $this->relations(Article::class, 'mygroup_field', 'my_awesome_articles'); |
| 310 | + } |
| 311 | + |
| 312 | + public function getTypeName() |
| 313 | + { |
| 314 | + return 'page'; |
| 315 | + } |
| 316 | +} |
| 317 | +``` |
| 318 | + |
| 319 | +This will return an collection of the group field with loaded relations. |
| 320 | + |
| 321 | +``` |
| 322 | +$page = Page::with('article.title', 'article.body')->get(); |
| 323 | +
|
| 324 | +$page->articles->each(function ($item) { |
| 325 | + dump($item->my_awesome_articles->title; |
| 326 | +}); |
| 327 | +``` |
| 328 | + |
| 329 | + |
| 330 | +## Caching |
| 331 | + |
| 332 | +You are free to cache the returned data in any way you want. Like with the normal Laravel models. |
| 333 | + |
| 334 | +## Feedback |
| 335 | + |
| 336 | +Please create issues or pull requests in case you find any problems while using the package. |
0 commit comments