# Turso Quickstart (Laravel)

> Turso Quickstart (Laravel) — Turso documentation.
> Canonical HTML: https://tursodb.cn/docs/sdk/php/guides/laravel/
> Upstream Source (MIT): https://docs.turso.tech/sdk/php/guides/laravel
> Language: en

---
In this Laravel quickstart we will learn how to:

* Install the Turso Laravel adapter
* Configure Laravel to use Turso
* Create a model and migration
* Perform CRUD operations using Eloquent

**1. Retrieve database credentials**

You will need an existing database to continue. If you don't have one, [create one](/docs/quickstart/).

Get the database URL:

```bash
turso db show --url <database-name>
```

Get the database authentication token:

```bash
turso db tokens create <database-name>
```

Assign credentials to the environment variables inside `.env`.

```bash
TURSO_DATABASE_URL=
TURSO_AUTH_TOKEN=
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

You will want to store these as environment variables.

</div></div>


**2. Install**

Install the package to your Laravel project using Composer:

```console
composer require turso/libsql-laravel
```

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

Make sure to enable FFI in your PHP configuration to use the Laravel adapter.

</div></div>


**3. Configure**

Choose your preferred setup and configure accordingly:

<details class="doc-accordion"><summary>Local only</summary><div class="doc-accordion-body">

Update your `config/database.php`:

```php
return [
    "default" => env("DB_CONNECTION", "libsql"),

    "connections" => [
        "libsql" => [
            "driver" => "libsql",
            "database" => database_path("database.db"),
        ],
        // ...
    ],
];
```

This will use a local SQLite file with the libSQL adapter.

</div></details>


<details class="doc-accordion"><summary>Remote only</summary><div class="doc-accordion-body">

Update your `config/database.php`:

```php
return [
    "default" => env("DB_CONNECTION", "libsql"),

    "connections" => [
        "libsql" => [
            "driver" => "libsql",
            "url" => env("TURSO_DATABASE_URL"),
            "password" => env("TURSO_AUTH_TOKEN"),
        ],
        // ...
    ],
];
```

Then add to your `.env` file:

```
DB_CONNECTION=libsql
TURSO_DATABASE_URL=libsql://...
TURSO_AUTH_TOKEN=...
```

</div></details>


<details class="doc-accordion"><summary>Embedded Replicas</summary><div class="doc-accordion-body">

Update your `config/database.php`:

```php
return [
    "default" => env("DB_CONNECTION", "libsql"),

    "connections" => [
        "libsql" => [
            "driver" => "libsql",
            "database" => database_path("database.db"),
            "url" => env("TURSO_DATABASE_URL"),
            "password" => env("TURSO_AUTH_TOKEN"),
            "sync_interval" => env("TURSO_SYNC_INTERVAL", 300),
        ],
        // ...
    ],
];
```

Then add to your `.env` file:

```
DB_CONNECTION=libsql
TURSO_DATABASE_URL=libsql://...
TURSO_AUTH_TOKEN=...
TURSO_SYNC_INTERVAL=300
```

> [!NOTE]
>
> The `sync_interval` is optional and defaults to 300 seconds (5 minutes).

</div></details>


**4. Create a model and migration**

Create a User model and migration:

```console
php artisan make:model User -m
```

Update the migration file:

```php
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamps();
    });
}
```

**5. Run migrations**

Apply the migration to create the users table:

```console
php artisan migrate
```

**6. Execute CRUD operations**

Now you can perform CRUD operations using Eloquent:

```php Create
use App\Models\User;

$user = User::create([
    'name' => 'John Doe',
    'email' => 'john@example.com',
]);
```

```php Read
use App\Models\User;

// Fetch all users
$allUsers = User::all();

// Find a specific user by ID
$user = User::find(1);

// Filter users
$filteredUsers = User::where('name', 'John Doe')->get();
```

```php Update
use App\Models\User;

// Update a single record
$user = User::find(1);
$user->name = 'Jane Doe';
$user->save();

// Update multiple records
User::where('name', 'John Doe')->update(['name' => 'Jane Doe']);
```

```php Delete
use App\Models\User;

// Delete a single record
$user = User::find(1);
$user->delete();

// Delete multiple records
User::where('name', 'Jane Doe')->delete();
```
