簡體   English   中英

Laravel Livewire 組件偏移

[英]Laravel Livewire component offset

我正在嘗試通過在Livewire組件內部調用的 Laravel 控制器獲取一些技術人員的數據。 在某些情況下,控制器返回null是因為沒有相關的技術人員。

這是我的 livewire 組件渲染視圖:

class TechniciansAssociated extends Component
{
    public function render()
    {
        $technicians = (new HomeController())->getTechnicians();

        return view('livewire.dashboard.technicians-associated', [
            'technicians' => $technicians,
        ]);
    }
}

這里從家庭控制器調用的函數:

public function getTechnicians()
{
    $customer_ids = json_decode(User::find(Auth::id())->userCustomers->customer_id,true);

    $technicians_data = DB::connection('mysql')
        -> select('SELECT distinct users.id, users.name, users.surname, users.lastname, users.email, users.cargo, users.photo
           FROM users
           INNER JOIN contractsproductsmulti AS cpm ON users.id = cpm.userId
           INNER JOIN contractsproducts AS cp ON cp.id = cpm.contractproduct_id
           INNER JOIN contracts AS cont ON cont.id = cp.idcontract
           WHERE users.isActive = 1 AND cont.idCustomer IN (?)', $customer_ids
        );

    if (count($technicians_data) > 0){
        return $technicians_data;
    }

    return null;
}

最后,當試圖獲取渲染 livewire 視圖的主視圖時,我遇到了錯誤

@section('content')

<section id="dash-cust-info">
    <div class="row p-t-30 p-b-30">
        <div class="col-md-6">
            <h4>Customer info</h4>
        </div>
        <div class="col-md-3">
            <h4>Comm_1</h4>
            <div class="card no-border no-margin">
                @livewire('dashboard.commercials-associated')
            </div>
        </div>
        <div class="col-md-3">
            <h4>Comm_2</h4>
            <div class="card no-border no-margin">
                @livewire('dashboard.technicians-associated')
            </div>
        </div>
    </div>
....

錯誤:

Undefined offset: 1 (View: /......../resources/views/home.blade.php)

我想這個答案為 livewire 中的此類問題提供了很好的解釋。 把它留在這里,以便將來遇到此類問題的任何人都可以獲得幫助。 不客氣

好的,LiveWire 組件需要一個 HTML 元素才能在它自己的視圖上獲得結果。 例如:

<div> //missed on my code
    @foreach($users as $user)
        {{$user->id}}
    @endforeach
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM