mike
mike

Reputation: 1

How to display the result of 2 variables laravel

I want to display the results of 2 functions from the controller on the home page. I want to count from the computers table how many DELLs there are and how many IBMs.

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]);

 }

Route 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>

Unfortunately, I only get the result of one of the variables. The other does not display the result

Upvotes: 0

Views: 85

Answers (1)

STA
STA

Reputation: 34828

You can't define same URI with same method route at a time, so delete one :

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

And your controller will be :

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]);
 }

Upvotes: 2

Related Questions