TappNetwork / laravel-airtable

Airtables Client for Laravel
MIT License
99 stars 35 forks source link
airtables composer hacktoberfest hacktoberfest2021 laravel laravel-airtables php

Laravel SDK for Airtable

Latest Stable Version Code Style Action Status - Pint PHPStan Tests Quality Score Total Downloads

A simple approach to interacting with Airtables.

Installation

You can install the package via composer:

composer require tapp/laravel-airtable

Publish the config file:

php artisan vendor:publish --provider="Tapp\Airtable\AirtableServiceProvider"

Define airtables account information in .env:

AIRTABLE_KEY=
AIRTABLE_BASE=
AIRTABLE_TABLE=
AIRTABLE_TYPECAST=false 

Example Config

If you need to support multiple tables, add them to the tables config in the config/airtable.php If your table is on a different base than the one set in the env, add that as well.

...
    'tables' => [

        'default' => [
            'name' => env('AIRTABLE_TABLE', 'Main'),
            'base' => 'base_id',
        ],

        'companies' => [
            'name' => env('AIRTABLE_COMPANY_TABLE', 'Companies'),
            'base' => 'base_id',
        ],
        ...
    ],
...

Usage

Import the facade in your class.

use Airtable;

Get records from that table

Airtable::table('tasks')->get();

Get all records from that table.

Get one record from the default table.

Airtable::find('id_string');

Filter records

Filter records by formula

Sorting records

Airtable::orderBy('id')->get();
Airtable::orderBy('created_at', 'desc')->get();

You can sort by multiple fields by calling orderBy more than once (a single call with array syntax is not supported):

Airtable::orderBy('id')->orderBy('created_at', 'desc')->get();

Set other API Parameters

Airtable::addParam('returnFieldsByFieldId', true); // one param at a time
Airtable::params(['returnFieldsByFieldId' => true, 'view' => 'My View']) // multiple params at once

Create

Airtable::create(['name' => 'myName']);

First or Create

Update or Create

Airtable::table('companies')->firstOrCreate(['Company Name' => $team->name]);


#### Update 
- First argument will be the id
- Second argument is the whole record including the updated fields

**Note:** Update is destructive and clear all unspecified cell values if you did not provide a value for them. use PATCH up update specified fields

``` php
Airtable::table('companies')->update('rec5N7fr8GhDtdNxx', [ 'name' => 'Google', 'country' => 'US']);

Patch

Mass Update or Patch

Airtable::table('companies')->patch([
    [
        'id' => 'rec5N7fr8GhDtdNxx',
        'fields' => ['country' => 'US']
    ],
    [
        'id' => 'rec8BhDt4fs2',
        'fields' => ['country' => 'UK']
    ],
    ...
]);

Destroy

Airtable::table('companies')->destroy('rec5N7fr8GhDtdNxx');

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email steve@tappnetwork.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.