{"id":8281,"date":"2026-09-02T21:34:43","date_gmt":"2026-09-02T21:34:43","guid":{"rendered":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/"},"modified":"2026-09-02T21:34:43","modified_gmt":"2026-09-02T21:34:43","slug":"making-laravel-passport-tenant-aware","status":"publish","type":"post","link":"https:\/\/kutia.net\/fr\/making-laravel-passport-tenant-aware\/","title":{"rendered":"Sensibiliser les locataires de Laravel\/Passeport"},"content":{"rendered":"<p class=\"wp-block-paragraph\">There are many Laravel packages out there that help&nbsp;us make Laravel applications multi-tenant. Depending on the project, we chose&nbsp;<a href=\"https:\/\/github.com\/spatie\/laravel-multitenancy\">spatie\/laravel-multitenancy<\/a>. The package does almost everything for you, but if you are building API endpoints, you are probably going to use <a href=\"https:\/\/github.com\/laravel\/passport\">laravel\/passport<\/a>&nbsp;as an authentication package to secure your API endpoints. So, combining these two packages might be a little bit tricky. Let me show you why!<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-passport-migration-will-run-on-the-main-database\">Passport migration will run on the main database<\/h3>\n<p class=\"wp-block-paragraph\">Each tenant has its own users, and each user can authenticate to one tenant and should not access other tenant data. So we need our Passport migrations to run on each created tenant.&nbsp;&nbsp;<\/p>\n<p class=\"wp-block-paragraph\">To solve this,&nbsp;just run the command on your terminal<\/p>\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$ php artisan vendor:publish --tag=passport-migrations<\/code><\/pre>\n<p class=\"wp-block-paragraph\">so publish the migrations files and then move these files to your migrations tenant folder. This way, Passport migrations won&#8217;t run on the landlord database.<\/p>\n<h3 class=\"wp-block-heading\" id=\"h-passport-migration-will-run-automatically\">Passport migration will run automatically<\/h3>\n<p class=\"wp-block-paragraph\">Solving this is pretty easy, just add&nbsp;<code>Passport::ignoreMigrations();<\/code> &nbsp;to your AuthServiceProvider like shown in the image below.<\/p>\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"864\" src=\"https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.37.51-PM-1024x864.png\" alt=\"\" class=\"wp-image-3689\" srcset=\"https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.37.51-PM-1024x864.webp 1024w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.37.51-PM-300x253.webp 300w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.37.51-PM-768x648.webp 768w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.37.51-PM-1536x1296.webp 1536w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.37.51-PM-2048x1728.webp 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<h3 class=\"wp-block-heading\" id=\"h-passport-will-use-the-landlord-database-connection\">Passport will use the landlord database connection<\/h3>\n<p class=\"wp-block-paragraph\">By default, Laravel uses the default database connection as we call <em>the landlord database.<\/em> To force Passport use tenant connection, I&#8217;m going to create a task that will be performed when a tenant is detected and switched. But before creating this task, we should create few custom classes to extend default Passport classes. So, I&#8217;m using&nbsp;<code>UsesTenantConnection<\/code> trait on few Passport models as shown in the image below.<\/p>\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"763\" src=\"https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.55.43-PM-1024x763.png\" alt=\"\" class=\"wp-image-3691\" srcset=\"https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.55.43-PM-1024x763.webp 1024w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.55.43-PM-300x223.webp 300w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.55.43-PM-768x572.webp 768w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.55.43-PM-1536x1144.webp 1536w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-8.55.43-PM-2048x1526.webp 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p class=\"wp-block-paragraph\">After we told Passport models to use tenant connection, we have one think left which is implementing&nbsp;<code>Spatie\\Multitenancy\\Tasks\\SwitchTenantTask<\/code> interface.<\/p>\n<p class=\"wp-block-paragraph\">Now I&#8217;m going to create a class that implements that interface and force Passport to use our custom classes.<\/p>\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"763\" src=\"https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-9.00.57-PM-1024x763.png\" alt=\"\" class=\"wp-image-3693\" srcset=\"https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-9.00.57-PM-1024x763.webp 1024w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-9.00.57-PM-300x223.webp 300w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-9.00.57-PM-768x572.webp 768w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-9.00.57-PM-1536x1144.webp 1536w, https:\/\/kutia.net\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-03-03-at-9.00.57-PM-2048x1526.webp 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p class=\"wp-block-paragraph\">After creating the class, don&#8217;t forget to add it to the&nbsp;switch_tenant_tasks array at&nbsp;<code>config\/multitenancy.php&nbsp;<\/code><\/p>\n<h3 class=\"wp-block-heading\" id=\"h-the-user-php-eloquent-will-use-the-landlord-database-connection\">The User.php eloquent will use the landlord database connection<\/h3>\n<p class=\"wp-block-paragraph\">Depending on your situation, you can either tell the current User model to use the tenant connection by using the&nbsp;<code>UsesTenantConnection<\/code> trait, or create another custom model like <code>TenantUser.php.<\/code><br \/>&nbsp;<\/p>\n<p class=\"wp-block-paragraph\">If you completed all those steps, you are done. Just go on and generate those tokens ! \u200b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Il existe de nombreux packages Laravel qui nous aident \u00e0 rendre les applications Laravel multi-locataires. <\/p>\n","protected":false},"author":0,"featured_media":2984,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[82],"tags":[],"class_list":["post-8281","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-non-classifiee"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Making laravel\/passport tenant aware - Kutia<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Making laravel\/passport tenant aware - Kutia\" \/>\n<meta property=\"og:description\" content=\"Il existe de nombreux packages Laravel qui nous aident \u00e0 rendre les applications Laravel multi-locataires.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Kutia\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-02T21:34:43+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/\"},\"author\":{\"name\":\"\",\"@id\":\"\"},\"headline\":\"Sensibiliser les locataires de Laravel\\\/Passeport\",\"datePublished\":\"2026-09-02T21:34:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/\"},\"wordCount\":5,\"image\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/kutia.net\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/laravel_2-1.webp\",\"articleSection\":[\"Non classifi\u00e9(e)\"],\"inLanguage\":\"fr-FR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/\",\"url\":\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/\",\"name\":\"Making laravel\\\/passport tenant aware - Kutia\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/kutia.net\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/laravel_2-1.webp\",\"datePublished\":\"2026-09-02T21:34:43+00:00\",\"author\":{\"@id\":\"\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\\\/\\\/kutia.net\\\/making-laravel-passport-tenant-aware-2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/kutia.net\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/laravel_2-1.webp\",\"contentUrl\":\"https:\\\/\\\/kutia.net\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/laravel_2-1.webp\",\"width\":1200,\"height\":608,\"caption\":\"Making laravel\\\/passport tenant aware\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kutia.net\\\/#website\",\"url\":\"https:\\\/\\\/kutia.net\\\/\",\"name\":\"Kutia\",\"description\":\"Custom software, team augmentation, IT recruitment\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kutia.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Making laravel\/passport tenant aware - Kutia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/","og_locale":"fr_FR","og_type":"article","og_title":"Making laravel\/passport tenant aware - Kutia","og_description":"Il existe de nombreux packages Laravel qui nous aident \u00e0 rendre les applications Laravel multi-locataires.","og_url":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/","og_site_name":"Kutia","article_published_time":"2026-09-02T21:34:43+00:00","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/#article","isPartOf":{"@id":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/"},"author":{"name":"","@id":""},"headline":"Sensibiliser les locataires de Laravel\/Passeport","datePublished":"2026-09-02T21:34:43+00:00","mainEntityOfPage":{"@id":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/"},"wordCount":5,"image":{"@id":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/#primaryimage"},"thumbnailUrl":"https:\/\/kutia.net\/wp-content\/uploads\/2021\/03\/laravel_2-1.webp","articleSection":["Non classifi\u00e9(e)"],"inLanguage":"fr-FR"},{"@type":"WebPage","@id":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/","url":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/","name":"Making laravel\/passport tenant aware - Kutia","isPartOf":{"@id":"https:\/\/kutia.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/#primaryimage"},"image":{"@id":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/#primaryimage"},"thumbnailUrl":"https:\/\/kutia.net\/wp-content\/uploads\/2021\/03\/laravel_2-1.webp","datePublished":"2026-09-02T21:34:43+00:00","author":{"@id":""},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/"]}]},{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/kutia.net\/making-laravel-passport-tenant-aware-2\/#primaryimage","url":"https:\/\/kutia.net\/wp-content\/uploads\/2021\/03\/laravel_2-1.webp","contentUrl":"https:\/\/kutia.net\/wp-content\/uploads\/2021\/03\/laravel_2-1.webp","width":1200,"height":608,"caption":"Making laravel\/passport tenant aware"},{"@type":"WebSite","@id":"https:\/\/kutia.net\/#website","url":"https:\/\/kutia.net\/","name":"Kutia","description":"Custom software, team augmentation, IT recruitment","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kutia.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"}]}},"_links":{"self":[{"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/posts\/8281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/comments?post=8281"}],"version-history":[{"count":0,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/posts\/8281\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/media\/2984"}],"wp:attachment":[{"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/media?parent=8281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/categories?post=8281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kutia.net\/fr\/wp-json\/wp\/v2\/tags?post=8281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}