Laravel - Return multiple values within one method

Viewed 27261

Heres where I'm currently stuck at. I am doing multiple methods where I am doing the different queries to the same table:

public function totalOfA()
{
    return $a = Stocks::where('user_id','=', $this->employee->id)
        ->where('category','=','a')
        ->pluck('qty')
        ->sum();
}

public function totalOfB()
{
    return $a = Stocks::where('user_id','=', $this->employee->id)
        ->where('category','=','a')
        ->pluck('qty')
        ->sum();
}

I'm trying to look for a way to sum that all up to just one function

public function totalStocks()
{
    $stocks = Stocks::where('user_id','=', $this->employee->id)
        ->get();

    $a = $stocks::where('category', '=', 'a')
        ->pluck('qty')
        ->sum();

    $b = $stocks::where('category', '=', 'b')
        ->pluck('qty')
        ->sum();

    return $a and $b
}

So I can just call it from view as totalstocks()->a or totalstocks()->b something like that.

5 Answers

try this:

function totalStocks() {
   $a = 1;
   $b = 2;

   return  [$a, $b];
}

list($valueA, $valueB) = totalStocks();
echo $valueA; //1
echo $valueB; //2

You cant call like that.you can create an aray or you can pass seperately to view

public function totalStocks()
{
    $stocks = Stocks::where('user_id','=', $this->employee->id)
        ->get();

    $a = $stocks::where('category', '=', 'a')
        ->pluck('qty')
        ->sum();

    $b = $stocks::where('category', '=', 'b')
        ->pluck('qty')
        ->sum();

    return view('home',['stocks'=> $stocks,'a'=>$a,'b'=>$b]);

}

Now you can access $a ,$b in your view

 public function totalStocks()
    {

        $data['stocks'] = Stocks::where('user_id','=', $this->employee->id)
            ->get();

       $data['a'] = $stocks::where('category', '=', 'a')
            ->pluck('qty')
            ->sum();

       $data['b'] = $stocks::where('category', '=', 'b')
            ->pluck('qty')
            ->sum();

        return view('home',['data'=>$data]);

    }

so you can accessin view like {{$data['a']}}

Try this:

 $x = array(
        'a' => $a,
        'b' => $b,
    );
return (object) $x

Possible solution is that you define private/protected variables as a and b within the class and in the method totalStocks() you can set values to them.

So your function would look like this:

public function totalStocks()
{
    $stocks = Stocks::where('user_id','=', $this->employee->id)
        ->get();

    $this->a = $stocks::where('category', '=', 'a')
        ->pluck('qty')
        ->sum();

    $this->b = $stocks::where('category', '=', 'b')
        ->pluck('qty')
        ->sum();

    // Return optional
    return $this;
}

So you can access $this->a and $this->b as properties of the class whenever class is in use.

In My case

public function sideBar($id)
{
    $className = category::where('id', '=', $id)->first(['c_name','parent_id']);
    $subName = category::where('id', '=', $className->parent_id)->first(['c_name']);
    return [$className, $subName];
}

than call it sideBar function

 public function show($id)
{
    //
    list($className,$subName) = $this->sideBar($id);
    return view('Dashboard.user.cha_wiseView',['className' => $className->c_name,'subName' => $subName->c_name]);

}
Related