> ## Documentation index
> Fetch the complete documentation index at: https://upstash.com/docs/llms.txt
> Ask it with GET https://upstash.com/docs/ask?q=<keywords or question>. Short keywords rank best.
> Use these to discover all available pages before exploring further.
> Feature requests: https://upstash.com/docs/common/help/feature-requests.md lists what users asked for and how to vote for a request or add one.
> Page rating: `GET https://upstash.com/docs/feedback?path=/docs/redis/quickstarts/laravel&helpful={true|false}&comment={comment}` records whether this page helped. The `comment` (URL-encoded, at most 500 characters) says what helped, or what was missing or wrong; a `helpful=false` rating needs it. It is anonymous and needs no key. Comments hold no code, secrets or personal data.

# Upstash Redis with Laravel

> Use Upstash Redis in a Laravel app with Predis or PhpRedis, including the Redis facade and Laravel's cache, session and queue drivers.

## Project Setup

To get started, let’s create a new Laravel application. If you don’t have the Laravel CLI installed globally, install it first using Composer:

```shell
composer global require laravel/installer
```

After installation, create your Laravel project:

```shell
laravel new example-app
cd example-app
```

Alternatively, if you don’t want to install the Laravel CLI, you can create a project using Composer:
```shell
composer create-project laravel/laravel example-app
cd example-app
```

## Redis Client Setup

Laravel needs a Redis client to talk to your database. The easiest option is Predis, a pure-PHP client that requires no extension:

```shell
composer require predis/predis
```

<Info>
  If you have the [PhpRedis](https://github.com/phpredis/phpredis) PECL extension installed, you can skip this step, since Laravel uses it by default.
</Info>

## Database Setup

Create a Redis database using [Upstash Console](https://console.upstash.com). Go to the **Connect to your database** section and click on **PHP** and then **Laravel**. Copy those values into your .env file. `REDIS_PASSWORD` takes the **Token** shown in the console, which is also your database's password. Note the `tls://` prefix on the endpoint, Upstash requires TLS connections:

```shell .env
REDIS_CLIENT="predis"
REDIS_HOST="tls://<YOUR_ENDPOINT>"
REDIS_PORT=6379
REDIS_PASSWORD="<YOUR_TOKEN>"
```

## Framework Integration
Upstash Redis integrates seamlessly with Laravel, allowing it to be used as a driver for multiple framework components.

### Interact with Redis

The Redis Facade in Laravel provides a convenient way to interact with your Redis database. For example:

```php
use Illuminate\Support\Facades\Redis;

// Storing a value in Redis
Redis::set('key', 'value');

// Retrieving a value from Redis
$value = Redis::get('key');
```

This can be particularly useful for simple caching or temporary data storage.

### Cache
To use Upstash Redis as your caching driver, update the CACHE_STORE in your .env file:

```shell .env
CACHE_STORE="redis"
REDIS_CACHE_DB="0"
```

With this configuration, you can use Laravel’s caching functions, such as:

```php
Cache::put('key', 'value', now()->addMinutes(10));
$value = Cache::get('key');
```

For more advanced cache configurations, see the [Laravel Cache Documentation](https://laravel.com/docs/cache).

### Session
Laravel can store session data in Upstash Redis. To enable this, set the SESSION_DRIVER in your .env file:

```shell .env
SESSION_DRIVER="redis"
```

This ensures that session data is stored in your Upstash Redis database, providing fast and reliable session management.

### Queue
Upstash Redis can also serve as a driver for Laravel’s queue system, enabling job processing. To configure this, update the QUEUE_CONNECTION in your .env file:

```shell .env
QUEUE_CONNECTION="redis"
```

For detailed queue configurations and usage, refer to the [Laravel Queues Documentation](https://laravel.com/docs/queues).
