laravel计数两个不同的表,获得两个值后转换为单个值

时间:2019-05-19 09:16:48

标签: php laravel laravel-5.8

laravel计数两个不同的表,example(table-A,table-B)计数后,我们得到两个不同的值,即两个值example(table-A = 10,table-B = 10)如何求和该值example( table-A = 10 + table-B = 10),总值20 并将其传递给视图

public function index() 
{
 $table-A = A::all()->count(); 
$table-B = B::all()->count(); 
return view('home',compact('table-A','table-B')) 
}

1 个答案:

答案 0 :(得分:0)

以这种方式进行:

public function index()
{
    $tableA = A::all()->count();
    $tableB = B::all()->count();
    $sum = $tableA + $tableB;
    return view('home',compact('tableA','tableB', 'sum'))
}