Esoteriker
Esoteriker

Reputation: 51

why is always this error Undefined variable $question

I try to give the data from database to the blade.php.

The controller:

class DataController extends Controller
{
// sql query from datebase
public  function  question_number()
{
    $questions =  DB::table('questions')->sum('id');

    return  view('/statistics',['question'  =>  $questions ]);
}


public  function  group_number()
{
    $groups =  DB::table('groups')->sum('id');

    return  view('/statistics', ['group'  =>  $groups ]);
}

}

in there I have define the value $question and $group

and in Blade.php I have try to use this value:

<div>Die Anzahl von Fragen</div>
                    <p> {{ $question }} </p>
                    <div>Die Anzahl von Gruppen</div>
                    <p> {{ $group }} </p>  

in route

Route::get('/statistics', 'DataController@question_number');
Route::get('/statistics', 'DataController@group_number');

but the error is always

Undefined variable $question

What did I miss? And what should I do?

Upvotes: 0

Views: 54

Answers (1)

Rouhollah Mazarei
Rouhollah Mazarei

Reputation: 4153

You want to send 2 variables to one view, I think. To do this you don't need 2 routes and 2 controllers. In fact you CAN'T do that.

Route File:

Route::get('/statistics', 'DataController@question_number');

Controller:

class DataController extends Controller
{
    // sql query from datebase
    public  function  question_number()
    {
        $questions =  DB::table('questions')->sum('id');
        $groups =  DB::table('groups')->sum('id');

        return  view('/statistics',['question'  =>  $questions , 'group' => $groups]);
    }     
}

View (without change):

<div>Die Anzahl von Fragen</div>
                    <p> {{ $question }} </p>
                    <div>Die Anzahl von Gruppen</div>
                    <p> {{ $group }} </p>  

Upvotes: 3

Related Questions