check user status in laravel vue - laravel

I have middleware where i check user status if is online or offline and it's working if i use blade, but since i'm using vue.js components i don't know how i can pass that to component.
in blade i can use:
#if($user->isOnline)
Online
#else
Offline
#endif
this is my user model:
public function isOnline()
{
return Cache::has('user-is-online-' . $this->id);
}
now my question is How can I use isOnline in my components?
PS: if you need any code i share just let me know

If you used blade to get the variable, you can only get it on page load. If your vue is not written in the blade file then your vue can't get the variable in your blade.
I would suggest you to make an API call using axios or something to your Laravel during the created life cycle of your vue component to get the variable.
// Vue
axios.get('example.com/api/users').then((res) => {
this.users = res.data;
}
// Laravel
Route::get('/users', UserController#index);
// Controller
public function index() {
return User::all()->map(function ($user) {
$user->isOnline = $user->isOnline();
return $user;
});
}

Related

Laravel Page Route Not Using The Correct Parameters Passed In

Hey all you smart people,
Im having a issue I normally work with API routes not really used Web Routes before and finding this rather complicated for some reason :D
Ive made this route
Route::get('/test/{page?}', \App\Http\Livewire\Test::class);
sand this is my logic in the render() in the controller
public function render(Request $request, $page = 1)
{
dd($page);
}
however when I'm on the browser and type
http://url.com/test/2
The Die Dump keeps giving me page 1 all the time am i missing something here ??
Thanks for the help if anyone can help...
Update
Im not sure if its because I'm using a livewire component and not an actual controller....
Livewire Component
<?php
namespace App\Http\Livewire;
use Illuminate\Http\Request;
use Livewire\Component;
class Test extends Component
{
public function render(Request $request, $page = 1)
{
dd($page);
return view('livewire.test');
}
}
Route Parameters in livewire works like this
web.php
Route::get('/test/{page?}', \App\Http\Livewire\Test::class);
component
public function mount($page = 1)
{
dd($page);
}
ref link https://laravel-livewire.com/docs/2.x/rendering-components#route-params

How to pass Eloquent Data to blade component?

Hello,
I have a blade Layout called:
profile.blade.php // component
And I have a blade file infos which extends from profile.blade.php component.
On my controller, I have a profile method:
public method profile(User $user) {
return view('infos.blade.php', compact('user'));
}
When I try to use the variable user to the profile.blade.php component, I have an error that says "undefined user variable"
My question is how can I get the data I received from my controller to Blade Component profle ?
Because that part of component will be used many times.
Thanks.
in your app/view/components/profile-
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class profile extends Component
{
public $user; //for consistency
public function __construct($user)
{
$this->user = $user;
}
public function render()
{
return view('components.profile');
}
}
now the component can render a variable ($user),
in your info's blade/infos.blade.php you can feed the
variable to the component like --
<x-profile :user='$user' ></x-profile>
i understand the question was posted quite a long ago
but i had the same issue and the answers posted here
didn't work for me but this method did so.....it may help somebody.
As it's a component you have to pass user parameter each time you call it, for example
#component('profile', ['user' => $user]) #endcomponent // here $user comes from controller

I have problem when i logout from admin "Trying to get property 'id' of non-object" using laravel

I am trying to logout from admin but unfortunately, I face error Trying to get property 'id' of non-object How to fix this error? please help me thanks.
public function index(){
$user_permission = Users_Permissions::with('user')
->Where('user_id',Auth::user()->id)
->paginate(5);
return view('index',compact('user_permission'));
}
The problem is clear. When you log out, then Auth::user() is null. so there is no id . You can solve the issue like this.
public function index()
{
if (Auth::check()) {
$user_permission = Users_Permissions::with('user')->Where('user_id',Auth::user()->id)
->paginate(5);
return view('index',compact('user_permission'));
} else {
// The condition when no user logged in
// For an example
return redirect('login'); // This is just an example
}
For that, use try & catch in every function, and use auth middleware for specific routes while you are working with auth users.
so that specific routes are required to use auth, so you don't need to check in every function. auth is always present.
Route::middleware('auth')->group(function () {
Route::get('logout', 'AuthController#logout');
});
or else you can use middleware in a controller too,
class AuthController extends Controller
{
public function __construct(){
$this->middleware(['guest'])->except('logout');
}
}
It's because after logout you don't have user ID, so in this situation the User is null, and you want to get the if from null object. you can change this part of code like below
Users_Permissions::with('user')->Where('user_id',Auth::user()->id ?? 0)
then you pass the error. you can put every number or null in query based on your needs.

Why Auth::user() return null in routes of a custom Service Provider?

I'm making a new Service called Factures in App\Services\Factures.
I created the \App\Services\Factures\FacturesServiceProvider:
public function register() {
$this->app->bind('factures', function ($app) {
return new Facture;
});
}
public function boot() {
// laod Routes
$this->loadRoutesFrom(__DIR__ .'/Http/routes.php');
// load Views
$this->loadViewsFrom(__DIR__ . '/views', 'factures');
}
I registered my provider everything works fine expect the Auth::user() in returns me  null in the views and the routes.php.
How can I get access to the Auth() in custom service?
This post resolved my problem: User Auth not persisting within Laravel package
I figure out that Laravel  apply to the default routes/web.php file a middleware called 'web' And doesn't apply this group to external package routes loaded via service provider's.
So my routes in the custom file should be in web middleware:
Route::group(['middleware' => ['web']], function () {
Route::get('testing-services', function(){
dd(Auth::user());
// output is valid
});
});

Laravel how to execute specific function every time a specific controller's functions are used

I am running laravel and i just implemented PHP Excel in order to export excels with data. For security reasons i want to redirect the user if his facebook id is no match with "my administrator facebook id".
My controller is named ExcelController and it has different functions inside it.
I do not want to use the below code inside every function so i am trying to find something to execute like __construct() - everytime this controller is accessed :
if(Auth::user()->facebook_id != env('facebook_id_admin_access')) {
return redirect()->route('home');
}
env('facebook_id_admin_access') is an .env variable(like a constant) with the facebook id i want to give access so it can use the specific controller.
I tried creating a public function __construct(){} in the controller and put the above code block but it doesn't get called.
Is this possible and how? Should i use middleware for that?
EDITED QUESTION
1) Created a middleware CheckAdminAccess
*/
public function handle($request, Closure $next)
{
if(Auth::user()->facebook_id != env('FACEBOOK_ID_ADMIN_ACCESS') || !Auth::check()) {
return redirect()->route('/');
}
return $next($request);
}
2) Updated in app\http\kernel.php to
protected $routeMiddleware = [
'admin_access' => 'App\Http\Middleware\CheckAdminAccess',
];
3) Updated in routes.php :
Route::get('/output/completed', 'ExcelController#completed')->middleware('admin_access');
But it doesn't seem to work, did i forgot anything?
After creating a middleware,
To make it work i changed in my routes.php from :
Route::get('/output/completed', 'ExcelController#completed')->middleware('admin_access');
To :
Route::group(['middleware' => ['auth','admin_access']], function() {
Route::get('/output/completed', 'ExcelController#completed');
});

Resources