0
votes

when querying, I wanted to use store_id instead of nd_001_store_id. I get the error as below production.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'store_id' in 'where clause'

$user_store_id = Session::get('user_store_id'); //for example 6
$a = "2020-06-20 13:00:00";
$b = "2020-07-05 13:00:00";
$sales_types = ['retail', 'wholesale'];
$invoice_items_stock_ids = InvoiceItems::select(
    'nd_001_stock_id AS stock_id',
    'nd_000_store_id AS store_id',
    DB::raw("CONCAT(add_date_store, ' ', add_hours_store) AS date_range")
)->where(function ($query) use ($user_store_id) {
    $query->where('store_id', '=', $user_store_id);
})->orWhere(function ($query) use ($a, $b) {
    $query->where('date_range', '>=', $a)
            ->where('date_range', '<=', $b);
})
->whereIn('type', $sales_types)->get();

date_range and store_id Column not found error

Thanks, Best Regards

1
date_range and store_id Columns are in InvoiceItems table?OMR
Your where query is looking for the store_id column. You should be able to query the nd_000_store_id directly. ->where('nd_000_store_id', $user_store_id).LLai
@OMR no. I used 'nd_000_store_id AS store_id', and DB::raw("CONCAT(add_date_store, ' ', add_hours_store) AS date_range"). There are nd_000_store_id, add_date_store, add_hours_store Columns in tablehakkidilek

1 Answers

0
votes

please try whereRaw

$invoice_items_stock_ids = InvoiceItems::select(
    'nd_001_stock_id AS stock_id',
    'nd_000_store_id AS store_id',
    DB::raw("CONCAT(add_date_store, ' ', add_hours_store) AS date_range")
)->where(function ($query) use ($user_store_id) {
    $query->whereRaw('store_id', '=', $user_store_id);
})->orWhere(function ($query) use ($a, $b) {
    $query->whereRaw('date_range', '>=', $a)
            ->whereRaw('date_range', '<=', $b);
})
->whereIn('type', $sales_types)->get();