asmiarowski / laravel-postgres
Eloquent support for postgreSQL fields
Installs: 37 191
Dependents: 1
Suggesters: 0
Security: 0
Stars: 15
Watchers: 2
Forks: 10
Open Issues: 0
Requires
- php: >=5.5.9
- laravel/framework: ^5.2
Requires (Dev)
- henrikbjorn/phpspec-code-coverage: ^2.0
- phpspec/phpspec: ^2.4
Suggests
- barryvdh/laravel-ide-helper: Adds IDE autocomplete to query builders through php artisan ide-helper:models
This package is not auto-updated.
Last update: 2024-11-23 19:32:06 UTC
README
Adds support for postgreSQL fields for Eloquent.
Installation
``` composer require asmiarowski/laravel-postgres ```Array field
Add trait to your model:
``` use Illuminate\Database\Eloquent\Model; use Smiarowski\Postgres\Model\Traits\PostgresArray;class ExampleModel extends Model { use PostgresArray; }
<p>Set up accessor and mutator for your array field like so:</p>
public function setArrayField(array $value) { $this->array_field = self::mutateToPgArray($value); } public function getArrayField() { return self::accessPgArray($this->array_field); }
<p>Query scopes available for builder:</p>
<p><b>wherePgArrayContains(string $column, mixed $value)</b>: Adds where query part, $column has all of the elements in $value. $value can be array, integer or string</p>
<p><b>wherePgArrayOverlap(string $column, mixed $value)</b>: Adds where query part, $column has any (at least one) of the elements in $value. $value can be array, integer or string</p>
<p>For example, let's say you have an array of strings as tags for restaurants. If you would want to find all restaurants that serve pizza or lasagne, you would build your query like so:</p>
$restaurants = Restaurant::wherePgArrayOverlap('tag', ['pizza', 'lasagne'])->get();
<p>Above example would return only thoes restaurants that have tags pizza <b>or</b> lasagne in their defined tags field. If you would want only restaurants that have all of the tags specified, you would use <b>wherePgArrayContains</b> instead.</p>