簡體   English   中英

如何顯示 2 個變量 laravel 的結果

[英]How to display the result of 2 variables laravel

我想在主頁上顯示 controller 的 2 個函數的結果。 我想從計算機表中數出有多少 DELL 和多少 IBM。

public function ibm()
    {
        $ibm= DB::table('computers')
            ->where( 'name', 'IBM')->count();
 
        return view('welcome', ['ibm'=>$ibm]);
 
    }
 
 
    public function dell()
    {
        $dell= DB::table('computers')
            ->where( 'name', 'DELL')->count();
 
        return view('welcome', ['dell'=>$dell]);

 }

路線 web.php:

Route::get('welcome','ComputerController@ibm');
Route::get('welcome','ComputerController@dell');

Welcome.blade.php

<php>

   DELL<p>{{  $dell ?? '' }}</p>

    IBM<p>{{  $ibm ?? '' }}</p>
</php>

不幸的是,我只得到其中一個變量的結果。 另一個不顯示結果

您不能一次定義具有相同方法路由的相同 URI,因此刪除一個:

Route::get('welcome','ComputerController@ibm');

您的 controller 將是:

public function ibm()
{
    $ibm= DB::table('computers')
        ->where('name', 'IBM')->get()->count();
    $dell= DB::table('computers')
        ->where('name', 'DELL')->get()->count();
 
    return view('welcome', ['ibm' => $ibm, 'dell' => $dell]);
 }

暫無
暫無

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

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