【问题标题】:Attempt to read property "investment_id" on array尝试读取数组上的属性“investment_id”
【发布时间】:2022-01-08 23:00:32
【问题描述】:

我知道有很多类似的问题,很遗憾我找不到适合我的情况的解决方案。 单击另一个列表中的链接时,我想使用 Laravel Livewire 显示模态数据列表。但是,由于 Livewire 无法将公共变量设置为 Eloquent 或 Query Builder 实例,所以我使用数组来检索列表,在这种情况下我使用 select() 方法,因为它返回数组。然后,列表确实显示正确,但几秒钟后,提示错误提示 Attempt to read property "investment_id" on array。有没有关于这个问题的解决方案?谢谢。

投资-modal.blade.php

@if ($investmentModalClicked == true)
<div id="investment-modal" wire:ignore.self class="modal fade">
    ....
    ....
    <table class="table table-sm table-striped">
        <thead>
            <th style="width: 5%;" scope="col">No.</th>
            <th style="width: 25%;" scope="col">Investment ID</th>
            <th style="width: 50%;" scope="col">Initial Investment</th>
            <th style="width: 20%;" scope="col">Status</th>
        </thead>
        <tbody>
            @foreach ($investmentList as $key => $item)
                <tr>
                    <th scope="row">{{ $key+1 }}</th>
                    <td>{{ $item->investment_id }}</td>
                    <td>USD {{ $item->initial_investment }}</td>
                    <td>{{ $item->status }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
</div>
@endif

投资列表.blade.php

<div>
<table class="table table-sm table-striped">
    <thead>
        <th style="width: 5%;" scope="col">No.</th>
        <th style="width: 15%;" scope="col">Name</th>
        ...
        ...
    <tbody>
        @foreach ($users as $key => $user)
            <tr>
                <th scope="row">{{ ($currentPage-1) * $rowPerPage + $key + 1 }}</th>
                <td><a class="link-primary" href wire:click.prevent="showInvestmentModal({{ $user->id}})">{{ $user->name}}</a></td>
                ...
                ...
            </tr>
        @endforeach
    </tbody>
</table>
</div>

投资清单.php

class InvestmentList extends Component
{
    public $currentPage = 1;
    public $investmentList;
    public $investmentModalClicked = false;

    public function render()
    {
        return view('livewire.partnership', [
            'users' => $this->retrieveUserList(),
            'rowPerPage' => 20
        ]);
    }

    public function retrieveUserList()
    {
        //returns array
    }

    public function showInvestmentModal($userId) 
    {
        $this->investmentPopupClicked = true;

        $this->investmentList = DB::select('select id, date, investment_id, initial_investment, status '
            . 'from investment_data '
            . 'where user_id = ? '
            . "and (status = 'ACTIVE' or status = 'INACTIVE')"
            . 'order by status asc', [$userId]);

        $this->emit('showInvestmentModal');
    }
}

$investmentList 的 var 转储结果

array:6 [▼
  0 => {#652 ▼
    +"id": 55
    +"date": "2021-12-02 18:48:52"
    +"investment_id": "31733685"
    +"initial_investment": "400.00"
    +"status": "ACTIVE"
  }
  1 => {#647 ▼
    +"id": 54
    +"date": "2021-12-02 18:48:50"
    +"investment_id": "31735260"
    +"initial_investment": "2400.00"
    +"status": "ACTIVE"
  }
  2 => {#646 ▼
    +"id": 53
    +"date": "2021-12-02 18:48:48"
    +"investment_id": "31733712"
    +"initial_investment": "3400.00"
    +"status": "ACTIVE"
  }
  3 => {#645 ▼
    +"id": 52
    +"date": "2021-12-02 18:48:45"
    +"investment_id": "31734980"
    +"initial_investment": "3000.00"
    +"status": "ACTIVE"
  }
  4 => {#644 ▼
    +"id": 51
    +"date": "2021-12-02 18:48:36"
    +"investment_id": "31733654"
    +"initial_investment": "12000.00"
    +"status": "ACTIVE"
  }
  5 => {#643 ▼
    +"id": 56
    +"date": "2021-11-30 13:39:45"
    +"investment_id": "31728693"
    +"initial_investment": "21.00"
    +"status": "ACTIVE"
  }
]

【问题讨论】:

  • 应该是$item['investment_id'] 而不是$item-&gt;investment_id
  • 或者可能是$item[0]-&gt;investment_id 而不是$item-&gt;investment_id
  • 两个都试过了,但我得到了不能使用 stdClass 类型的对象作为数组错误。无论如何,谢谢你的评论。 @OMiShah
  • 查看它包含哪些数据的最佳方法是转储变量并自己查看。无法帮助您提供的信息不足!
  • 啊,我忘了包括那个。我更新了问题。 @OMiShah

标签: php laravel laravel-livewire


【解决方案1】:

最后我自己想通了,通过render方法传递$investmentList,几秒后就不再提示这个错误了。是的,它不需要强制转换为数组,也不需要转换为集合。

public $userId;

public function render()
{
    return view('livewire.investment-list', [
        'users' => $this->retrieveUserList(),
        'rowPerPage' => 20,
        'investmentList' => $this->getInvestmentList()
    ]);
}

public function showInvestmentModal($userId) 
{
    $this->investmentPopupClicked = true;
    $this->userId= $userId;
    $this->emit('showInvestmentModal');
}

public function getInvestmentList()
{
    return DB::select('select id, date, investment_id, initial_investment, status '
        . 'from investment_data '
        . 'where user_id = ? '
        . "and (status = 'ACTIVE' or status = 'INACTIVE')"
        . 'order by status asc', [$this->userId]);
}

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 2021-05-10
    • 2022-11-26
    • 2022-10-12
    • 2021-09-14
    • 2021-10-25
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多