use 'where' with 'like' in an eloquent realation

1

hello I have two models Movement and Account

In the Movement model I have the following relationship.

public function cuenta()
{
    return $this->hasOne(Cuenta::class);
}

What I am looking for is to bring all the Movements with their Account relation, in the consultation with Account they must comply with the name of the bank, and the name of the holder.

The detail is in the holder of the account, the 'LIKE' and the wild cards of the last WHERE does not work and does not find any co-residence.

$movimientos = Movimiento::where('fecha', $fecha)
->where('status','NUEVO')->with('cuenta')->get()
->where('cuenta.banco', $banco)
->where('cuenta.titular','LIKE','%'.$titular.'%');

If I remove the 'like' and the wildcards and pass the variable $ holder as it is in the database if you return the corresponding records.

$movimientos = Movimiento::where('fecha', $fecha)
->where('status','NUEVO')->with('cuenta')->get()
->where('cuenta.banco', $banco)
->where('cuenta.titular', $titular);
    
asked by gonzalezZ 03.09.2018 в 01:04
source

1 answer

1

The with function can contain an array as a parameter, this array can contain a callback where you should make queries for your account , as follows:

$movimientos = Movimiento::where('fecha', $fecha)
->where('status','NUEVO')->with(['cuenta'=>function($query) use ($banco,$titular){
    $query->where('banco', $banco)->where('titular','LIKE','%'.$titular.'%');
}])->get();

That way your query should work.

    
answered by 03.09.2018 в 02:43