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

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

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&#39;m new using php, and i keep getting error &quot;attempt to read property &quot;id&quot; on array&quot;

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>

&lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-lg-3&quot;&gt;
        &lt;div class=&quot;card&quot;&gt;
            &lt;div class=&quot;card-header&quot;&gt;
                &lt;h5 class=&quot;mb-0&quot;&gt;Jenis Antrian&lt;/h5&gt;
            &lt;/div&gt;
            &lt;div class=&quot;card-body overflow-hidden p-2&quot;&gt;
                @forelse ($monitors as $monitor)
                &lt;a class=&quot;border-bottom-0 notification rounded-0 border-x-0 border border-300 w-100 mb-3&quot;
                    wire:click.prevent=&quot;addItem({{ $monitor-&gt;ms_id }})&quot;&gt;
                    &lt;div class=&quot;notification-body&quot;&gt;
                        &lt;h5 class=&quot;fs-0&quot;&gt;
                            {{ $monitor-&gt;ms_name }}
                        &lt;/h5&gt;
                    &lt;/div&gt;
                &lt;/a&gt;
                @empty
                &lt;a class=&quot;border-bottom-0 notification rounded-0 border-x-0 border border-300&quot; href=&quot;#!&quot;&gt;
                    &lt;div class=&quot;notification-body&quot;&gt;
                        &lt;p class=&quot;mb-1&quot;&gt;
                            no record found...
                        &lt;/p&gt;
                    &lt;/div&gt;
                &lt;/a&gt;
                @endforelse
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    
    &lt;div class=&quot;col-lg-4&quot;&gt;
        &lt;div class=&quot;card&quot;&gt;
            &lt;div class=&quot;card-header&quot;&gt;
                &lt;h5 class=&quot;mb-0&quot;&gt;Spesialis&lt;/h5&gt;
            &lt;/div&gt;
            &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;
                &lt;div style=&quot;height: 450px;&quot;&gt;
                    &lt;table class=&quot;table table-hover table-striped overflow-hidden&quot;&gt;
                        &lt;tbody&gt;
                            @foreach ($polyclinics as $polyclinic)
                            &lt;tr&gt;
                                &lt;td wire:click=&quot;addSchedule({{ $polyclinic-&gt;id }})&quot;&gt;
                                        &lt;h6 class=&quot;fs--1&quot; value={{ $polyclinic_id }}&gt;{{ $polyclinic-&gt;name }}&lt;/h6&gt;
                                &lt;/td&gt;
                            &lt;/tr&gt;
                            @endforeach
                        &lt;/tbody&gt;
                    &lt;/table&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &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>


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-&gt;state = MonitorGetQueueItem::where(&#39;id&#39;, $id)-&gt;get();

        $this-&gt;state-&gt;toArray();

        $this-&gt;monitorId = $id;
        $this-&gt;schedules = [];
    } else {
        $this-&gt;state = [];
        $this-&gt;schedules = [];
        $this-&gt;polyclinics = [];
    }
}

public function addItem($id)
{
    $BPJS = &quot;Bpjs&quot;;
    $this-&gt;stateService = Service::where(&#39;id&#39;, $id)-&gt;get();
    if ($this-&gt;stateService[0][&#39;clinic&#39;] == 1) {
        if (stripos($this-&gt;stateService[0][&#39;name&#39;], $BPJS)) {
            $this-&gt;isBPJS = 1;
            $this-&gt;loadPolyClinic($this-&gt;isBPJS);
        } else {
            $this-&gt;isBPJS = 0;
            $this-&gt;loadPolyClinic($this-&gt;isBPJS);
        }
    } else {
        $this-&gt;loadPolyClinic();
    }
}

public function loadPolyClinic($isBPJS = null)
{
    if ($isBPJS !== null) {

        $this-&gt;polyclinics = DB::connection(&quot;sqlsrv2&quot;)-&gt;table(DB::raw(&quot;(
        	SELECT mmp.id,mmp.name,
        	CASE WHEN rtds.bpjs_starttime &lt;&gt; 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]=&#39;SPECIALIST&#39;
            AND mmp.id NOT IN (68,64,67)
            GROUP BY mmp.id, mmp.name, rtds.bpjs_starttime) as a&quot;))
            -&gt;select(&quot;*&quot;)
            -&gt;where(&#39;isBpjs&#39;, $isBPJS)
            -&gt;groupBy(&#39;id&#39;, &#39;name&#39;, &#39;isBpjs&#39;)
            -&gt;get();

    } else {
        $this-&gt;polyclinics = [];
    }
}

public function addSchedule($id)
{
    $this-&gt;polyclinic_id = $id;
    $this-&gt;loadSchedule($this-&gt;polyclinic_id);
}

public function loadSchedule($id = null)
{
    if ($id) {
        $this-&gt;schedules = DB::connection(&quot;sqlsrv2&quot;)-&gt;table(DB::raw(&quot;(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) &lt;= 120 THEN 1 ELSE 0 END AS isStart,
                CASE WHEN rtds.bpjs_starttime = 0 THEN 1 ELSE 0 END AS isBpjs,
                RIGHT(&#39;0&#39; + CAST(rtds.cash_startime / 60 AS VARCHAR),2)  + &#39;:&#39; +
                RIGHT(&#39;0&#39; + CAST(rtds.cash_startime % 60 AS VARCHAR),2) as [CashStart],
                RIGHT(&#39;0&#39; + CAST(rtds.cash_endtime / 60 AS VARCHAR),2)  + &#39;:&#39; +
                RIGHT(&#39;0&#39; + CAST(rtds.cash_endtime % 60 AS VARCHAR),2) as [CashEnd],
                RIGHT(&#39;0&#39; + CAST(rtds.insurance_starttime / 60 AS VARCHAR),2)  + &#39;:&#39; +
                RIGHT(&#39;0&#39; + CAST(rtds.insurance_starttime % 60 AS VARCHAR),2) as [InsuranceStart],
                RIGHT(&#39;0&#39; + CAST(rtds.insuranace_endtime / 60 AS VARCHAR),2)  + &#39;:&#39; +
                RIGHT(&#39;0&#39; + CAST(rtds.insuranace_endtime % 60 AS VARCHAR),2) as [InsuranceEnd],
                RIGHT(&#39;0&#39; + CAST(rtds.bpjs_starttime / 60 AS VARCHAR),2)  + &#39;:&#39; +
                RIGHT(&#39;0&#39; + CAST(rtds.bpjs_starttime % 60 AS VARCHAR),2) as [BpjsStart],
                RIGHT(&#39;0&#39; + CAST(rtds.bpjs_endtime / 60 AS VARCHAR),2)  + &#39;:&#39; +
                RIGHT(&#39;0&#39; + 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&quot;))
            -&gt;orderByRaw(&#39;b.CashStart ASC,b.CashEnd ASC&#39;)
            -&gt;get();

        // dd($this-&gt;polyclinics, $this-&gt;polyclinic_id, $this-&gt;schedules);
    } else {
        $this-&gt;schedules = [];
    }
}

public function render()
{
    $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;)
        -&gt;where(&#39;monitor_get_queue_id&#39;, $this-&gt;monitorId)
        -&gt;where(&#39;mst_monitor_get_queue_item.disabled&#39;, 0)
        -&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;)
        -&gt;join(&#39;mst_service&#39;, &#39;mst_service.id&#39;, &#39;=&#39;, &#39;mst_monitor_get_queue_item.service_id&#39;)
        -&gt;get();

    // $this-&gt;polyclinics = $this-&gt;loadPolyClinic($this-&gt;isBPJS);
    // dd($this-&gt;polyclinics);

    return view(&#39;livewire.admin.monitors.list-monitors-get-show&#39;, [
        &#39;monitors&#39; =&gt; $monitorItem,
        &#39;polyclinics&#39; =&gt; $this-&gt;polyclinics,
        &#39;schedules&#39; =&gt; $this-&gt;schedules,
    ]);
}

when i try dd($this-&gt;polyclinics); [this poly][1]

when i try dd($this-&gt;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 &quot;attempt to read property &quot;id&quot; on array&quot;

i also tried to change

    &lt;td wire:click=&quot;addSchedule({{ $polyclinic-&gt;id }})&quot;&gt;

to

    &lt;td wire:click=&quot;addSchedule({{ $polyclinic[&#39;id&#39;] }})&quot;&gt;

but i got error &quot;cannot use object of type stdClass as array&quot;

when I click 1 item on &quot;Jenis Antrian&quot;
[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&#39;re accessing $polyclinic as an object.
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;]` ?


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



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:

确定