Hola tengo estas dos tablas relacionadas y esta funcion que me devuelve los campos que necesito de las tablas como puedo hacer una busqueda filtrada por uno o mas campos? he intentado hacerlo con el orwhere pero no funciona
public function index(Request $request)
{
$name = $request->get('first_name');
$lastname = $request->get('last_name');
$dni = $request->get('document_number');
$cellphone = $request->get('cellphone');
$domicile = $request->get('domicile');
$cdcontract = $request->get('id');
$name = 'agu';
$contracts = Contract::join('affiliates', 'affiliates.contract_id', '=', 'contracts.id')
->orwhere('affiliates.first_name', 'like','%'. $name .'%')
->orwhere('affiliates.last_name', 'like','%'. $lastname .'%')
->orwhere('contracts.cellphone', 'like','%'. $cellphone .'%')
->orwhere('contracts.domicile', 'like','%'. $domicile .'%')
->orwhere('contracts.document_number', $dni)
->orwhere('contracts.id', 'like','%'. $cdcontract .'%')
->get();
return $contracts->map(function ($contract) {
return [
'id' => $contract->id,
'number' => $contract->full_number,
'holder_last_name' => $contract->holder->last_name,
'holder_first_name' => $contract->holder->first_name,
'domicile' => $contract->domicile,
'phone' => $contract->phone,
'cellphone' => $contract->cellphone,
'branch' => $contract->branch->name,
'current_fee' => $contract->current_fee,
];
})->toArray();
}
este error me aparece en el storage/log/laravel.log
local.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contracts.document_number' in 'where clause' (SQL: select * from contracts
inner join affiliates
on affiliates
.contract_id
= contracts
.id
where (affiliates
.first_name
like %agu% or affiliates
.last_name
like %% or contracts
.cellphone
like %% or contracts
.domicile
like %% or contracts
.document_number
is null or contracts
.id
like %%) and contracts
.deleted_at
is null) {"userId":1,"exception":"[object] (Illuminate\Database\QueryException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contracts.document_number' in 'where clause' (SQL: select * from contracts
inner join affiliates
on affiliates
.contract_id
= contracts
.id
where (affiliates
.first_name
like %agu% or affiliates
.last_name
like %% or contracts
.cellphone
like %% or contracts
.domicile
like %% or contracts
.document_number
is null or contracts
.id
like %%) and contracts
.deleted_at
is null) at C:\xampp\htdocs\osdam - copia\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664, Doctrine\DBAL\Driver\PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contracts.document_number' in 'where clause' at C:\xampp\htdocs\osdam - copia\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:63, PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contracts.document_number' in 'where clause' at C:\xampp\htdocs\osdam - copia\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:61)
[stacktrace]
codigo
y no como imagen. También recuerda que si dices "no funciona", debes aclarar qué es lo que no funciona : - si da un error, mostrar el error. - si no obtienes el resultado esperado, cual es el resultado obtenido/storage/logs/laravel.log
de tu proyecto.