Aiven for Laravel

A presentation at PHPUGMRN in October 2021 in by Lorna Jane Mitchell

Slide 1

Slide 1

Aiven for Laravel Lorna Mitchell, Aiven

Slide 2

Slide 2

Aiven for Laravel Laravel The PHP Framework for Web Artisans Aiven Database-as-a-Service, on a cloud of your choice @lornajane | @aiven_io

Slide 3

Slide 3

Databases •MySQL - Natural match for PHP •PostgreSQL - MySQL’s older brother •Redis - Cache? Queue? You choose! •OpenSearch - Open source search backend (fork of Elasticsearch) Bonus content: Artisan helper for Aiven with Laravel @lornajane | @aiven_io

Slide 4

Slide 4

Eloquent ORM Eloquent is Laravel’s database package and ORM It supports PostgreSQL and MySQL, used in most applications @lornajane | @aiven_io

Slide 5

Slide 5

MySQL and PostgreSQL A relational database for every occasion

Slide 6

Slide 6

Configure Connection MySQL connection: DATABASE_URL=mysql://avnadmin:s3cr3t@host:port/defaultdb (may need to disable mysql.sql_require_primary_key ) PostgreSQL connection: DATABASE_URL=postgres://avnadmin:s3cr3t@host:port/defaultdb DB_CONNECTION=pgsql Remove other DB_* fields to use connection strings @lornajane | @aiven_io

Slide 7

Slide 7

Quick Test: Breeze Laravel Breeze is users-in-a-box for Laravel - with database backend. •Set database config •Run php artisan migrate •Start your app php artisan serve •Register at https://localhost:8000/register •Check the database for users @lornajane | @aiven_io

Slide 8

Slide 8

Redis In-memory data store

Slide 9

Slide 9

Redis Requirements Redis has some dependencies, either one of: •PHP extension PhpRedis from PECL •The userland predis/predis package Configure redis in .env: REDIS_URL=rediss://default:s3cr3t@host:port @lornajane | @aiven_io

Slide 10

Slide 10

Redis in Action Add a route to see Redis in action use Illuminate\Support\Facades\Redis; Route::get(‘/redis’, function () { $count = Redis::get(‘count’); if(!$count) { $count = 0; } Redis::incr(‘count’); return “Current value of count: ” . $count; }); The key is laravel_database_count @lornajane | @aiven_io

Slide 11

Slide 11

Redis with Laravel Simple data store, cache, or queue. Cache: https://laravel.com/docs/8.x/cache •Helper functions including tagging, expiry Queues: https://laravel.com/docs/8.x/queues •An object to represent a job and payload •Performed asynchronously by worker apps @lornajane | @aiven_io

Slide 12

Slide 12

OpenSearch Search all the things! (also Elasticsearch)

Slide 13

Slide 13

OpenSearch and Scout •Laravel Scout supports search backends •https://laravel.com/docs/8.x/scout •Plus the Elasticsearch/OpenSearch addition Explorer •https://jeroen-g.github.io/Explorer/ Great writeup: https://laravel-news.com/explorer @lornajane | @aiven_io

Slide 14

Slide 14

Aiven for Laravel Add some Aiven magic to your Laravel project! https://github.com/aiven/aiven-laravel/

Slide 15

Slide 15

Aiven for Laravel Add some artisan commands for easy Aiven database connection and management. composer require aiven/aiven-laravel Set up the environment variables: • AIVEN_API_TOKEN • AIVEN_DEFAULT_PROJECT @lornajane | @aiven_io

Slide 16

Slide 16

Database Config Your artisan command should now have new entries: php artisan list Get database lists and config: php artisan aiven:list php artisan aiven:getconfig —service my-database Paste the config into your .env file @lornajane | @aiven_io

Slide 17

Slide 17

Avoid Cloud Bills Leaving services running can add up! Check if the database is running: php artisan aiven:state —service my-database Turn it on or off: php artisan aiven:powerup —service my-database php artisan aiven:powerdown —service my-database @lornajane | @aiven_io

Slide 18

Slide 18

Aiven and Laravel And a little sprinkle of helper magic

Slide 19

Slide 19

Resources •https://github.com/aiven/aiven-laravel •https://aiven.io (free trial) •https://laravel.com •https://lornajane.net @lornajane | @aiven_io