namespace BASE\Livewire\Settings\Users;
use Illuminate\Support\Facades\Auth;
use Livewire\WithPagination;
use Livewire\Component;
use BASE\Tenant;
use BASE\Lbzuser;
class Show extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $Tenants;
public function mount() {
$this->Tenants = \BASE\Tenant::all();
$this->Users = \BASE\Lbzuser::orderBy('id', 'desc')->paginate(10);
}
public function impersonate($userId) {
//$originalId = Auth::user()->id;
//$originalId = auth()->user()->id;
dd(Auth::user());
session()->put('impersonate', $originalId);
//auth()->guard('lbenz')->loginUsingId($userId);
//return redirect('/_erp');
}
public function render()
{
return view('base::livewire.settings.users.show', ['Users' => $this->Users]);
}
}
Livewire component's [b-a-s-e.livewire.settings.users.show] public property [Users] must be of type: [numeric, string, array, null, or boolean]. Only protected or private properties can be set as other types because JavaScript doesn't need to access them.
and thats correct because i passin the blade tform an controller he $Users as an collection ;)
You cannot have a public property of a model collection
Get the models you need in the render method.
namespace BASE\Livewire\Settings\Users;
use Illuminate\Support\Facades\Auth;
use Livewire\WithPagination;
use Livewire\Component;
use BASE\Tenant;
use BASE\Lbzuser;
class Show extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public function impersonate($userId)
{
session()->put('impersonate', request()->user()->id);
}
public function render()
{
return view('base::livewire.settings.users.show')
->withUsers(\BASE\Lbzuser::orderBy('id', 'desc')->paginate(10));
}
}
ps, save Titlecase for class names, eg $users not $Users
For impersonating users, best approach is to store the id of the user you want to impersonate, not the id of the person who is logged in.
Then you override the logged in user with the impersonated user via middleware and then when done, clear the impersonation from session. With that in mind, I would change to;
session()->put('impersonate', $userId);
middleware;
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Impersonate
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(session()->has('impersonate')){
Auth::onceUsingID(session('impersonate'));
}
return $next($request);
}
}