How to do a like research in Aravel 5.5 using eloquent relationship?

Asked

Viewed 450 times

1

I need to do a survey using like in a model that has relationship with another table(equipment). What has happened is that it is bringing all the records of the ordemservic table with the null related table (equipment) and in one of the records comes the related equipment. You’re not getting Where. Can you help me? I was clear?

Look at:

    $pesquisa = $dataForm['pesquisa'];
    //dd($pesquisa);
    $data = Ordemservico::with([
        'equipamento' => function ($query) use ($pesquisa) {
            $query->where('codigoequipamento', 'LIKE', '%117%');
        },
    ])->get();
    dd($data);

I left the 117 fixed but still brings all the work orders!.

Thank you

  • Put the example of the two tables and their respective Model

  • $fillable = ['codigoequipamento', 'descricaoequipamento', 'centrocusto_id', 'tipoequipamento_id', 
 'tipogas_id', 'local_id', 'tipolocal_id', 'fornecedor_id', 'fabricante_id', 'modelo', 'patrimonio', 'numeroserie', 
 'dataaquisicao', 'notafical', 'valoraquisicao', 'dataagarantia', 'tag', 'btu', 'tr', 'hp', 'active']; public Function Rules($id = ') { Return [ 'codigoequipamento' => 'required|min:3|max:25', 'descricaoequipamento' => 'required|min:1|max:190', 'tag' => 'required|min:1|max:190',

  • class Ordemservico extends Model {
 protected $fillable = ['user_id', 'colaborador_id', 'centrocusto_id', 'local_id', 'tiposolicitacao_id', 'dataplanejamento',
 'dataencerramento', 'tiposervico_id', 'checklist_id', 'descricaopadrao', 'prioridade_id', 'equipe1', 'equipe2', 
 'equipment_id', 'equipe_id', 'descricaoservico', 'dataultimaexecucao', 'periodicity', 'frequencia_id', 'impedimento_id', 'dataexecucao', 'attachment'];

  • $search = $dataForm['search']; //dd($search); $data = Ordemservico::whereHas([ 'equipment' => Function ($query) use ($search) { $query->Where('codigoequipamento', 'LIKE', "%$search%"); }, 'local', 'typosolicitacao', ])->get();

  • in your question! edit your question and put it all there!

  • 1

    maybe here lives the problem $query->where('codigoequipamento', 'LIKE', '%117%'); because this filter, this codeequipment is the key to the relationship?

Show 1 more comment

1 answer

1


In your case, you should use whereHas and not with.

There’s a difference between the two:

  • whereHas -> You use when you want the result of your query to be changed by the condition passed in the relationship.

  • with -> It is used to load relationships in advance. When using an anonymous function, returns the relationships according to the query passed within it.

Change your query to:

OrdemServico::whereHas('equipamento', static function ($query) {
    $query->where('codigoequipamento', 'LIKE', '%117%');
})
->get();
  • is giving an error: "strpos() expects Parameter 1 to be string, array Given" ([ 'equipment' => Static Function ($query) use ($search) { $query->Where('codigoequipamento', 'LIKE', "%$pesquisa%"); }, 'local', 'typosolicitacao', ])->get(); ?

  • 1

    I don’t understand how this relates to the question that was asked earlier. Please, if another problem has arisen, consider asking another question.

  • When I switched to whereHas I gave this error: "strpos() expects Parameter 1 to be string, array Given"

  • 1

    You switched to whereHas or did you do as is in the code of my answer? The whereHas waiting string in the first parameter, and not a array

  • I just switched to whereHas Yes, I took the array with the other relationships, left only the relationship with equipment and worked but need to put the other relationships. If it cannot be in an array, how should I put?

  • 1

    @Marcosbirro looks at the answer, man. I put it the way you should :\

  • You told me to do like this: Ordemservico::whereHas('equipment', Static Function($query) { $query->Where('codigoequipamento', 'LIKE', '%117%'); }) ->get(); I did but need the other relationships. How should I put the other relationships? Now $search = $dataForm['search']; ///dd($search); $ordemservicos = Ordemservico::whereHas( 'equipment', Static Function ($query) use ($search) { $query->Where('codigoequipamento', 'LIKE', "%$pesquisa%"); })->get(); This is how it works

  • 1

    Like I said: whereHas affects the consultation according to the condition of the relationship, already the with carries the relationships. You can use the two together

  • 1

    Perfect. It worked as I wanted. Thank you.

Show 4 more comments

Browser other questions tagged

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