英文:
Laravel 9 Livewire attempt to read property "id" on array
问题
我是新手使用PHP,但我一直遇到错误“尝试在数组上读取属性“id””。
这是我的视图list-monitors-get-show:
```html
<!-- ... (你的HTML代码) ... -->
这是我的Livewire控制器getmonitor:
public $state, $stateService, $schedules, $polyclinics = [];
// ... (你的其他代码) ...
当我尝试dd($this->polyclinics); [这个 poly][1]
当我尝试dd($this->schedules); [这个 schedule][2]
当我尝试在loadSchedule和addSchedule函数中使用dd时,我可以得到schedules数据。但是当删除那个dd后,我得到错误“尝试在数组上读取属性“id””。
我还尝试过将:
<td wire:click="addSchedule({{ $polyclinic->id }})">
更改为:
<td wire:click="addSchedule({{ $polyclinic['id'] }})">
但我得到错误“无法将stdClass对象用作数组”。
当我点击“Jenis Antrian”中的1项时
[jenis antrian][3]
成功显示Spesialis项目
[specialis][4]
但是当我在specialise上单击1项时
error
我想在那个表格中显示时间表数据
我想要的
[1]: https://i.stack.imgur.com/mjk9N.png
[2]: https://i.stack.imgur.com/4S11V.png
[3]: https://i.stack.imgur.com/mw9a0.png
[4]: https://i.stack.imgur.com/VOYfP.png
<details>
<summary>英文:</summary>
i'm new using php, and i keep getting error "attempt to read property "id" on array"
this my view list-monitors-get-show
<div>
<x-loading-indicator></x-loading-indicator>
<div class="d-flex justify-content-between">
<div>
<p>
<b>
Monitor 1
</b>
</p>
</div>
<div>
<nav style="--falcon-breadcrumb-divider: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8'%3E%3Cpath d='M2.5 0L1 1.5 3.5 4 1 6.5 2.5 8l4-4-4-4z' fill='%23748194'/%3E%3C/svg%3E");"
aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">Dashboard</a></li>
<li class="breadcrumb-item active" aria-current="page">Monitor 1</li>
</ol>
</nav>
</div>
</div>
<div class="row">
<div class="col-lg-3">
<div class="card">
<div class="card-header">
<h5 class="mb-0">Jenis Antrian</h5>
</div>
<div class="card-body overflow-hidden p-2">
@forelse ($monitors as $monitor)
<a class="border-bottom-0 notification rounded-0 border-x-0 border border-300 w-100 mb-3"
wire:click.prevent="addItem({{ $monitor->ms_id }})">
<div class="notification-body">
<h5 class="fs-0">
{{ $monitor->ms_name }}
</h5>
</div>
</a>
@empty
<a class="border-bottom-0 notification rounded-0 border-x-0 border border-300" href="#!">
<div class="notification-body">
<p class="mb-1">
no record found...
</p>
</div>
</a>
@endforelse
</div>
</div>
</div>
<div class="col-lg-4">
<div class="card">
<div class="card-header">
<h5 class="mb-0">Spesialis</h5>
</div>
<div class="table-responsive scrollbar containerme" id="items-container" style="position: relative; display: grid; grid-template-columns: auto auto auto; overflow:auto;">
<div style="height: 450px;">
<table class="table table-hover table-striped overflow-hidden">
<tbody>
@foreach ($polyclinics as $polyclinic)
<tr>
<td wire:click="addSchedule({{ $polyclinic->id }})">
<h6 class="fs--1" value={{ $polyclinic_id }}>{{ $polyclinic->name }}</h6>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="col-lg-5">
<div class="card">
<div class="table-responsive scrollbar">
<table class="table table-hover table-striped overflow-hidden">
<thead>
<tr>
<th scope="col">Dokter</th>
</tr>
</thead>
<tbody>
@foreach ($schedules as $schedule)
<tr class="align-middle">
<td class="text-nowrap">
<h6 class="fs-0">
{{ $schedule->full_name }}
<span class="badge rounded-pill badge-soft-success">
{{ $schedule->BpjsStart }} - {{ $schedule->BpjsEnd }}
</span>
</h6>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
and my livewire controller getmonitor
public $state, $stateService, $schedules, $polyclinics = [];
public $monitorId = 0;
public $serviceId = 0;
public $polyclinic_id = 0;
public $isBPJS = 0;
public function mount($id)
{
if ($id !== null) {
$this->state = MonitorGetQueueItem::where('id', $id)->get();
$this->state->toArray();
$this->monitorId = $id;
$this->schedules = [];
} else {
$this->state = [];
$this->schedules = [];
$this->polyclinics = [];
}
}
public function addItem($id)
{
$BPJS = "Bpjs";
$this->stateService = Service::where('id', $id)->get();
if ($this->stateService[0]['clinic'] == 1) {
if (stripos($this->stateService[0]['name'], $BPJS)) {
$this->isBPJS = 1;
$this->loadPolyClinic($this->isBPJS);
} else {
$this->isBPJS = 0;
$this->loadPolyClinic($this->isBPJS);
}
} else {
$this->loadPolyClinic();
}
}
public function loadPolyClinic($isBPJS = null)
{
if ($isBPJS !== null) {
$this->polyclinics = DB::connection("sqlsrv2")->table(DB::raw("(
SELECT mmp.id,mmp.name,
CASE WHEN rtds.bpjs_starttime <> 0 THEN 1 ELSE 0 END AS isBpjs
FROM reg_txn_doctor_schedule AS rtds
LEFT OUTER JOIN mktg_mst_clinic AS mmc ON mmc.id = rtds.clinic_id
LEFT OUTER JOIN mktg_mst_polyclinic AS mmp ON mmp.id = mmc.polyclinic_id
WHERE
CONVERT(DATE,rtds.[date]) = CONVERT(DATE,GETDATE())
AND rtds.[disabled]=0
AND rtds.inactive=0
AND mmp.[type]='SPECIALIST'
AND mmp.id NOT IN (68,64,67)
GROUP BY mmp.id, mmp.name, rtds.bpjs_starttime) as a"))
->select("*")
->where('isBpjs', $isBPJS)
->groupBy('id', 'name', 'isBpjs')
->get();
} else {
$this->polyclinics = [];
}
}
public function addSchedule($id)
{
$this->polyclinic_id = $id;
$this->loadSchedule($this->polyclinic_id);
}
public function loadSchedule($id = null)
{
if ($id) {
$this->schedules = DB::connection("sqlsrv2")->table(DB::raw("(SELECT
rtds.id AS schedule_id,
mmmp.full_name,
mmp.name as polyclinic,
CASE WHEN rtds.bpjs_starttime-CAST(LTRIM(DATEDIFF(MINUTE, 0, CONVERT(VARCHAR(5),GETDATE(),108))) AS INT) <= 120 THEN 1 ELSE 0 END AS isStart,
CASE WHEN rtds.bpjs_starttime = 0 THEN 1 ELSE 0 END AS isBpjs,
RIGHT('0' + CAST(rtds.cash_startime / 60 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(rtds.cash_startime % 60 AS VARCHAR),2) as [CashStart],
RIGHT('0' + CAST(rtds.cash_endtime / 60 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(rtds.cash_endtime % 60 AS VARCHAR),2) as [CashEnd],
RIGHT('0' + CAST(rtds.insurance_starttime / 60 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(rtds.insurance_starttime % 60 AS VARCHAR),2) as [InsuranceStart],
RIGHT('0' + CAST(rtds.insuranace_endtime / 60 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(rtds.insuranace_endtime % 60 AS VARCHAR),2) as [InsuranceEnd],
RIGHT('0' + CAST(rtds.bpjs_starttime / 60 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(rtds.bpjs_starttime % 60 AS VARCHAR),2) as [BpjsStart],
RIGHT('0' + CAST(rtds.bpjs_endtime / 60 AS VARCHAR),2) + ':' +
RIGHT('0' + CAST(rtds.bpjs_endtime % 60 AS VARCHAR),2) as [BpjsEnd],
rtds.cash_allocated_offline_quota + rtds.insurance_allocated_offline_quota AS totalOffNb,
rtds.cash_used_offline_quota + rtds.insurance_used_offline_quota AS usedOffNb,
rtds.bpjs_allocated_offline_quota AS totalOffB, rtds.bpjs_used_offline_quota AS usedOffB,
rtds.cash_allocated_offline_quota + rtds.insurance_allocated_offline_quota + rtds.bpjs_allocated_offline_quota AS totalOff,
rtds.cash_used_offline_quota + rtds.insurance_used_offline_quota + rtds.bpjs_used_offline_quota AS usedOff
FROM reg_txn_doctor_schedule AS rtds
LEFT OUTER JOIN mktg_mst_med_pract AS mmmp ON mmmp.id = rtds.doctor_id
LEFT OUTER JOIN mktg_mst_clinic AS mmc ON mmc.id = rtds.clinic_id
LEFT OUTER JOIN mktg_mst_polyclinic AS mmp ON mmp.id = mmc.polyclinic_id
WHERE
CONVERT(DATE,rtds.[date]) = CONVERT(DATE,GETDATE())
AND rtds.[disabled] = 0
AND rtds.inactive = 0
AND mmp.id = $id) as b"))
->orderByRaw('b.CashStart ASC,b.CashEnd ASC')
->get();
// dd($this->polyclinics, $this->polyclinic_id, $this->schedules);
} else {
$this->schedules = [];
}
}
public function render()
{
$monitorItem = MonitorGetQueueItem::selectRaw('mst_monitor_get_queue_item.id,mst_monitor_get_queue.id as mmgq_id, mst_monitor_get_queue.name as mmgq_name, mst_service.id as ms_id, mst_service.name as ms_name, mst_service.prefix')
->where('monitor_get_queue_id', $this->monitorId)
->where('mst_monitor_get_queue_item.disabled', 0)
->join('mst_monitor_get_queue', 'mst_monitor_get_queue.id', '=', 'mst_monitor_get_queue_item.monitor_get_queue_id')
->join('mst_service', 'mst_service.id', '=', 'mst_monitor_get_queue_item.service_id')
->get();
// $this->polyclinics = $this->loadPolyClinic($this->isBPJS);
// dd($this->polyclinics);
return view('livewire.admin.monitors.list-monitors-get-show', [
'monitors' => $monitorItem,
'polyclinics' => $this->polyclinics,
'schedules' => $this->schedules,
]);
}
when i try dd($this->polyclinics); [this poly][1]
when i try dd($this->schedules); [this schedule][2]
when i try to dd on function loadSchedule and addSchedule, i got schedules data. but when delete that dd. i got error "attempt to read property "id" on array"
i also tried to change
<td wire:click="addSchedule({{ $polyclinic->id }})">
to
<td wire:click="addSchedule({{ $polyclinic['id'] }})">
but i got error "cannot use object of type stdClass as array"
when I click 1 item on "Jenis Antrian"
[jenis antrian][3]
success to show Spesialis items
[specialis][4]
but when I click 1 item on specialise
[error](https://i.stack.imgur.com/xI0UU.png)
i want to show schedule data on that table
[what i want](https://i.stack.imgur.com/MCLs1.png)
[1]: https://i.stack.imgur.com/mjk9N.png
[2]: https://i.stack.imgur.com/4S11V.png
[3]: https://i.stack.imgur.com/mw9a0.png
[4]: https://i.stack.imgur.com/VOYfP.png
</details>
# 答案1
**得分**: 1
你正在以对象方式访问$polyclinic。
我认为你的意思是要以数组方式访问它,所以请在“addSchedule”方法内更新代码为`$polyclinic['id']`?
<details>
<summary>英文:</summary>
You're accessing $polyclinic as an object.
I believe you mean to access it as an array so update the code within the "addSchedule" method to `$polyclinic['id']` ?
[![snippet][1]][1]
[1]: https://i.stack.imgur.com/8Yy1m.png
</details>
# 答案2
**得分**: 0
同样的问题发生在我身上。我通过获取搜索结果并将其转换为数组来解决了这个问题。
在我的情况下,我收到了一个包含StdClass对象的数组,所以在发送整个对象集合之前,我使用了array_map来将对象转换为数组。这样就解决了问题。
<details>
<summary>英文:</summary>
The same problem happened to me. I solved it by taking the search result and converting it into an array.
In my case, I received an array with StdClass objects so before sending the entire collection of objects, I used array_map to cast the objects into arrays. It worked
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论