View share only for sharing data berween blade file not in the controller.
Different variables behaviour in localhost vs deployed
In my application I need to make globally available four variables:
$user, $employee, $role and $company
In order to show different menu options and landing views after login.
I do this in my Controller.php
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct()
{
$this->middleware(function ($request, $next) {
if (Auth::user()) {
$user = Auth::user();
$employee = $user->employee;
$role = $user->role->slug;
$company = $user->company;
view()->share('signed_in', Auth::check());
view()->share('user', $user);
view()->share('employee', $employee);
view()->share('role', $role);
view()->share('company', $company);
}
return $next($request);
});
}
}
This work as expected in my development local enviroment (Localhost).
Now I deployed my app (first time I deploy a Laravel app) in CloudWays and it does ont work as expected.
It turns out that Auth::user() is empty (not defined and therefore it breaks the app.
I double checked the login info (email and password) and they are right.
It is the same code, that I pushed to git and deployed from there.
Any idea, why Auth::user() is never defined? What Am I missing?
Can you add something to check if the user is logged in? add a
@dump(Auth()->user()->name ?? 'not logged in')
somewhere prominent in your master layout.
By the way, adding your code to every controller is not the correct approach. You should consider a view composer for information shared with every view.
Please or to participate in this conversation.