troubleshooting Question

Could you point how to adjust this Laravel Controller/ Model code to correctly obtains data from these 02 tables?

Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil asked on
LaravelPHP
23 Comments1 Solution121 ViewsLast Modified:
Hi Experts


Could you point how to adjust this Laravel Controller/ Model code to correctly obtains data from these 02 tables?

Accordingly to:
img004

Actual Controller code:
 $vitrinesX = Vitrine
                ::vigente()
                ->ativa()
                ->aprovada()
                ->global(Auth::user()->tipoparticipante_id)
                ->orderBy('id','desc')
                ->get();

It obtains data just for the table Vitrines.

What is needed is to obtain a sum() of column curtiu at the associated table User_vitrines.

In a way that the resultant query presents the actual data plus two other columns that are  the sum(curtiu=0)  and sum (curtiu=1)

The Model Vitrine code is:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\User;
use Carbon\Carbon;

class Vitrine extends Model
{
  public function tipoparticipante(){
      return $this->belongsTo('App\Models\Tipoparticipante');
  }

  public function criador(){
    return $this->belongsTo('App\User', 'criador_id', 'id');
  }

  public function aprovador(){
    return $this->belongsTo('App\User','aprovador_id');
  }

  public function scopeTitulo($query,$value){
    if ($value===''){
      return $query;
    } else {
      return $query->where('titulo','like','%'.$value.'%');
    }
  }

  public function scopeConteudo($query,$value){
    if ($value===''){
      return $query;
    } else {
      return $query->where('conteudo','like','%'.$value.'%');
    }
  }

  public function scopeStatus($query,$value){
    if ($value==''){
      return $query;
    } else {
      return $query->where('ativo', $value);
    }
  }

  public function scopeAtiva($query){
    return $query->where('ativo', 1);
  }

  public function scopeAprovada($query){
    return $query->where('aprovada', 1);
  }

  public function scopeGlobal($query,$value){
    if ($value===''){
      return $query->whereNull('tipoParticipante_id');
    } else {
      return $query->whereNull('tipoParticipante_id')->orWhere('tipoParticipante_id',$value);
    }
  }

  public function scopePerfil($query,$value){
    if ($value==='0'){
      return $query->whereNull('tipoParticipante_id');
    } elseif ($value=='') {
      return $query;
    } else {
      return $query->where('tipoParticipante_id',$value);
    }
  }

  public function scopeAprovacao($query,$value){
    if($value===''){
      return $query;
    } else {
      return $query->where('aprovada',$value);
    }
  }

  public function scopeCriador($query,$value){
    if($value===''){
      return $query;
    } else {
      return $query->where('criador_id',$value);
    }
  }

  public function scopeAprovador($query,$value){
    if($value===''){
      return $query;
    } else {
      return $query->where('aprovador_id',$value);
    }
  }

  public function scopeVigente($query){
    $agora = Carbon::now();
    $agora = Carbon::create($agora->year, $agora->month, $agora->day,23,59,59);
    return $query->where('vigencia_inicio','<=',$agora)->where('vigencia_termino','>=',$agora);
  }

}


I guess a new method must be coded here to have the desired sum()

Thanks in advance!
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 23 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros