eloquence
eloquence copied to clipboard
Mappable + Multiple Wheres
When you try to use mappable and logical grouping the mappable fields won't work.
https://laravel.com/docs/8.x/queries#logical-grouping
class MyModel {
use Eloquence, Mappable;
protected $maps = [
'id' => 'myId',
'field1' => 'myField1',
'field2' => 'myField2',
'is_enabled' => 'enabled'
];
}
$this->queryBuilder = MyModel::query();
$this->queryBuilder->where('is_enabled', 1);
$this->queryBuilder->where(function ($query) use ($searchCriteria) {
$text = '%'.Arr::pull($searchCriteria, 'text').'%';
return $query->where('field1', 'LIKE', $text)
->orWhere('field2', 'LIKE', $text);
});
$this->queryBuilder->get();
Error: SQLSTATE[42S22]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'field1'.
If I try to play a bit things are getting worse.
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Must specify table to select from. (SQL: select top 20 * from my_model where [enabled] = 1 and (select * where [field1] LIKE %asse% or [field2] LIKE %asse%) is null)
code
$productModelCollects = ProductModel
::where
(
function($query) use ($prefix)
{
foreach( $prefix as $i => $v )
{
$query->orWhere("prod_name", "like", "{$v}%");
}
}
)
->whereIn("prod_id", ["1"])
->get();
error
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1096 No tables used (SQL: select * from product where (select * where prod_name like 衣服% or prod_name like 禮物%) is null and prod_id in (1) and product.deleted_at is null)