How to handle if does not have a "current" tenant? - laravel

I'm using Spatie laravel-multitenancy. laravel v8
I'm not defined domain one.localhost and now I'm getting an error. so how to handle the error.
error is
Spatie\Multitenancy\Exceptions\NoCurrentTenant The request expected a
current tenant but none was set. http://one.localhost:8000/
web.php
Route::domain('{tenant}.localhost')->middleware('tenant')->group(function(){
Route::get('/', function ($tenant) {
return $tenant;
// return view('welcome');
});
});
Route::domain('localhost')->group(function(){
Route::get('/', function () {
return view('welcome');
});
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');})->name('dashboard');

Hi I am facing the same problem and after Debugging I found that I missed domain in tenants table
please add "one.localhost" in tenants table and everthing will be fine
if you don't have tenants table please run
php artisan vendor:publish --provider="Spatie\Multitenancy\MultitenancyServiceProvider" --tag="multitenancy-migrations"
php artisan migrate --path=database/migrations/landlord
Actually this package also provide Exception Handling if tenant did not exist. You can study more in these links
https://spatie.be/docs/laravel-multitenancy/v2/basic-usage/automatically-determining-the-current-tenant
https://spatie.be/docs/laravel-multitenancy/v2/advanced-usage/ensuring-a-current-tenant-has-been-set

Hi i get same error and i solve it by add the handle exception for NoCurrentTenant
got to Handler.php and try this register function:
$this->reportable(function (NoCurrentTenant $e) {
abort(404);
});

Related

Laravel 8 working with subdomain routing but its not working, I'm little confused about this

I'm working on a project where for each user there will be separate subdomain for example my website name is xyz.com then for user it will be user.xyz.com, for that im trying to do subdomain routing but its not working.
Below are the routes for main domain
Route::get('/', function () {
return redirect('login');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Below are routes for subdomain
Route::domain('user.xyz.com')->group(function () {
Route::get('/posts', function () {
return 'Second subdomain landing page';
});
});
Any expert please look into this.
Suggestions are welcome.
I have a suggestion... why not pass the domain as a parameter the way you would an unknown, article id for example /article/{id}?
Try this:
Route::domain('{user}.xyz.com')->group(function () {
Route::get('/posts', [PostsController::class, 'view'])->name('posts');
});
In our PostsController output the results...
class PostsController {
public function view ($user){
dd($user) //this will output the current user's subdomain name
}
}
Let me know if it works out for you.
Try to add a domain line in RouteServiceProvider.
Route::middleware("web")->domain('Domain Name')->namespace($this->namespace)->group(base_path("routes/web.php"));

Laravel - new guard issue

I am using Laravel/Fortify to manage my authentication. I have created a new guard to allow employees to log in.
Employee credentials now work but whenever I tried and retrieve the authenticated employees details via {{ Auth::user() }} it returns null.
Any ideas?
composer require laravel/fortify
php artisan vendor:publish --provider="Laravel\\Fortify\\FortifyServiceProvider"
php artisan migrate
Open config/app.php and register Fortify service provider:
App\Providers\FortifyServiceProvider::class,
Next, open config/fortify.php and update your features array as follow:
'features' => [
Features::registration(),
Features::resetPasswords(),
],
Now we need to tell Fortify where is our auth views.
Open app/Providers/FortifyServiceProvider.php and in the boot method add:
Fortify::loginView(function () {
return view('auth.login');
});
Fortify::registerView(function () {
return view('auth.register');
});
Fortify::requestPasswordResetLinkView(function () {
return view('auth.forgot-password');
});
Fortify::resetPasswordView(function () {
return view('auth.reset-password');
});
Protect your pages
Now we need to protect our routes, open routes/web.php and use auth middleware, like:
Route::get('/', function () {
return view('welcome');
})->middleware(['auth']);

Laravel any route any doesnt get executed

I have tried the following in my laravel web routes
Route::get('/{any}', function () {
return view('welcome');
})->where('any', '.*');;
But the above doesnt get called. I expect whenever i try calling any route not specified to use the above but it doesnt work.
I have checked on This but still the issue persists. What could be the issue
I have also tried adding it like
///other routes above
Route::get('/{any}', function () {
return view('welcome');
})->where('any', '*');
But still doesnt work.
You need to use regular expressions in your where condition.
https://laravel.com/docs/8.x/routing#parameters-regular-expression-constraints
In your case this should work:
Route::get('/{any}', function () {
return view('welcome');
})->whereAlphaNumeric('any');
this is equivalent to:
->where('any', '[a-zA-Z0-9]+');

Laravel 5.5 all routes except home result in 404 not found

Just trying out laravel 5.5 which I installed in cpanel. Only the root route is working:
Route::get('/', function () {
return view('public/content/home-content');
});
Even this isn't working
Route::get('/about', function () {
return view('public/content/about-content');
});
It also work:
www.mydomain/index.php/about
How can I solve this error

laravel Unable to prepare route ... for serialization. Uses Closure

When I clear caches in my Laravel 5.2 project, I see this error message:
[LogicException]
Unable to prepare route [panel] for serialization. Uses Closure.
I think that it's related with a route
Route::get('/article/{slug}', 'Front#slug');
associated with a particular method in my controller:
public function slug($slug) {
$article = Article::where('slug',$slug)->first();
$id = $article ->id_article ;
if ( ($article=== null) || (is_null($id)) ) return view('errors/Db');
else return view('detail')->with(array('article'=> $article, 'title'=>'My title - '.$article->title));
}`
In short, from a master view I pass $slug, that is a shortlink to the article, with $slug , which is unique in the database, I identify the record and then I pass it's contents to the detail view.
I didn't have any problem when I wrote the method, infact it worked like a charm, but after I cleaned caches, I get that error and the links in the master view don't show any shortcode.
Where am I doing wrong?
I think that it's related with a route
Route::get('/article/{slug}', 'Front#slug');
associated with a particular method in my controller:
No, thats not it. The error message is coming from the route:cache command, not sure why clearing the cache calls this automatically.
The problem is a route which uses a Closure instead of a controller, which looks something like this:
// Thats the Closure
// v
Route::get('/some/route', function() {
return 'Hello World';
});
Since Closures can not be serialized, you can not cache your routes when you have routes which use closures.
If none of your routes contain closures, but you are still getting this error, please check
routes/api.php
Laravel has a default auth api route in the above file.
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
which can be commented or replaced with a call to controller method if required.
This is definitely a bug.Laravel offers predefined code in routes/api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
which is unabled to be processed by:
php artisan route:cache
This definitely should be fixed by Laravel team.(check the link),
simply if you want to fix it you should replace routes\api.php code with some thing like :
Route::middleware('auth:api')->get('/user', 'UserController#AuthRouteAPI');
and in UserController put this method:
public function AuthRouteAPI(Request $request){
return $request->user();
}
The Actual solution of this problem is changing first line in web.php
Just replace Welcome route with following route
Route::view('/', 'welcome');
If still getting same error than you probab
the solustion when we use routes like this:
Route::get('/', function () {
return view('welcome');
});
laravel call them Closure so you cant optimize routes uses as Closures you must route to controller to use php artisan optimize
Check your routes/web.php and routes/api.php
Laravel comes with default route closure in routes/web.php:
Route::get('/', function () {
return view('welcome');
});
and routes/api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
if you remove that then try again to clear route cache.
If you're coming to this problem because you've upgraded Laravel <5.8 project up to >=5.8, you've likely used the ./vendor/bin/carbon-upgrade method to upgrade the project as suggested by your terminal. In this case, you simply need to remove the following two blocks from the bottom of your composer.json file and composer install again:
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
],
If someone is still looking for an answer, for me the problem was in routes/web.php file. Example:
Route::get('/', function () {
return view('welcome');
});
It is also Route, so yeah...Just remove it if not needed and you are good to go!
You should also follow answers provided from above.
In order to troubleshoot this (at least in laravel 6):
The action property inside Route.php has all the info needed. A better error message should be possible to provide by laravel.
What I did was to add a dd($this->action) just before the exception is thrown here:
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Routing/Route.php#L917
With that in place I could easily pinpoint the location, in my case api.php and lines 22-24:
array:6 [
"middleware" => "api"
"domain" => "local-api.mydomain.com"
"uses" => Closure()^ {#6497
class: "App\Providers\RouteServiceProvider"
this: App\Providers\RouteServiceProvider {#5743 …}
file: "./routes/api.php"
line: "22 to 24"
}
"namespace" => "App\Http\Controllers"
"prefix" => null
"where" => []
]
This is how I solved mine.
Navigate to routes directory
Then open api.php
comment code that looks like this:
Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); });
Then run
php artisan optimize
check that your web.php file has this extension
use Illuminate\Support\Facades\Route;
my problem gone fixed by this way.

Resources