Laravel 9 Livewire 尝试在数组上读取属性 “id”

huangapple go评论108阅读模式
英文:

Laravel 9 Livewire attempt to read property "id" on array

问题

  1. 我是新手使用PHP,但我一直遇到错误“尝试在数组上读取属性“id””。
  2. 这是我的视图list-monitors-get-show
  3. ```html
  4. <!-- ... (你的HTML代码) ... -->

这是我的Livewire控制器getmonitor:

  1. public $state, $stateService, $schedules, $polyclinics = [];
  2. // ... (你的其他代码) ...

当我尝试dd($this->polyclinics); [这个 poly][1]

当我尝试dd($this->schedules); [这个 schedule][2]

当我尝试在loadSchedule和addSchedule函数中使用dd时,我可以得到schedules数据。但是当删除那个dd后,我得到错误“尝试在数组上读取属性“id””。

我还尝试过将:

  1. <td wire:click="addSchedule({{ $polyclinic->id }})">

更改为:

  1. <td wire:click="addSchedule({{ $polyclinic['id'] }})">

但我得到错误“无法将stdClass对象用作数组”。

当我点击“Jenis Antrian”中的1项时
[jenis antrian][3]

成功显示Spesialis项目
[specialis][4]

但是当我在specialise上单击1项时
error

我想在那个表格中显示时间表数据
我想要的

  1. [1]: https://i.stack.imgur.com/mjk9N.png
  2. [2]: https://i.stack.imgur.com/4S11V.png
  3. [3]: https://i.stack.imgur.com/mw9a0.png
  4. [4]: https://i.stack.imgur.com/VOYfP.png
  5. <details>
  6. <summary>英文:</summary>
  7. i&#39;m new using php, and i keep getting error &quot;attempt to read property &quot;id&quot; on array&quot;
  8. 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(&#34;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&#34;);"
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>

  1. &lt;div class=&quot;row&quot;&gt;
  2. &lt;div class=&quot;col-lg-3&quot;&gt;
  3. &lt;div class=&quot;card&quot;&gt;
  4. &lt;div class=&quot;card-header&quot;&gt;
  5. &lt;h5 class=&quot;mb-0&quot;&gt;Jenis Antrian&lt;/h5&gt;
  6. &lt;/div&gt;
  7. &lt;div class=&quot;card-body overflow-hidden p-2&quot;&gt;
  8. @forelse ($monitors as $monitor)
  9. &lt;a class=&quot;border-bottom-0 notification rounded-0 border-x-0 border border-300 w-100 mb-3&quot;
  10. wire:click.prevent=&quot;addItem({{ $monitor-&gt;ms_id }})&quot;&gt;
  11. &lt;div class=&quot;notification-body&quot;&gt;
  12. &lt;h5 class=&quot;fs-0&quot;&gt;
  13. {{ $monitor-&gt;ms_name }}
  14. &lt;/h5&gt;
  15. &lt;/div&gt;
  16. &lt;/a&gt;
  17. @empty
  18. &lt;a class=&quot;border-bottom-0 notification rounded-0 border-x-0 border border-300&quot; href=&quot;#!&quot;&gt;
  19. &lt;div class=&quot;notification-body&quot;&gt;
  20. &lt;p class=&quot;mb-1&quot;&gt;
  21. no record found...
  22. &lt;/p&gt;
  23. &lt;/div&gt;
  24. &lt;/a&gt;
  25. @endforelse
  26. &lt;/div&gt;
  27. &lt;/div&gt;
  28. &lt;/div&gt;
  29. &lt;div class=&quot;col-lg-4&quot;&gt;
  30. &lt;div class=&quot;card&quot;&gt;
  31. &lt;div class=&quot;card-header&quot;&gt;
  32. &lt;h5 class=&quot;mb-0&quot;&gt;Spesialis&lt;/h5&gt;
  33. &lt;/div&gt;
  34. &lt;div class=&quot;table-responsive scrollbar containerme&quot; id=&quot;items-container&quot; style=&quot;position: relative; display: grid; grid-template-columns: auto auto auto; overflow:auto;&quot;&gt;
  35. &lt;div style=&quot;height: 450px;&quot;&gt;
  36. &lt;table class=&quot;table table-hover table-striped overflow-hidden&quot;&gt;
  37. &lt;tbody&gt;
  38. @foreach ($polyclinics as $polyclinic)
  39. &lt;tr&gt;
  40. &lt;td wire:click=&quot;addSchedule({{ $polyclinic-&gt;id }})&quot;&gt;
  41. &lt;h6 class=&quot;fs--1&quot; value={{ $polyclinic_id }}&gt;{{ $polyclinic-&gt;name }}&lt;/h6&gt;
  42. &lt;/td&gt;
  43. &lt;/tr&gt;
  44. @endforeach
  45. &lt;/tbody&gt;
  46. &lt;/table&gt;
  47. &lt;/div&gt;
  48. &lt;/div&gt;
  49. &lt;/div&gt;
  50. &lt;/div&gt;

<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>

  1. and my livewire controller getmonitor

public $state, $stateService, $schedules, $polyclinics = [];

  1. public $monitorId = 0;
  2. public $serviceId = 0;
  3. public $polyclinic_id = 0;
  4. public $isBPJS = 0;
  5. public function mount($id)
  6. {
  7. if ($id !== null) {
  8. $this-&gt;state = MonitorGetQueueItem::where(&#39;id&#39;, $id)-&gt;get();
  9. $this-&gt;state-&gt;toArray();
  10. $this-&gt;monitorId = $id;
  11. $this-&gt;schedules = [];
  12. } else {
  13. $this-&gt;state = [];
  14. $this-&gt;schedules = [];
  15. $this-&gt;polyclinics = [];
  16. }
  17. }
  18. public function addItem($id)
  19. {
  20. $BPJS = &quot;Bpjs&quot;;
  21. $this-&gt;stateService = Service::where(&#39;id&#39;, $id)-&gt;get();
  22. if ($this-&gt;stateService[0][&#39;clinic&#39;] == 1) {
  23. if (stripos($this-&gt;stateService[0][&#39;name&#39;], $BPJS)) {
  24. $this-&gt;isBPJS = 1;
  25. $this-&gt;loadPolyClinic($this-&gt;isBPJS);
  26. } else {
  27. $this-&gt;isBPJS = 0;
  28. $this-&gt;loadPolyClinic($this-&gt;isBPJS);
  29. }
  30. } else {
  31. $this-&gt;loadPolyClinic();
  32. }
  33. }
  34. public function loadPolyClinic($isBPJS = null)
  35. {
  36. if ($isBPJS !== null) {
  37. $this-&gt;polyclinics = DB::connection(&quot;sqlsrv2&quot;)-&gt;table(DB::raw(&quot;(
  38. SELECT mmp.id,mmp.name,
  39. CASE WHEN rtds.bpjs_starttime &lt;&gt; 0 THEN 1 ELSE 0 END AS isBpjs
  40. FROM reg_txn_doctor_schedule AS rtds
  41. LEFT OUTER JOIN mktg_mst_clinic AS mmc ON mmc.id = rtds.clinic_id
  42. LEFT OUTER JOIN mktg_mst_polyclinic AS mmp ON mmp.id = mmc.polyclinic_id
  43. WHERE
  44. CONVERT(DATE,rtds.[date]) = CONVERT(DATE,GETDATE())
  45. AND rtds.[disabled]=0
  46. AND rtds.inactive=0
  47. AND mmp.[type]=&#39;SPECIALIST&#39;
  48. AND mmp.id NOT IN (68,64,67)
  49. GROUP BY mmp.id, mmp.name, rtds.bpjs_starttime) as a&quot;))
  50. -&gt;select(&quot;*&quot;)
  51. -&gt;where(&#39;isBpjs&#39;, $isBPJS)
  52. -&gt;groupBy(&#39;id&#39;, &#39;name&#39;, &#39;isBpjs&#39;)
  53. -&gt;get();
  54. } else {
  55. $this-&gt;polyclinics = [];
  56. }
  57. }
  58. public function addSchedule($id)
  59. {
  60. $this-&gt;polyclinic_id = $id;
  61. $this-&gt;loadSchedule($this-&gt;polyclinic_id);
  62. }
  63. public function loadSchedule($id = null)
  64. {
  65. if ($id) {
  66. $this-&gt;schedules = DB::connection(&quot;sqlsrv2&quot;)-&gt;table(DB::raw(&quot;(SELECT
  67. rtds.id AS schedule_id,
  68. mmmp.full_name,
  69. mmp.name as polyclinic,
  70. CASE WHEN rtds.bpjs_starttime-CAST(LTRIM(DATEDIFF(MINUTE, 0, CONVERT(VARCHAR(5),GETDATE(),108))) AS INT) &lt;= 120 THEN 1 ELSE 0 END AS isStart,
  71. CASE WHEN rtds.bpjs_starttime = 0 THEN 1 ELSE 0 END AS isBpjs,
  72. RIGHT(&#39;0&#39; + CAST(rtds.cash_startime / 60 AS VARCHAR),2) + &#39;:&#39; +
  73. RIGHT(&#39;0&#39; + CAST(rtds.cash_startime % 60 AS VARCHAR),2) as [CashStart],
  74. RIGHT(&#39;0&#39; + CAST(rtds.cash_endtime / 60 AS VARCHAR),2) + &#39;:&#39; +
  75. RIGHT(&#39;0&#39; + CAST(rtds.cash_endtime % 60 AS VARCHAR),2) as [CashEnd],
  76. RIGHT(&#39;0&#39; + CAST(rtds.insurance_starttime / 60 AS VARCHAR),2) + &#39;:&#39; +
  77. RIGHT(&#39;0&#39; + CAST(rtds.insurance_starttime % 60 AS VARCHAR),2) as [InsuranceStart],
  78. RIGHT(&#39;0&#39; + CAST(rtds.insuranace_endtime / 60 AS VARCHAR),2) + &#39;:&#39; +
  79. RIGHT(&#39;0&#39; + CAST(rtds.insuranace_endtime % 60 AS VARCHAR),2) as [InsuranceEnd],
  80. RIGHT(&#39;0&#39; + CAST(rtds.bpjs_starttime / 60 AS VARCHAR),2) + &#39;:&#39; +
  81. RIGHT(&#39;0&#39; + CAST(rtds.bpjs_starttime % 60 AS VARCHAR),2) as [BpjsStart],
  82. RIGHT(&#39;0&#39; + CAST(rtds.bpjs_endtime / 60 AS VARCHAR),2) + &#39;:&#39; +
  83. RIGHT(&#39;0&#39; + CAST(rtds.bpjs_endtime % 60 AS VARCHAR),2) as [BpjsEnd],
  84. rtds.cash_allocated_offline_quota + rtds.insurance_allocated_offline_quota AS totalOffNb,
  85. rtds.cash_used_offline_quota + rtds.insurance_used_offline_quota AS usedOffNb,
  86. rtds.bpjs_allocated_offline_quota AS totalOffB, rtds.bpjs_used_offline_quota AS usedOffB,
  87. rtds.cash_allocated_offline_quota + rtds.insurance_allocated_offline_quota + rtds.bpjs_allocated_offline_quota AS totalOff,
  88. rtds.cash_used_offline_quota + rtds.insurance_used_offline_quota + rtds.bpjs_used_offline_quota AS usedOff
  89. FROM reg_txn_doctor_schedule AS rtds
  90. LEFT OUTER JOIN mktg_mst_med_pract AS mmmp ON mmmp.id = rtds.doctor_id
  91. LEFT OUTER JOIN mktg_mst_clinic AS mmc ON mmc.id = rtds.clinic_id
  92. LEFT OUTER JOIN mktg_mst_polyclinic AS mmp ON mmp.id = mmc.polyclinic_id
  93. WHERE
  94. CONVERT(DATE,rtds.[date]) = CONVERT(DATE,GETDATE())
  95. AND rtds.[disabled] = 0
  96. AND rtds.inactive = 0
  97. AND mmp.id = $id) as b&quot;))
  98. -&gt;orderByRaw(&#39;b.CashStart ASC,b.CashEnd ASC&#39;)
  99. -&gt;get();
  100. // dd($this-&gt;polyclinics, $this-&gt;polyclinic_id, $this-&gt;schedules);
  101. } else {
  102. $this-&gt;schedules = [];
  103. }
  104. }
  105. public function render()
  106. {
  107. $monitorItem = MonitorGetQueueItem::selectRaw(&#39;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&#39;)
  108. -&gt;where(&#39;monitor_get_queue_id&#39;, $this-&gt;monitorId)
  109. -&gt;where(&#39;mst_monitor_get_queue_item.disabled&#39;, 0)
  110. -&gt;join(&#39;mst_monitor_get_queue&#39;, &#39;mst_monitor_get_queue.id&#39;, &#39;=&#39;, &#39;mst_monitor_get_queue_item.monitor_get_queue_id&#39;)
  111. -&gt;join(&#39;mst_service&#39;, &#39;mst_service.id&#39;, &#39;=&#39;, &#39;mst_monitor_get_queue_item.service_id&#39;)
  112. -&gt;get();
  113. // $this-&gt;polyclinics = $this-&gt;loadPolyClinic($this-&gt;isBPJS);
  114. // dd($this-&gt;polyclinics);
  115. return view(&#39;livewire.admin.monitors.list-monitors-get-show&#39;, [
  116. &#39;monitors&#39; =&gt; $monitorItem,
  117. &#39;polyclinics&#39; =&gt; $this-&gt;polyclinics,
  118. &#39;schedules&#39; =&gt; $this-&gt;schedules,
  119. ]);
  120. }
  1. when i try dd($this-&gt;polyclinics); [this poly][1]
  2. when i try dd($this-&gt;schedules); [this schedule][2]
  3. when i try to dd on function loadSchedule and addSchedule, i got schedules data. but when delete that dd. i got error &quot;attempt to read property &quot;id&quot; on array&quot;
  4. i also tried to change
  5. &lt;td wire:click=&quot;addSchedule({{ $polyclinic-&gt;id }})&quot;&gt;
  6. to
  7. &lt;td wire:click=&quot;addSchedule({{ $polyclinic[&#39;id&#39;] }})&quot;&gt;
  8. but i got error &quot;cannot use object of type stdClass as array&quot;
  9. when I click 1 item on &quot;Jenis Antrian&quot;
  10. [jenis antrian][3]
  11. success to show Spesialis items
  12. [specialis][4]
  13. but when I click 1 item on specialise
  14. [error](https://i.stack.imgur.com/xI0UU.png)
  15. i want to show schedule data on that table
  16. [what i want](https://i.stack.imgur.com/MCLs1.png)
  17. [1]: https://i.stack.imgur.com/mjk9N.png
  18. [2]: https://i.stack.imgur.com/4S11V.png
  19. [3]: https://i.stack.imgur.com/mw9a0.png
  20. [4]: https://i.stack.imgur.com/VOYfP.png
  21. </details>
  22. # 答案1
  23. **得分**: 1
  24. 你正在以对象方式访问$polyclinic
  25. 我认为你的意思是要以数组方式访问它,所以请在“addSchedule”方法内更新代码为`$polyclinic['id']`
  26. <details>
  27. <summary>英文:</summary>
  28. You&#39;re accessing $polyclinic as an object.
  29. I believe you mean to access it as an array so update the code within the &quot;addSchedule&quot; method to `$polyclinic[&#39;id&#39;]` ?
  30. [![snippet][1]][1]
  31. [1]: https://i.stack.imgur.com/8Yy1m.png
  32. </details>
  33. # 答案2
  34. **得分**: 0
  35. 同样的问题发生在我身上。我通过获取搜索结果并将其转换为数组来解决了这个问题。
  36. 在我的情况下,我收到了一个包含StdClass对象的数组,所以在发送整个对象集合之前,我使用了array_map来将对象转换为数组。这样就解决了问题。
  37. <details>
  38. <summary>英文:</summary>
  39. The same problem happened to me. I solved it by taking the search result and converting it into an array.
  40. 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
  41. </details>

huangapple
  • 本文由 发表于 2023年6月22日 11:46:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定