簡體   English   中英

在 Laravel PhP 中調用 NULL 上的成員函數

[英]Call to member function on NULL in Laravel PhP

我有以下錯誤。 客戶模型與約會模型有關系。 當我用他們的 id (1) 替換變量 $client 時,它成功地返回了一組約會。 但是對於變量,它返回 NULL。 我正在嘗試遍歷每個客戶的約會以計算出持續時間總數。 我的錯誤是在每個循環中使用前一個 $client 變量時我無法檢索約會。

僅供參考 foreach $clients as $client 循環返回所有客戶端的集合。

在 null 上調用成員函數約會()

公共功能自動生成(請求 $request){

    $date1       =  $request->input('date_from');
    $date2       =  $request->input('date_to'); 
    $customer    = $request->input('customer_account');

    if(count($customer) > 0)
    {       
        $clients     = Customer::find($customer)->client()->get();

        foreach($clients as $client)
            { 
                $appointments = Client::find($clients)->appointments()->get();

                    foreach($appointments as $appointment)
                    {       
                        //other code            
                    }               
            }

        //other code


    }
    $invoices = Invoice::all();
    return redirect('invoice');
}

我不確定您的$client變量是如何構建的,但您可能應該使用

$appointments = Client::find($client)->appointments()->get();

或者

$appointments = Client::find($client->id)->appointments()->get();

請注意在我的第一個示例中使用$client而不是$clients

暫無
暫無

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

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