0
votes

I'm using tabs but same controller, different methods. How to return values to different views with same route?

In /users, get value from db via BuyerSellerController@buyers method for buyer.

Route::get('users','BuyerSellerController@buyers');

In /users as well, get value from db via BuyerSellerController@sellers method for seller.

Route::get('users','BuyerSellerController@sellers');

//BuyerSellerController

public function buyers()
{
    $buyerSeller = DB::table('buyerseller')
        ->where('buyerseller','=','Buyer')
        ->pluck('id');

    $buyers = DB::table('users')
        ->where('buyerseller','=',$buyerSeller)
        ->get();

    return View::make('pages.users')->with('buyers', $buyers);
}

public function sellers()
{
    $buyerSeller = DB::table('buyerseller')
        ->where('buyerseller','=','Seller')
        ->pluck('id');

    $sellers = DB::table('users')
        ->where('buyerseller','=',$buyerSeller)
        ->get();

    return View::make('pages.users')->with('sellers', $sellers);
}

//users.blade.php Then I got this error:

Undefined variable: sellers (View: ...)
1
You have to use different URIs. How would the application otherwise know which controller method to call? - lukasgeiter

1 Answers

0
votes

compact saved my life! :D

public function index()
{
    /* buyers */
    $buyerSeller = DB::table('buyerseller')
        ->where('buyerseller','=','Buyer')
        ->pluck('id');

    $buyers = DB::table('users')
        ->where('buyerseller','=',$buyerSeller)
        ->get();


    /* sellers */
    $buyerSeller = DB::table('buyerseller')
        ->where('buyerseller','=','Seller')
        ->pluck('id');

    $sellers = DB::table('users')
        ->where('buyerseller','=',$buyerSeller)
        ->get();

    return View::make('pages.users', compact('buyers', 'sellers'));

}