Download the PHP package rafaelwendel/phpsupabase without Composer
On this page you can find all versions of the php package rafaelwendel/phpsupabase. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rafaelwendel/phpsupabase
More information about rafaelwendel/phpsupabase
Files in rafaelwendel/phpsupabase
Package phpsupabase
Short Description PHPSupabase is a library written in php language, which allows you to use the resources of a project created in Supabase (https://supabase.io), through integration with its Rest API.
License MIT
Homepage https://github.com/rafaelwendel/phpsupabase
Informations about the package phpsupabase
PHPSupabase
PHPSupabase is a library written in php language, which allows you to use the resources of a project created in Supabase (supabase.io), through integration with its Rest API.
Content
- About Supabase
- PHPSupabase Features
- Instalation & Loading
- How to use
- Auth Class
- Create a new user with email and password
- Sign in with email and password
- Get the data of the logged in user
- Update user data
- Database class
- Insert data
- Update data
- Delete data
- Fetch data
- Comparison operators
- QueryBuilder class
- Auth Class
About Supabase
Supabase is "The Open Source Firebase Alternative". Through it, is possible to create a backend in less than 2 minutes. Start your project with a Postgres Database, Authentication, instant APIs, realtime subscriptions and Storage.
PHPSupabase Features
- Create and manage users of a Supabase project
- Manage user authentication (with email/password, magic links, among others)
- Insert, Update, Delete and Fetch data in Postgres Database (by Supabase project Rest API)
- A QueryBuilder class to filter project data in uncomplicated way
Instalation & loading
PHPSupabase is available on Packagist, and instalation via Composer is the recommended way to install it. Add the follow line to your composer.json
file:
or run
How to use
To use the PHPSupabse library you must have an account and a project created in the Supabase panel. In the project settings (API section), you should note down your project's API key
and URL
. (NOTE: Basically we have 2 suffixes to use with the url: /rest/v1
& /auth/v1
, but since version 0.0.5
the definition of one of these suffixes is optional)
To start, let's instantiate the Service()
class. We must pass the API key
and url
in the constructor
The Service
class abstracts the actions with the project's API and also provides the instances of the other classes (Auth
, Database
and QueryBuilder
).
Auth class
Let's instantiate an object of the Auth
class
The $auth
object has several methods for managing project users. Through it, it is possible, for example, to create new users or even validate the sign in of an existing user.
Create a new user with email and password
See how to create a new user with email
and password
.
This newly created user is now in the project's user table and can be seen in the "Authentication" section of the Supabase panel. To be enabled, the user must access the confirmation link sent to the email.
In the third parameter of the createUserWithEmailAndPassword
method you can pass an array containing the user_metadata
to be saved (Ex: name
and age
)
Sign in with email and password
Now let's see how to authenticate a user. The Authentication request returns a access_token
(Bearer Token) that can be used later for other actions and also checks expiration time. In addition, other information such as refresh_token
and user data are also returned. Invalid login credentials result in throwing a new exception
Get the data of the logged in user
To get the user data, you need to have the access_token
(Bearer Token), which was returned in the login action.
Update user data
It is possible to update user data (such as email and password) and also create/update metadata
, which are additional data that we can create (such as first_name
, last_name
, instagram_account
or any other).
The updateUser
method must take the bearerToken
as argument. In addition to it, we have three more optional parameters, which are: email
, password
and data
(array). If you don't want to change some of this data, just set it to null
.
An example of how to save/update two new meta data (first_name
and last_name
) for the user.
Note that in the array returned now, the keys first_name
and last_name
were added to user_metadata
.
Database class
The Database class provides features to perform actions (insert, update, delete and fetch) on the Postgre database tables provided by the Supabase project.
For the samples below, consider the following database structure:
The Database class is also instantiated from the service
object. You must pass the table
that will be used and its respective primary key (usually id
).
Let's create an object to work with the categories
table:
Through the db
variable it is possible to perform the actions on the categories
table.
NOTE: If Row Level Security (RLS) is enabled in the used table, pass the bearerToken
to the Service
object:
Insert data
Inserting a new record in the categories
table:
Now let's insert a new product from category 1 - Video Games
:
Update data
To update a record in the database, we use the update
method, passing as parameter the id
(PK) of the record to be updated and an array
containing the new data (NOTE: For now, it is not possible to perform an update using a parameter other than the primary key).
In the example below, we will update the productname
and price
of the product with id=1
("Xbox Series S" to "XBOX Series S 512GB" and "299.99" to "319.99"):
Delete data
To delete a record from the table, just call the delete
method and pass the id
(PK) of the record to be deleted as a parameter.
The following code deletes the product of id=1
in the products
table:
Fetch data
The following methods for fetching data are available in the Database
class:
fetchAll()
: fetch all table records;findBy(string $column, string $value)
: fetch records filtereds by a column/value (using the=
operator);findByLike(string $column, string $value)
: fetch records filtereds by a column/value (using theLIKE
operator);join(string $foreignTable, string $foreignKey)
: make ajoin
between the seted table and another table related and fetch records;createCustomQuery(array $args)
: build a custom SQL query. The followingkeys
are valid for theargs
argument:select
from
join
where
limit
range
All the mentioned methods return the self instance of Database
class. To access the fetched data, call the getResult
method.
See some examples:
Now, an example using the findBy
method:
Searching for products
and adding a join with the categories
table:
An example of a custom query to search id,productname,price
for all products
"JOIN" categories
filtering by price
(price
greater than 200.00
):
Other examples for custom query:
Comparison operators
The main operators available for the where
clause:
eq
: equalneq
: not equalgt
: greater thangte
: greater than or equallt
: less thanlte
: less than or equallike
: search for a specified pattern in a columnilike
: search for a specified pattern in a column (case insensitive)
Other operators available:
is
in
cs
cd
sl
sr
nxl
nxr
adj
ov
fts
plfts
phfts
wfts
not.eq
not.neq
not.gt
not.gte
not.lt
not.lte
not.like
not.ilike
not.is
not.in
not.cs
not.cd
not.sl
not.sr
not.nxl
not.nxr
not.adj
not.ov
not.fts
not.plfts
not.phfts
not.wfts
QueryBuilder class
The QueryBuilder class provides methods for dynamically building SQL queries. It is instantiated from the service
object.
NOTE: If Row Level Security (RLS) is enabled on any of the tables used, pass the bearerToken
to the Service
object:
Available methods:
select(string $select)
: the fields (comma separated) or*
from(string $from)
: the tablejoin(string $table, string $tablekey, string $select = null)
: related tablewhere(string $column, string $value)
: conditionslimit(int $limit)
: limit rowsorder(string $order)
: the "order by" fieldrange(string $range)
: results range (E.g. "0-3")
All the mentioned methods return the self instance of QueryBuilder
class. To run the mounted query, call the execute
method. Then, to access the fetched data, call the getResult
method.
An example to fetch all data from the products
table:
An example to fetch all data from the products
"JOIN" categories
:
Fetch products
with categoryid=1
and price>200
order by price
:
Some of the operators to be used in the where
method can be seen in the Comparison operators section.