How to get online users and their count? - laravel

I want to use this package in my Laravel 8 projects for retrieving online users. However, I am lost. I follow the documentation but I don't understand the steps. When I login through another browser, it keeps giving me 0 users. What did I do wrong?
User.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laratrust\Traits\LaratrustUserTrait;
use Shetabit\Visitor\Traits\Visitor;
class User extends Authenticatable
{
use Visitor;
use LaratrustUserTrait;
use HasFactory, Notifiable;
protected $guarded = [];
}
DashboardController.php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\User;
class DashboardController extends Controller
{
public function index(User $user)
{
$onlineUsers = $user->visits()->count();
return view('dashboard.index', compact('onlineUsers'));
}
}

visitor()->onlineVisitors(User::class); // returns collection of online users
User::online()->get(); // another way

Related

Error when using Spate Media Library: "BadMethodCallException Call to undefined method Illuminate\Foundation\Auth\User::addMediaFromRequest()"

This is my User Model
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\File;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements HasMedia
{
use HasMediaTrait;
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function role(){
return $this->belongsToMany('App/Role');
}
}
This is my UserController
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
I want to use Spatie Media Library and upload a photo for each user but I get this error related to Spatie Library:
"Call to undefined method
Illuminate\Foundation\Auth\User::addMediaFromRequest()".
I have read some related posts but I don't understand how to fix this.
Thank you.
Version 8 of the library has this documentation. Are you using the correct trait?
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class YourModel extends Model implements HasMedia
{
use InteractsWithMedia;
}
Edited:
On another note, using HasMediaTrait is for version 7 so I assume you're using version 7. I think it's because you're importing the wrong User class. The User class used to implement HasMedia lies in the App\User namespace. But you're importing the User class from the Illuminate\Foundation\Auth\User namespace. So change it to use App\User; and you should be fine.
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function store(Request $request)
{
$user=new User;
$user->name=($request['name']);
$user->email=($request['email']);
$password=bcrypt($request['password']);
$user->password=$password;
$user_photo=$request['photo'];
$user->addMediaFromRequest('photo')->toMediaCollection('images');
$user->save();
return redirect('/admin');
}
}
https://docs.spatie.be/laravel-medialibrary/v8/basic-usage/preparing-your-model/

Eloquent query doesn't use the where/first conditions. Why?

Ma simple eloquent query doesn't use the where condition.
Players::where('id_player',"=" ,3113)->first()->toSql();
Returns:
"select * from players"
My Controller:
namespace App\Http\Controllers\Office;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Players;
class OfficeController extends Controller{
public function playerLineup(Request $request){
$e = Players::where('id_player',"=" ,3113)->first()->toSql();
dd($e);
}
}
My Modal:
namespace App;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Players extends Model{
protected $primaryKey = 'idp';
}
Once you run ->first() the result is a fresh instance of the Player model. then ->toSql() on a fresh model is like Player::query()->toSql().
you need to call ->toSql() before that:
Players::where('id_player',"=" ,3113)->toSql();
or
Players::where('id_player',"=" ,3113)->limit(1)->toSql();
/try to do this/
namespace App\Http\Controllers\Office;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Players;
use DB;
class OfficeController extends Controller{
public function playerLineup(Request $request){
$e = DB::table('Players')->where('id_player',"=" ,3113)->get()->first();
dd($e);
}
}

laravel model class not found

I'm getting class model not found error
In my model class
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $table = 'customers';
}
And in my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \app\Models\Customer;
use Illuminate\Support\Facades\Input;
class RoomsController extends Controller
{
public function index()
{
$room= Customer::all();
return view('rooms',compact('room'));
}
}
I even tried composer dump-autoload.
I'm still getting the same error
Use use in head of your controller
use App\models\Customer;
Than you can call it in your function:
Customer::find($id);//etc
use App\Customer;
Use this in head of the controller

"User Model" functions in Laravel 5 don't work

I migrate from laravel 4 to laravel 5. I modified User Model to make it compatible to laravel 5 User Model. The User model in Laravel 4 is like that:
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
// some model relation functions..
...
public function getLocationId()
{
return $this->location_id;
}
}
I modified it and the new User model is like that now:
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
// some model relation functions (some changes for namespace)
...
public function getLocationId()
{
return $this->location_id;
}
}
However, getLocationId method is now problematic. The following line results in an error.
$auth_location_id = Auth::user()->getLocationId();
This is the error:
BadMethodCallException in Builder.php line 2508:
Call to undefined method Illuminate\Database\Query\Builder::getLocationId()
How can I make the method compatible to laravel 5? Thanks..
Update:
$auth_location_id = Auth::user()->getLocationId; works but returns empty!!
The line below works properly.
$auth_user_id = Auth::user()->getAuthIdentifier();
By my understanding, and short experience with Laravel, all I do is just to use:
use Illuminate\Foundation\Auth\User as Authenticatable;
Then have my Model as:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function getLocationId()
{
return $this->location_id;
}
}
So I may be safe to assume, at least because it works for me that all those contracts has be extended in the facade called when ....\User is used.
PS: this is just an illustration of how I currently use it in Laravel 5.2.*.
Hope this can help or lead you to the solution :)

Class App\User contains 6 abstract methods and must therefore be declared abstract

I saw this error for the first time and don't really know what to do about it.
When I tried to register a new user on my website and when I clicked on submit button it shows:
FatalErrorException in User.php line 11:
Class App\User contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifierName, Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifier, Illuminate\Contracts\Auth\Authenticatable::getAuthPassword, ...)
User Model:
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
protected $table = 'users';
protected $primaryKey = 'id';
}
What is it trying to say, I don't understand. Please can someone help me with this?
You are implementing Illuminate\Contracts\Auth\Authenticatable. This is interface and requires your User class to have some required methods:
public function getAuthIdentifierName();
public function getAuthIdentifier();
public function getAuthPassword();
public function getRememberToken();
public function setRememberToken($value);
public function getRememberTokenName();
If you are trying to make default User model, you should use Illuminate\Foundation\Auth\User as Authenticatable and extend it instead of Model class. There is no need to implement Authenticatable interfase.
You need to either extend Illuminate\Foundation\Auth\User instead of Illuminate\Database\Eloquent\Model, or use Illuminate\Auth\Authenticatable trait in your class
UPDATE
You need to extend Illuminate\Foundation\Auth\User like this
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
}
UPDATE 2
Also make sure you don't have native Laravel's App\User model in you app folder, named User.php

Resources