We help IT Professionals succeed at work.
Get Started

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

Eduardo Fuerte
on
118 Views
Last Modified: 2020-02-07
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();

Open in new window


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);
  }

}

Open in new window



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

Thanks in advance!
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2020
Most Valuable Expert 2018
Commented:
This problem has been solved!
Unlock 1 Answer and 23 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE