简体   繁体   中英

passing different type variables to the view from controller in laravel

i want to pass two variables called as repcounter and suppliers to the view to do two different task. here is my function in controller,

 public function admin()
    {

        $suppliers = SupplierData::all();
       $repcounter= SalesRep::count();


        return view('dashboard', compact('suppliers'));
    }

this is how i send suppliers data. it worked fine. but i don't have an idea to send repcounter and suppliers at once.. each time i try i gave error undefined variable. so how to send this two varibles to the dashboard.blade.php view?

You should try this:

public function admin()
    {

        $suppliers = SupplierData::all();
       $repcounter= SalesRep::count();


        return view('dashboard', compact('suppliers','repcounter'));
    }

Replace:

return view('dashboard', compact('suppliers'));

With the following code:

return view('dashboard', compact('suppliers,'repcounter'));

You can pass multiple variables to the view by nesting the data you want in an array. See below code.

public function admin()
    {

        //create an empty array
        $response = array();

        //nest your data insde the array instead of creating variables
        $response['suppliers'] = SupplierData::all();
        $response['repcounter'] = SalesRep::count();


        return view('dashboard', compact('response'));
    }

Inside your View, you can access them as below

$response['suppliers']
$response['repcounter']

This is how I'd implement it

public function admin()
{
    $data['suppliers'] = SupplierData::all();
    $data['repcounter']= SalesRep::count();

    return view('dashboard', $data);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM