laravel 尝试获取非对象错误的属性“id”

我正在处理另一个人的代码,当我部署 laravel 应用程序时,登录页面可以工作,但是当我输入测试凭据时,它会抛出此错误


    Trying to get property 'id' of non-object

    in helpers.php line 159

    at HandleExceptions->handleError(8, 'Trying to get property \'id\' of non-object', '/var/www/html/first-project/app/Helpers/helpers.php', 159, array('fields' => object(Collection), 'fieldsValues' => object(Collection), 'htmlFields' => array(), 'startSeparator' => '<div style="flex: 50%;max-width: 50%;padding: 0 4px;" class="column">', 'endSeparator' => '</div>', 'field' => object(CustomField), 'dynamicVars' => array('$RANDOM_VARIABLE$' => 'var15931958241638660037ble', '$FIELD_NAME$' => 'phone', '$DISABLED$' => '', '$REQUIRED$' => '"required" => "required",', '$MODEL_NAME_SNAKE$' => 'user', '$FIELD_VALUE$' => '\'+136 226 5660\'', '$INPUT_ARR_SELECTED$' => '+136 226 5660'), 'gf' => object(GeneratorField), 'value' => object(CustomFieldValue)))

    in helpers.php line 159

实际引用的函数如下


function generateCustomField($fields, $fieldsValues = null)

{

    $htmlFields = [];

    $startSeparator = '<div style="flex: 50%;max-width: 50%;padding: 0 4px;" class="column">';

    $endSeparator = '</div>';

    foreach ($fields as $field) {

        $dynamicVars = [

            '$RANDOM_VARIABLE$' => 'var' . time() . rand() . 'ble',

            '$FIELD_NAME$' => $field->name,

            '$DISABLED$' => $field->disabled === true ? '"disabled" => "disabled",' : '',

            '$REQUIRED$' => $field->required === true ? '"required" => "required",' : '',

            '$MODEL_NAME_SNAKE$' => getOnlyClassName($field->custom_field_model),

            '$FIELD_VALUE$' => 'null',

            '$INPUT_ARR_SELECTED$' => '[]',


        ];



它在控制器中的用法如下。它在几乎所有控制器中多次使用,例如在 UserController.php 文件中,我认为这是调用方法。我不太熟悉 Laravel,对于任何菜鸟错误提前表示歉意。

米脂
浏览 162回答 2
2回答

守着一只汪

你必须改变这一行从$user&nbsp;=&nbsp;$this->userRepository->findWithoutFail(auth()->id());到$user&nbsp;=&nbsp;$this->userRepository->findWithoutFail(auth()->user()->id());

Smart猫小萌

我的第一个猜测,将其更改$user = $this->userRepository->findWithoutFail(auth()->id());为$user = $this->userRepository->findWithoutFail(auth()->user()->id);添加auth()->user()->id;
打开App,查看更多内容
随时随地看视频慕课网APP