This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

phplee
phplee

Hi,

Im using the 'Plan' plugin which requires the Rainlab 'User' plug and also the 'Roles' plugin. Ive tried asking on the support page of the 'Plan' plugin but got no response.

However ive noticed that if i use Auth::getUser()->id it doesn't return anything. but Auth::getUser()->name does. the user id is there however if i test by dumping Auth::getUser().

Is there any reason why Auth::getUser()->id would not return the users ID.

Any help would be great

Last updated

DMeganoski
DMeganoski

Hmm that does sound strange... That function should return a standard eloquent model, in which case it uses a magic method to retrieve the value from the array of $model->attributes.

Try instead:


$user = Auth::getUser();
        if (!$user_id = $user->id) {
            $user_id = $user->attributes['id'];
        }

If that doesn't work... since you say dumping the object does... try


$user_array = $user->toArray();
$user_id = $user_array['id'];
phplee
phplee

Thanks DMeganoski for taking the time to help me. Unfortunetly non of your solutions worked. They work if i try and get anything other than 'id'.

if i $user->toArray(); and dump the array id is there but $user_array['id'] returns nothing.

Could it be protected? preventing it from being returned.

thanks

DMeganoski
DMeganoski

Hmm that is strange. As far as I understand php, if you cast the object to an array and dump it, and the key is there, it's there.

Once cast to array you are no longer working with an object, just an array, and there is no 'protection' for keys on an array.

You don't get a 'undefined index' error when calling $user_array['id'] ? That means the index does indeed exist. (by default, php does not throw an exception for undefined index, however laravel does)

I have had many instances where I was overlooking something simple, such as re-declaring the variable inside the loop so it gets overwritten every iteration, or trying to compare with a single '=' sign instead of double equals '==' (which instead overwrites the variable)

I.E.


$variable = 'not_test';
if($variable == 'test') {
// this will not execute
}
if($variable = 'test') {
// this will execute and now $variable = 'test';
}

I suspect something of this nature, though I could be wrong. Otherwise I'm baffled. Heh.

m.asli
m.asli

same problem. i have no idea why but this worked for me: $user_id = ''.Auth::getUser()->id;

bongvet198631773
bongvet198631773

Thanks DMeganoski for taking the time to help me. Unfortunetly non of your solutions worked. They work if i try and get anything other than 'id'.[.......]----------------()

tech.computeraid55120
tech.computeraid55120

What worked for me is retweeting the Auth or BackendAuth in my case from the controller basis my set permissions like so. Also, I needed to set readOnly to '1' or '0' while 'true' or 'false' didn't work for me -

public function formExtendFields($form)
{
    if (!$this->user->hasAnyAccess(['daf.erp.manage_employee_tickets'])) {
        $form->addFields([
            'backenduser_id' => [
                'label' => 'daf.erp::lang.ticket.backenduser',
                'emptyOption' => '-- Select --',
                'showSearch' => 'false',
                'span' => 'auto',
                'required' => '1',
                'readOnly' => '1',
                'default' => BackendAuth::getUser()->id,
                'type' => 'dropdown'
            ],
        ]);
    }else {
        $form->addFields([
            'backenduser_id' => [
                'label' => 'daf.erp::lang.ticket.backenduser',
                'emptyOption' => '-- Select --',
                'showSearch' => 'true',
                'span' => 'auto',
                'required' => '1',
                'readOnly' => '0',
                'default' => BackendAuth::getUser()->id,
                'type' => 'dropdown'
            ],
        ]);
    }

Last updated

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.