Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I set up row level security (RLS) in Supabase for Laravel
    Laravel Forge


How do I set up row level security (RLS) in Supabase for Laravel


Here is how you can set up row-level security (RLS) in Supabase for your Laravel application:

Enabling RLS in Supabase

1. Create a Supabase Project: Go to the Supabase dashboard and create a new project. This will give you a Postgres database and other features like authentication and APIs.

2. Enable RLS on Tables: When you create new tables in the Supabase dashboard, RLS is automatically enabled. If you create tables using raw SQL, you'll need to manually enable RLS:

sql
   ALTER TABLE "table_name" ENABLE ROW LEVEL SECURITY;
   

Configuring RLS Policies in Supabase

1. Create RLS Policies: Use the Supabase dashboard or the SQL editor to create RLS policies for your tables. For example, to allow users to only see their own records in a `profiles` table:

sql
   CREATE POLICY "Users can only view their own profile" 
   ON profiles
   FOR SELECT
   USING (auth.uid() = user_id);
   

You can create more complex policies using Postgres' powerful RLS features.

Integrating Supabase RLS with Laravel

1. Set up Laravel Project: Create a new Laravel project and configure the Postgres database connection in `config/database.php`:

php
   'pgsql' => [
       'driver' => 'pgsql',
       'url' => env('DATABASE_URL'),
       'host' => env('DB_HOST', '127.0.0.1'),
       'port' => env('DB_PORT', '5432'),
       'database' => env('DB_DATABASE', 'forge'),
       'username' => env('DB_USERNAME', 'forge'),
       'password' => env('DB_PASSWORD', ''),
       'charset' => 'utf8',
       'prefix' => '',
       'prefix_indexes' => true,
       'search_path' => 'public', // Update this to match your Supabase schema
       'sslmode' => 'prefer',
   ],
   

2. Use Supabase Client in Laravel: Install the Supabase PHP client library and use it in your Laravel application to interact with the Supabase database:

bash
   composer require supabase/supabase-php
   

php
   use Supabase\Client;

   $supabase = new Client([
       'supabaseUrl' => 'https://your-supabase-url.supabase.co',
       'supabaseKey' => 'your-supabase-anon-key',
   ]);

   $data = $supabase->from('profiles')->select('*')->execute();
   

The Supabase client will automatically respect the RLS policies you've set up in Supabase.

3. Authenticate Users: If you're using Supabase's authentication features, you'll need to integrate that with your Laravel application. You can use the Supabase client to handle user authentication and then use the authenticated user's ID to apply the appropriate RLS policies.

By following these steps, you can set up row-level security in Supabase and seamlessly integrate it with your Laravel application. This allows you to have fine-grained control over data access and security within your application.

Citations:
[1] https://supabase.com/docs/guides/database/postgres/row-level-security
[2] https://www.youtube.com/watch?v=dg1suKt-7D4
[3] https://www.reddit.com/r/sveltejs/comments/utlbua/sveltekit_endpoints_and_supabase_with_rls/
[4] https://ahmadrosid.com/blog/how-to-use-supabase-with-laravel
[5] https://stackoverflow.com/questions/77280009/how-to-use-supabase-rls-with-in-and-select-without-having-to-provide-unrestr