Count in PHP via variable value

Asked

Viewed 77 times

-2

I’m struggling with something relatively simple, but it’s causing me a headache because I always get error.

In an application I need to get the amount of open classes coming from the Crud of Classes. It was to show the number of classes with open status that in this case are 6, but I’m not able to implement this to appear on Dashboard. I can only set the value manually, in this example I put as 40, someone can help me?

Canvas:

Tela para o usuário

And here’s the excerpt from the code:

@extends('layouts.app')

@section('content')
<div class="row">
        <div class="col-lg-6 col-xs-12">
          <!-- small box -->
          <div class="small-box bg-yellow">
            <div class="inner">
              <h3>40</h3>

              <p>Turmas Abertas</p>
            </div>
            <div class="icon">
              <i class="ion ion-person-add"></i>
            </div>
            <a href="treinamentos" class="small-box-footer">Veja Mais <i class="fa fa-arrow-circle-right"></i></a>
          </div>
    </div>

<div class="row">
<div class="col-md-6">
            <div class="panel panel-default">

                <div class="panel-heading">Turmas com status <strong>Aberta</strong></div>

                <div class="panel-body table-responsive">
                    <table class="table table-bordered table-striped ajaxTable">
                        <thead>
                        <tr>
                            <th><center> @lang('Nome do Treinamento')</th> 
                            <th><center> @lang('Data de Inicio')</th> 
                            <th><center> @lang('Data de Conclusão ')</th> 
                            <th><center> @lang('Situação da Turma')</th> 
                            <th>Ações<center>&nbsp;</th>
                        </tr>
                        </thead>
                        @foreach($treinamentos as $treinamento)
                            @if($treinamento->situacao_turma == "Aberta")
                            <tr>

                                <td><center> ? </td>
                                <td><center>{{ $treinamento->data_inicio }} </td> 
                                <td><center>{{ $treinamento->data_conclusao }} </td> 
                                <td><center>{{ $treinamento->situacao_turma  }} </td>
                                <td><center>

                                    @can('treinamento_view')
                                    <a href="{{ route('admin.treinamentos.show',[$treinamento->id]) }}" class="btn btn-xs btn-primary">@lang('quickadmin.qa_view')</a>
                                    @endcan
                                </td>
                            </tr>
                        @endif
                        @endforeach
                    </table>
                </div>
            </div>
</div>

@endsection

Imagery:

inserir a descrição da imagem aqui

CONTROLLER CODE:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $cargos = \App\Cargo::latest()->get(); 
        $setores = \App\Setore::latest()->get(); 
        $turmas = \App\Turma::latest()->get(); 
        $treinamentos = \App\Treinamento::latest()->get(); 

        return view('home', compact( 'cargos', 'setores', 'turmas', 'treinamentos' ));
    }
}
  • I have classes, with open and planned status. I wanted to take only the classes that have open status in the $situaca_turma variable and display the total amount on Dashboard

  • I understand Allan, but do it > print_r($treinamentos) just so I can see this object.

  • Returned on the screen exactly the: 'print_r($trainings)', was this or am I doing something wrong?

  • Try it this way: {{print_r($treinamentos)}}

  • That way he returned me all the records I have registered in my table classes, nor could I post here because exceeds the character limit

  • Put the records in the question, edit it... and put there

  • I edited and placed the records

  • Blz, I’ll take a look

  • I thank you for helping me

  • I think I figured it out. We’re gonna have to test.

  • Massa, I’m on

  • Delete the list you posted!

Show 7 more comments

1 answer

1


After clarification of doubts, I arrived at this answer:

In your Controller you will count to get the total:

public function index()
    {

        $cargos = \App\Cargo::latest()->get(); 
        $setores = \App\Setore::latest()->get(); 
        $turmas = \App\Turma::latest()->get(); 
        $treinamentos = \App\Treinamento::latest()->get(); 
          $total = 0;
          foreach($treinamentos as $treinamento)
            if($treinamento->situacao_turma == "Aberta") $total++;

        return view('home', compact( 'cargos', 'setores', 'turmas', 'treinamentos', 'total' ));
    }

Then just insert it into the view:

<h3>{{ $total }}</h3>

The rest of the code is the same.

  • I thank you for your commitment to help. I will take the tests.

  • 1

    @Allansampaio but don’t take it for granted yet! You have to see if that’s right!

  • Okay, I’ll test and return the feedback

  • 1

    @Allansampaio you need to create this object array before entering the view! using the toArray()

  • I think I got it, I’m gonna apply.

  • 1

    @Allansampaio tries to use in Controller, I think is ideal.

  • I put it in the controller, I’m getting this bug now: Undefined variable: training But it’s become clearer to try to find a solution, keep trying here. Thank you very much.

  • 1

    @Allansampaio you need to use the variable you used to create the object array, understood?

  • 1

    @Allansampaio had a better idea, calm and

  • 1

    @Allansampaio takes the test now. See if it works. I made an edition.

  • I made an edit on my question and added an image of my controller code, that’s correct?

  • 1

    @Allansampaio ... No... Do not use that line. Use only the second part. I made an edit on it that I believe will work.

  • Showed all the records on Dashboard.

  • 1
Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.