php
  • laravel-5
  • 2015-03-25 73 views 0 likes 
    0

    我想在傳遞給view/json之前在Eloquent模型對象中添加更多的數據。從Laravel Eloquent對象獲取數據?

    $company = Company::with("users")->get(); 
    

    我想與$company添加一些更多的數據,我嘗試代碼如下,

    $company->data->client_count = User::where('status', '=', 1)->count(); 
    

    但它不能正常工作,有什麼解決辦法嗎?

    回答

    0

    如果你想添加數據到$company。你可以把它的數組,並傳遞值
    $company[] = Company::with("users")->get();

    $company[] = User::where('status', '=', 1)->count();

    0

    嘗試這個希望它的工作

    $公司[ '數'] =用戶::在哪裏( '狀態',「= ',1) - > count();

    然後檢查結果。

    0

    with方法接受一個數組。 static Builder | Model(with array | string $ relations) 這意味着你可以說Model :: with(['one','two']) - > get();

    相關問題