Undefined variable: errors in Laravel - laravel

When I want to register a user in my laravel project, the page always says
Undefined variable: errors (View: /var/www/resources/views/auth/register.blade.php)"
According to the Laravel documentation, $errors should always automatically be set:
So, it is important to note that an $errors variable will always be available in all of your views on every request, allowing you to conveniently assume the $errors variable is always defined and can be safely used.
I have this on on every view when I use:
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
or any other way when I want to use the $errors variable.
Why is this? I never had this problem before.
Can someone help me please?

You should make sure that in app/Http/Kernel.php in middlewareGroups property for web you have:
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
in this array. Compare this with https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php
EDIT
It seems you need to add 'middleware' => 'web' for route you are using or put \Illuminate\View\Middleware\ShareErrorsFromSession::class, into $middleware property array
or
Inside of the routes.php file try to create your routes within the following block
Route::group(['middleware' => ['web']], function () {
//routes here
});
UPDATE FOR NEWER VERSIONS OF LARAVEL APPLICATION
Be aware that you might run into problems also in case you use web middleware twice. There was a change in Laravel application 5.2.27 (don't confuse it with Laravel framework you use at the moment - you might use Laravel framework for example 5.2.31 but have Laravel application in version 5.2.24) in which web middleware is applied automatically for all routes. So in case of problems, you should open your app/Providers/RouteServiceProvider.php file and verify its content.
You can compare it also here :
RouteServiceProvider for Laravel application 5.2.24
RouteServiceProvider for Laravel application 5.2.27
In case you have newer version (that applies web middleware automatically), you shouldn't use web middleware in routes.php anymore or you should modify your RouteServiceProvider method to not apply web group middleware. Otherwise if web middleware group is automatically applied in this provider and you use it also in routes.php you might get very unexpected results.

I had this very same issue with Laravel 5.2.x.
Inside of the routes.php file try yo create your routes within the
Route::group(['middleware' => ['web']], function () {
//routes here
}
statement.

Also to be aware of: If you write tests and your view has $errors variable make sure you don't use WithoutMiddleware trait.

I had similar problem and solved this one by adding routes into middleware property array as well,
BUT
it worked only after calling php artisan route:cache (clearing route cache) subsequently.
I hope some of you would find this useful.

I was seeing this error too and later realised I had used the WithoutMiddleware trait as a means to bypass authentication for this particular test, but it ended up removing the validation error binding too. So I had to stop using the trait to keep the views working.

Go to App\Http\Kernel.php file. Move all the things of $middlewareGroups properties to $middleware.
Check for more details-
http://www.tisuchi.com/laravel-5-2-undefined-variable-error-validation/

count is not really realiable since it assumes the variable already exists. change the condition check to: #if($errors->has()) or just #if($errors)
Also if you are redirecting make sure to use this in your controller
return redirect()->back()->with('errors', $validator->messages());
EDIT: seen now that you are using L5.2
This may answer your question - you need to put your Routes in Route group.
Laravel 5.2 validation errors

protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Social\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Social\Http\Middleware\VerifyCsrfToken::class,
];
/**
* The application's route middleware groups.
*
* #var array
*/
protected $middlewareGroups = [
'web' => [
],
'api' => [
'throttle:60,1',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
make your kernel look like this

Your problem will be fixed by using this method.
Route::group(['middleware' => ['web']], function () {
//routes should go here
});
If this doesn't help you, just run the following artisan command in addition to the above code:
php artisan key:generate
I solved in this way while using 5.2.*

Related

Two separate Inertia.js applications on one Laravel

Hello Masters of the Javascript and Laravel.
I'm wondering if it's possible to make two separate inertia.js applications in one Laravel application.
I'm making a SaaS style application, where there will be a front facing part and a backoffice part.
This will be split in two domains (or more), using the same Laravel/Octane application. This works.
However, the Ziggy routes from the backoffice also show up in the front facing application.
There's of course a security risk that these are included, in the Ziggy data. Also transferring the routes from the admin part creates unnecessary traffic, since the routes are transferred with every request with inertia. The backoffice will have a lot more routes than the relatively simple frontend.
Any idea on how this could be structured so the Ziggy data is split in two, while it still being one application?
I dont think this would be possible or if it is, it would be too complex setup since Interia relies with Laravel routing and when you are passing Laravel back to interia you'd have to determine which of that Inertia App you'd push the data.
I think the better approach for this is to just create domain group routes if you need multiple domains support, then create a persistent layout in the front-end which you can use for public/front-end area and admin area. You can just use Laravel Jetstream with Vue+Inertia.
I dont really see any reason why you would need different front-end application, but if you really do, I think you better just set-up Laravel for API routes so you can set-up different application for the front-end.
I ended up doing this in HandleInertiaRequests middleware
/**
* Define the props that are shared by default.
*
* #return array<string, mixed>
*/
public function share(Request $request): array
{
$ziggyObj = (new Ziggy)->toArray();
if($request->host()!=config('app.admin_domain')) {
$ziggyObj['routes'] = array_filter(
$ziggyObj['routes'],
fn ($key) => !str_starts_with($key, 'admin'),
ARRAY_FILTER_USE_KEY
);
}
return array_merge(parent::share($request), [
'auth' => [
'user' => $request->user(),
],
'ziggy' => function () use ($ziggyObj, $request) {
return array_merge($ziggyObj, [
'location' => $request->url(),
]);
},
]);
}
So any routes that starts with "admin" are not sent to the frontend, unless it's the "admin domain".
The front-facing application routes are also sent to the backoffice, but I might filter those out later.

How to write laravel 5.8 controllers routes similar to laravel 5.1

Having more than 20 controllers. It's very difficult to set each and every routes for add, edit and delete (also having more actions).
This is my laravel 5.1 routes.php :
Route::controllers([
'user' => 'UserController',
'taxes' => 'TaxController',
]);
Is there any way to support these routes in laravel 5.8?
You can use the Resource Controller and implement in routes/web.php. It will autogenerate the name for the route
//web.php
Route::resource('user', 'UserController');
Route::resource('taxes', 'TaxController');
Edit 1
If you want to exclude show method of the controller for the resource, You can add array inside the except method.
Route::resource('taxes', 'TaxController', [
'except' => ['show']
]);
Further, if you want to get only selected options, You can use only.
Route::resource('taxes', 'TaxController', [
'only' => ['index', 'create', 'store', 'edit']
]);
The controllers method was deprecated in Laravel 5.2. From the upgrade guide:
Implicit controller routes using Route::controller have been deprecated. Please use explicit route registration in your routes file.
1) Use Resource Routes
Provided that your controllers use the standard index, store, show etc methods you can simply use resource routes. For example:
Route::resource('user', 'UserController');
However if you want to exclude certain methods you can add them to the resource. For example:
Route::resource('user', 'UserController', ['except' => 'show']);
2) Declare Routes Explicitly
You can follow the Laravel 5.2 upgrade guide as above and instead declare each route explicitly.
3) Create a Macro
The Laravel router is Macroable. This means that you can add your own methods to it. For example, in your app service provider you could have the following:
Illuminate\Routing\Router::macro('controllers', function ($routes) {
// Create your own implementation of the controllers method.
});
This allows you to create your own implementation of the controllers method which means you wouldn't need to alter your routes or controllers, but you may need to dive in and look at Laravel's route handling to understand how to implement this.
I hope this helps.
You can use in the array, In as you call using routes. like {{route('claimsubmit')}}
Route::resource('claimform',array('as'=>'claimform','uses'=>'UserController#claimform');

Multiple middlewares for routes in Laravel. Array or multiple arguments?

I am building a website that, in order to check some of the content, you need to be authenticated AND verified.
When I enclose the middlewares in an array, it works.
Route::resource('premiumContent', 'PremiumContentController')
->middleware(['auth', 'verified']);
However, when I follow the original documentation,
https://laravel.com/docs/5.8/middleware#registering-middleware
grouping the middlewares passing them as multiple parameters, it does not work.
Route::resource('premiumContent', 'PremiumContentController')
->middleware('auth', 'verified');
Only the 'auth' middlewares applies, thus letting me access the content even if I am not verified. Why? What is the right approach?
Change the web.php code.This worked for me.
Route::group(['middleware' => ['auth', 'verified']], function() {
Route::resource('premiumContent','PremiumContentController');
});

How does Laravel 5.4 Middleware work?

I've read manual of Laravel 5 but still don't find a solution for my problem.
My task: I need to run multiple websites on the one codebase.
What have I done for it? I made one main website and create table "sites" where I can add new satellites. It's all worked fine. But now, in front end part, I need to create middleware which will help me to divorce different parts of my engine.
I start to create middleware but I've only found 2 ways of middleware:
redirect to the correct URL
return $next($request) which means that this middleware will be always working.
I don't need a redirect. I just need one thing:
I need that in my web.php route file I can make routes with my middleware group:
For example:
Route::group(['middleware' => 'site'], function () {
//there will be only routes for my satellites
And
Route::group(['middleware' => 'web'], function () {
//There will be another routes which won't be on satellites
The problem is if I make a redirect on the main site my middleware still working and going into the cycle.
Is there any way to break middleware and don't going into the certain routes?
I've tried with HttpException but I've received only 404 or 500 errors, but I don't need it in this case.

Laravel 5 - Assign Middleware to Controller Namespace

I need to do an ACL check for the user before allowing access to the admin panel of a Laravel 5 website. What is the best way to do this for an entire controller group in the namespace App\Http\Controllers\Admin\*? Ultimately, I'm looking for a "set and forget" method to do this, and middleware looks like the best option so far.
The initial idea was to assign a middleware to the admin route, but this does not prevent any other non-admin route from accessing the controllers. This means a route can still target the admin controllers and bypass the ACL check.
The next idea was to insert the assignment in the constructor on the controllers, but this would require each additional controller to explicitly include the middleware. This would require a developer to know that the middleware should be included, which allows them to miss it entirely. This also applies to using one base controller as the parent for all admin controllers, since the developer would need to know that the base controller should be extended. Right now, this looks like the best solution.
This leads us back to the question: can middleware be assigned to a controller wildcard namespace like App\Http\Controllers\Admin\*? Or, is there a better way for the ACL check to never need to be explicitly assigned to every admin controller?
This leads us back to the question: can middleware be assigned to a controller wildcard namespace like App\Http\Controllers\Admin*?
No
The most simplest approach you can do is create a base controller such as App\Http\Controllers\Admin\Controller and include the middleware
while all other App\Http\Controllers\Admin\* extends it.
Alternatively, while still adding App\Http\Controllers\Admin\Controller, you could instead inject the middleware through IoC Container.
App::afterResolving('App\Http\Controllers\Admin\Controller', function ($controller) {
$controller->middleware('acl');
});
EDIT
My previous answer didn't quite work for all situations; it broke a lot of other routes. Here's what I ended up doing instead (in app/Http/routes.php):
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'App\Http\Middleware\Acl'], function()
{
Route::get('/', 'AdminController#index');
Route::get('/user', 'User\UserController#index');
...
});
This at least targets all admin controllers when I define a route. It's not exactly everything I had hope for, but it will do.

Resources