尝试读取数组上的 属性 "investment_id"
Attempt to read property "investment_id" on array
我知道有很多类似的问题,不幸的是我找不到适合我的情况的解决方案。
当单击另一个列表中的 link 时,我想使用 Laravel Livewire 显示模态数据列表。但是,由于 Livewire 无法将 public 变量设置为 Eloquent 或 Query Builder 实例,所以我使用数组来检索列表,在这种情况下我使用 select()
方法,因为它 returns数组。然后,列表确实显示正确,但几秒钟后,提示错误 Attempt to read 属性 "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
投资-list.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>
InvestmentList.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
的变量转储结果
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"
}
]
最后我自己想通了,通过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]);
}
我知道有很多类似的问题,不幸的是我找不到适合我的情况的解决方案。
当单击另一个列表中的 link 时,我想使用 Laravel Livewire 显示模态数据列表。但是,由于 Livewire 无法将 public 变量设置为 Eloquent 或 Query Builder 实例,所以我使用数组来检索列表,在这种情况下我使用 select()
方法,因为它 returns数组。然后,列表确实显示正确,但几秒钟后,提示错误 Attempt to read 属性 "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
投资-list.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>
InvestmentList.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
的变量转储结果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"
}
]
最后我自己想通了,通过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]);
}