从Laravel Eloquent对象获取数据?

I want to add some more data to Eloquent model object before passing to view/json.

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

I want to add few more data with $company,my tries code is below,

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

But it is not working,Is there any solution?

If you want to add data to $company. you can make it an array and pass the value
$company[] = Company::with("users")->get();

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

Try This Hope it working

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

Then Check the result.

the with method accepts an array. static Builder|Model with(array|string $relations) meaning you can say Model::with(['one','two'])->get();