【问题标题】:Laravel Union "Call to a member function union() on int"Laravel Union“调用 int 上的成员函数 union()”
【发布时间】:2021-04-28 03:17:35
【问题描述】:

错误:调用 int 上的成员函数 union()

$category = Category::select('name')->count();
$city = City::select('city')->count();
$client = Client::select('name')->count();

$property = Property::select('name')->count()
                    ->union($category)
                    ->union($city)
                    ->union($client)
                    ->get();

dd($property);

【问题讨论】:

  • 错误说明了一切,你不能调用count()->union($category)
  • @ArSeN 我应该如何行动才能获得联合中的所有计数?
  • @ArSeN 先做所有的并集,把 count 放在最后
  • @codedge "错误:在 int 上调用成员函数 getBindings()"$property=Property::select('name')->union($category)->union($city)->union($client)->count();

标签: php mysql laravel eloquent


【解决方案1】:

如果您只想要一个计数数组,则 union 不适合您。 Select count(*) from multiple tables

$counts = [
    'city' => City::count(),
    'category' => Category::count(),
    'client' => Client::count(),
    'property' => Property::count(),
];

无需将其保留在一个查询中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2020-07-21
    • 2011-03-07
    • 1970-01-01
    • 2022-01-22
    • 2017-05-22
    相关资源
    最近更新 更多