mafortis
mafortis

Reputation: 7128

check user status in laravel vue

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

Upvotes: 0

Views: 1220

Answers (1)

Cloud Soh Jun Fu
Cloud Soh Jun Fu

Reputation: 1502

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;
    });
}

Upvotes: 1

Related Questions