Take All Records and Separate by Status

Asked

Viewed 83 times

2

Dealer.php

class Dealer extends Model
{

    # Relacionamento com Medalhas
    public function dealer_medalhas(){
        return $this->hasMany('App\DealerMedal', 'id_concessionaria');
    }
}

Dealermedal.php

class DealerMedal extends Model
{
    # Relacionamento com Medalhas
    public function dealer(){
        return $this->belongsTo('App\Dealer');
    }
}

I have a relationship I’m in at The Laravel. This consultation brings all the medals that a store has. But I also want to bring the medals that the store does not have in the same query.

And sort by status, like:

SHOP 1 --- MEDAL 1 --- HAS
SHOP 1 --- MEDAL 2 --- HAS
SHOP 1 --- MEDAL 3 --- YOU DO NOT HAVE

The way it is the last line doesn’t come, because of the relationship.

I want you all to come.

The consultation:

# Pesquisar na Base de Dados a Consulta do Usuário
$consulta       = Dealer::whereIdMarca($codMarca)
                  ->whereIdCidade($codCidade)
                  ->where('concessionaria', 'like', '%'.$concessionaria.'%')
                  ->whereStatus(1)
                  ->get();

2 answers

2


Thinking quickly here, I can do this by separating into two queries.

$dealer = Dealer::find($id);

$medalhas_que_tem = $dealer->dealer_medalhas()->get();

$medalhas_nao_tem = DealerMedals::whereDoesntHave('dealer', function ($query) use($id)
{
      $query->where('id', '=', $id);

})->get();

The method get returns the method Illuminate\Support\Collection, which in turn has the merge method.

If you need the two results together, you can do.

$dealers = $medalhas_nao_tem->merge($medalhas_que_tem);
  • I’ll head tomorrow. Testing is hurting a lot lately.

  • That way it won’t work because I don’t know the Dealer ID at the moment. I have a query based on a like.

  • There is such a command: whereDoesnHave ?

  • You can take the ID column or a variable that has the ID value for the Model ?

  • 1

    Only if the query has already been performed. Even within a non-static method in the model you use $this->getAttribute('id')

  • He’s setting up the almost right consultation. select * from medals where **medals.id = 1** and medals.dealer_id is not null and id not in (select id from dealer_medals where id_concessionaria = 1). This part between ** appears automatically, I didn’t want it to appear.

  • This query is used by Laravel. Either you accept it or you’ll have to keep working on it.

  • Got it. It’s just that it’s hard to do this without putting SELECT in the view.

  • Based on the question, not all in itself, your answer is the most correct, as it teaches to rescue related and unrelated data. Therefore, I will mark as the correct answer. Although I have solved in another way, the way I solved does not answer the question I asked.

Show 4 more comments

1

I solved it like that, in logic anyway.

@foreach($res->dealer_medalhas as $item => $key)
    <?php 
        $arrM['id'][$key->medalhas->id]      = $key->medalhas->id;
        $arrM['medalha'][$key->medalhas->id] = $key->medalhas->medalha;
        $arrM['icon'][$key->medalhas->id]    = $key->medalhas->icon;
    ?>
@endforeach
<?php unset($medalhas[0]); ?>
@foreach($medalhas as $foo => $bar)
    @if(in_array($foo, $arrM['id']))
        <div class="badges" data-order="1" data-medalha="{!! $arrM['id'][$foo] !!}">
            {!! Html::image('images/medalhas/'.$arrM['icon'][$foo].'-on.png', $arrM['medalha'][$foo]) !!}
        </div>
    @else
        <?php 
            if($foo == 1)     $icon = 'medalha-melhor-preco-off.png';
            elseif($foo == 2) $icon = 'medalha-melhor-atendimento-off.png';
            elseif($foo == 3) $icon = 'medalha-melhor-servico-off.png';
        ?>
        <div class="badges" data-order="0" data-medalha="{!! $foo !!}">
            {!! Html::image('images/medalhas/'.$icon, $bar) !!}
        </div>
    @endif
@endforeach
  • @Wallacemaxters will be that can give performance problem ?

  • At least SELECT is not in View.

Browser other questions tagged

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