How can I optimize a consultation in Laravel?

Asked

Viewed 58 times

0

I have several linked tables and when I do a query it returns all query:

Model:

class Apontamento extends Model
{

  public function user()
  {
    return $this->hasOne(\App\User::class, 'id', 'user_id');
  }

  public function cliente()
  {
    return $this->hasOne(\App\Cliente::class, 'id', 'cliente_id');
  }
  public function servico()
  {
    return $this->hasOne(\App\Servico::class, 'id', 'servico_id');
  }
  public function participante()
  {
    return $this->hasMany(\App\RelacaoAp::class);
  }
  public function kanban()
  {
    return $this->hasMany(\App\Kanban::class, 'id', 'kanban_id');
   
  }
  public function descricaoAp()
  {
    return $this->hasMany(\App\Descricao_ap::class, 'apontamentodesc_id', 'id');
  }
}

Eloquent that I use very little, I make little changes "almost never": User, Servico, Cliente, what I could do not to be carrying all the time, I’ve tried with Cache::remember(); but nothing!

Controller:

 public function index(Request $request)
  {
    $getCliente = $request->cliente;
    $getAtividade = $request->atividade;
    $getResponsavel = $request->responsavel;
    $getDataF = implode('-', array_reverse(explode('/', $request->dataF)));

    $qbuilder = new Apontamento();

    if ($qbuilder) {
      if (!empty($getCliente)) {
        $qbuilder = Apontamento::whereHas('cliente', function ($query) use ($getCliente) {
          $query->where('razao', 'LIKE', '%' . $getCliente . '%');
        });
      }
      if (!empty($getAtividade)) {
        $qbuilder = Apontamento::where('atividade', 'LIKE', '%' . $getAtividade . '%');
      }
      if (!empty($getResponsavel)) {
        $qbuilder = Apontamento::whereHas('user', function ($query) use ($getResponsavel) {
          $query->where('name', 'LIKE', '%' . $getResponsavel . '%');
        });
      }
      if (!empty($getDataF)) {
        $qbuilder = Apontamento::whereHas('descricaoAp', function ($query) use ($getDataF) {
          $query->whereRaw('DATE(apontamentoData) = ?', [$getDataF]);
        });
      }
    }

    $apontamentos = $qbuilder->orderBy('dataApontamento', 'DESC')->paginate(10);


    $clientes = Cliente::orderBy('CodCliente', 'asc')->get();
    $servicos = Servico::all();
    $partici = User::whereHas('roles', function ($query) {
      $query->where("name", "equipe")->orWhere("name", "admin");
    })->get();

    return view(
      'webadmin.apontamento.listar',
      compact('clientes', 'servicos', 'apontamentos', 'partici', 'totalHrs')
    );
  }

How can I optimize, to avoid the repetition of this part in Controller, I must create in the model or in which folder?

  • I really don’t understand your question and your problem.

  • @Jedsonmelo, I’m sorry if I wasn’t clear, I’m suffering with slowness and code repetition

  • $qbuilder = new Apontamento(); then you do it again with a search and if you are satisfied by if will be done again. If you are using completely wrong because you are repeating at all times the same class. Slowness is a case by case how many records you return?

No answers

Browser other questions tagged

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