Share variable in Laravel controller

1

I am doing an intranet for my work, it is a production control (We are a printing press, the platform is to know in which department each work is at each moment). The platform consists of different phases (departments), jobs, clients and workers.

So far so good.

In the section see phase, I show the total of works that are in that phase, with the following function:

public function numberOfWorksForCategories($faseToShow)
{
    $count = count($faseToShow->works);
    foreach ($faseToShow->fases as $fase){
        foreach($fase->works as $work){
            $count = $count + count($work);
        }
    }

    return $count;
}

The function that shows the view is as follows:

public function show($id)
{
    $fases = Fase::orderBy("orden")->get();
    $faseToShow = Fase::find($id);
    $idFase = $this->idFase;
    $totalFases = $this->numberOfWorksForCategories($faseToShow);
    $orderType = Input::get("orderType");
    $orderBy = Input::get("orderBy");
    if (!$orderType) {
        $orderType = "desc";
    }
    if (!$orderBy) {
        $orderBy = "id";
    }
    return view("fase", compact("fases", "faseToShow", "orderBy", "orderType", "totalFases","totaltoShow","totalWorks"));
}

The problem I have is the following. In the code shown above I get the id of the phase via GET, but on the left side of the page I have a list where the phases appear (menu type), and I would like to see the total of works in each of the phases.

Then, according to my logic, I have done the following: - I created a function similar to the previous one shown

protected $idFase;
public function numberOfWorksLeft($idFase){
    $this->idFase =$idFase;
   $count = count($idFase->works);
    foreach ($idFase->fases as $fase){
        foreach ($fase->works as $work){
            $count = $count + count($work);
        }
    }
    return $count;
}

that id phase was passed through the template:

@foreach($fases as $fase)
        <li class="mb-1" title="{{ $fase->descripcion }}"><a
                href="{{ route("fase", $fase->id) }}">{{ $fase->nombre  }}</a> <small>({{$TotalLeft($fase->id)}} Jobs)</small></li>
        @if(count($fase->fases) > 0)
            @foreach($fase->fases->sortBy('orden') as $fase2)
                <li class="mb-1 second-level" title="{{ $fase2->descripcion }}">> <a
                            href="{{ route("fase", $fase2->id) }}">{{ $fase2->nombre  }}</a></li>
            @endforeach
        @endif
    @endforeach

and in the show I include the view variable:

public function show($id)
{
    $fases = Fase::orderBy("orden")->get();
    $faseToShow = Fase::find($id);
    $idFase = $this->idFase;
    $TotalLeft = $this->numberOfWorksLeft($idFase);
    $totalFases = $this->numberOfWorksForCategories($faseToShow);
    $orderType = Input::get("orderType");
    $orderBy = Input::get("orderBy");
    if (!$orderType) {
        $orderType = "desc";
    }
    if (!$orderBy) {
        $orderBy = "id";
    }
    return view("fase", compact("fases", "faseToShow",'TotalLeft', "orderBy", "orderType", "totalFases","totaltoShow","totalWorks"));
}

I get an error "Undefined variable: TotalLeft"

What am I doing wrong?

    
asked by Adrián 11.11.2018 в 16:21
source

0 answers