1
I’m doing a search taking a piece of word and playing a search like LIKE
for fields fields. Use the Eloquent
and relationships, but does not bring the data.
{
$pesquisa = $dataForm['pesquisa'];
$atestados = Atestado::with([
'colaborador' => function ($query) use ($pesquisa) {
$query->where('colaboradors.matricula', 'LIKE', "%{$pesquisa}%")
->orWhere('colaboradors.nome', 'LIKE', "%{$pesquisa}%");
},
'cid' => function ($query) use ($pesquisa) {
$query->orWhere('cids.codigo', 'LIKE', "%{$pesquisa}%");
},
'situacao' => function ($query) use ($pesquisa) {
$query->orWhere('situacaos.descricao', 'LIKE', "%{$pesquisa}%");
},
])
->orderBy('data_afastamento', 'desc')
->get();
}
You’re probably not satisfying so you don’t come back. Tell me you’re not returning what? explains better, a result you should trim, something like ...
– novic
Virgil, I put a part plate in the field search which is 9-7
– Marcos Birro
I will paste here the result dd. In case brought all the records being that should bring only one that has this condition.
– Marcos Birro
{Collection {#368 ▼
 #items: array:28 [▼
 0 => Atestado {#378 ▶}
 1 => Atestado {#379 ▶}
 2 => Atestado {#380 ▶}
 3 => Atestado {#381 ▶}
 4 => Atestado {#382 ▶}
 5 => Atestado {#383 ▶}
 6 => Atestado {#384 ▶}
 7 => Atestado {#385 ▶}
 8 => Atestado {#386 ▶}
 9 => Atestado {#387 ▶}
 10 => Atestado {#388 ▶}
 11 => Atestado {#389 ▶}
 12 => Atestado {#390 ▶}
 13 => Atestado {#391 ▶}
 14 => Atestado {#392 ▶}
 ]
}}
– Marcos Birro
You’re only supposed to bring this one. { 0 => Attested {#378 #Relations: array:3 [ "collaborator" => Contributor {#377 #fillable: array:7 [ ] "id" => 2 "centrocusto_id" => 1 "cargo_id" => 1 "matricula" => "45839-7" "name" => "João Carlos dos Santos" ] } "Cid" => Cid {#367 } "situation" => Situation {#371 } ] } 7 and brought all the records.
– Marcos Birro
Should only bring
0 => Atestado
does not work like this, load ratios only appear when the main has data! maybe you need to do it separately, I still don’t understand the purpose. try to explain in your question that I can help you– novic
Good, is a system that registers medical certificates. (absenteeism). Then I have a certificate table that has data (relationships) with collaborator, Cid (medical clearance code) and situation (status type away, working inss). The filter aims to take from a search field and pass to a select by doing like of the matricula, name of the contributor. This contributor table is related to the attested table. I thought of doing it separately using Join. Maybe I have more control. What do you think?
– Marcos Birro
instead of get I used toSql() to view select and came {"select * from
atestados
order bydata_afastamento
desc"} It is as if eloquent did not take the query variable and pass the content to select. This part is not working. { $search = $dataForm['search']; $attestations = Attestation::with([ 'collaborator' => Function ($query) use ($search) { $query->Where('colaboradors.matricula', 'LIKE', "%{$search}%") ->orWhere('colaboradors.nome', 'LIKE', "%{$pesquisa}%"); }, }– Marcos Birro
If you’re looking to get all the vouchers?
– novic
Yes. The certificate that corresponds to the part of the registration or name of the collaborator. That comes from the dataForm search as put in the previous comment. That’s all.
– Marcos Birro
Let’s see if I understand, you want to bring only the attestations according to the research of the relationship, then the technique is another,
$posts = App\Post::whereHas('comments', function ($query) {
 $query->where('content', 'like', 'foo%');
})->get();
that’s how– novic
this way look at the link: https://laravel.com/docs/5.7/eloquent-relationships#querying-Relationship-Existence and you beyond said can bring relationships
– novic