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();
I’ll head tomorrow. Testing is hurting a lot lately.
– Diego Souza
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.
– Diego Souza
There is such a command: whereDoesnHave ?
– Diego Souza
You can take the ID column or a variable that has the ID value for the Model ?
– Diego Souza
Only if the query has already been performed. Even within a non-static method in the model you use
$this->getAttribute('id')– Wallace Maxters
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.– Diego Souza
This query is used by
Laravel. Either you accept it or you’ll have to keep working on it.– Wallace Maxters
Got it. It’s just that it’s hard to do this without putting SELECT in the view.
– Diego Souza
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.
– Diego Souza