I have the problem with pagination in laravel framework.
I built my query in this way:
$auctions = Auction::where('status', '=', 1)
->Where('minQuantity', '>', 0)
->whereRaw('endTime > NOW()');
$auctions->selectRaw('*,' . DB::raw('IF ((SELECT COUNT(*) FROM observed_auctions WHERE observed_auctions.idUser=' . intval($userId) . ' AND observed_auctions.idAuction=auctions.id)>0,\'true\',\'false\') as isObserved'));
Next, I get the result:
$auctions->paginate(15, ['name'], 'page', $currentPage);
It works pretty well until I want to find only observed auctions if I added where clause:
$auctions->where('isObserved', '=', true);
I get error:
Column not found: 1054 Unknown column 'isObserved' in 'where clause' (SQL: select count(*) as aggregate from auctions where status = 1 and minQuantity > 0 and endTime > NOW() and isObserved = 1)
How can I solve this problem?
Related
I am new to laravel.
I have three tables in my DB
dsp_account with fields id,name_display,short_name.
dsp with fields id,dsp_account_id.
.
rtbcfg_region_dsp with fields dsp_id,status.
.
dsp table connected to dsp_account table via dsp_account_id field.
dsp table connected to rtbcfg_region_dsp via dsp_id.
I try to create this sql query:
select distinct dsp_account.id,dsp_account.name_display,dsp_account.short_name
from dsp_account
inner join dsp on dsp.dsp_account_id = dsp_account.id
AND dsp.id
IN(SELECT dsp_id
FROM rtbcfg_region_dsp
group by dsp_id, status
having count(*)>0 and status = 1) order by dsp_account.name_display ASC;
this is my code:
$all_list6 = DspAccount::select('DspAccount.id', 'DspAccount.name_display','DspAccount.short_name')
->join('dsp', 'dsp.dsp_account_id', '=', 'dspAccount.id')
->whereIn('DSP.id',function($query)
{
$query->select('dsp_id')
->from('rtbcfg_region_dsp')
->groupBy('dsp_id','status')
->havingRaw('COUNT(*) > 0 AND status = 1');
})
->get();
this is the error that I got:
what I am missing:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DspAccount.id' in 'field list' (SQL: select `DspAccount`.`id`, `DspAccount`.`name_display`, `DspAccount`.`short_name` from `dsp_account` inner join `dsp` on `dsp`.`dsp_account_id` = `dspAccount`.`id` where `DSP`.`id` in (select `dsp_id` from `rtbcfg_region_dsp` group by `dsp_id`, `status` having COUNT(*) > 0 AND status = 1) and `dsp_account`.`deleted_at` is null)
$all_list6 = DspAccount::select('dsp_account.id', 'dsp_account.name_display','dsp_account.short_name')
->join('dsp', 'dsp.dsp_account_id', '=', 'dsp_account.id')
->whereIn('dsp.id',function($query)
{
$query->select('dsp_id')
->from('rtbcfg_region_dsp')
->groupBy('dsp_id','status')
->havingRaw('COUNT(*) > 0 AND status = 1');
})
->distinct()->get();
You should use original table name dsp_account instead of DspAccount as it is model class name. I haven't debug the query but i answered about only syntax error. Thanks:)
SELECT COUNT(*) as count, MONTH(begin_date)
FROM `events`
WHERE (YEAR(begin_date) = YEAR(CURDATE()))
OR (YEAR(begin_date) = YEAR(CURDATE()) + 1)
GROUP BY MONTH(begin_date)
Here is sql query, i want to write it in laravel eloquent.
what i try:
$oncoming_events = DB::table('events')
->select(DB::raw('count(*) as numOfOncomingEvents, MONTH(begin_date)'))
->where('YEAR(begin_date)', '=', 'YEAR(CURDATE())')
->orWhere('YEAR(begin_date)', '=', 'YEAR(CURDATE()) +1')
->groupBy('MONTH(begin_date)')->get();
Error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'YEAR(begin_date)' in 'where clause' (SQL: select count(*) as numOfOncomingEvents, MONTH(begin_date) from events where YEAR(begin_date) =
laravel 5.6
btw
sql query works..
You need to use DB::raw() in where to tell the query builder that it is not column name it is data manipulation in query,
$oncoming_events = DB::table('events')->select(DB::raw('count(*) as numOfOncomingEvents, MONTH(begin_date)'))->where(DB::raw('YEAR(begin_date)'), '=', 'YEAR(CURDATE())')->orWhere(DB::raw('YEAR(begin_date)'), '=', 'YEAR(CURDATE()) +1')->groupBy(DB::raw('MONTH(begin_date)'))->get();
I have an eloquent query where one of the orderBy is the difference of two columns.
$mymodel = Level::where([['ColA', 5], ['ColB', 10], ['ColC', 7]])
->orderBy('ColA', 'Desc')
->orderBy('ColA' - 'ColB', 'Desc')
->orderBy('ColC', 'Desc')
->orderBy('ColD', 'Asc')
->pluck('userId')->toArray();
The exact same code on localhost with sqlite works without an error. But on production with MySQL has the following error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'order clause' (SQL: select `userId` from `levels` where (`ColA` = 5 and `ColB` = 10 and `ColC` = 7) order by `ColA` desc, `0` desc, `ColC` desc, `ColD` asc)
$model = Level::where($wheres)
->orderByRaw('(ColA - ColB) DESC')
->pluck('userId')
->toArray();
I'm trying to build in search functionality in my application (Laravel 5.1), but my join doesn't seem to do anything to the resulting query. What am I doing wrong?
Code:
$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);
$query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));
$invoices = $query->paginate(15);
Resulting query:
select count(*) as aggregate from invoice_headers where customer_code_id = 1 and (invoice_types.name <> 380)
Resulting response:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'invoice_types.name' in 'where clause'
This is the query I was hoping to see:
select count(*) as aggregate
from invoice_headers
inner join invoice_types
on invoice_headers.invoice_type_id = invoice_types.id
where customer_code_id = 1
and (invoice_types.name <> 380)
$query = InvoiceHeader::where('customer_code_id', '=', Auth::user()->customer_code_id);
you need to store the query in a variable.
$query = $query->join('invoice_types', 'invoice_headers.invoice_type_id', '=', 'invoice_types.id')
->where('invoice_types.name', '<>', array_search('Faktura', InvoiceHeader::INVOICE_TYPES));
$invoices = $query->paginate(15);
You need to add JOIN with invoice_types table if you want to filter on invoice_types.name.
I'm trying to use a result of subquery in where clause , but I failed
I'm using a laravel db class
this is my code
DB::table('sales as SL')
->join('prsn as PR1','PR1.id_person','=','SL.customer_id')
->join('prsn as PR2','PR2.id_person','=','SL.investor_id')
->select('SL.customer_id' ,'SL.investor_id','SL.type_agd','SL.status','SL.id_agd','PR1.full_name_person as Buyer'
,'PR2.full_name_person as Seller','PR1.mobile1_person as Mobile',
DB::raw('(select SUM(money_operation) from opers where agd_operation = SL.id_agd AND ty IN ("3","4","5","6","7","8","9","10","12") AND status = 1 ) as Madfoo3'),
DB::raw('(select SUM(money) from inst where agd_id = SL.id_agd AND date <= "'.DT::Today('Ymd').'" AND type != "D") as Matloob'),
DB::raw('(select (Matloob - Madfoo3 )) Mutakher' ))
->whereRaw('(Mutakher > 0)')
->paginate();
this error appear:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Mutakher' in 'where clause' (SQL: select count(*) as aggregate from `sales` as `SL` inner join `prsn` as `PR1` on `PR1`.`id_person` = `SL`.`customer_id` inner join `prsn` as `PR2` on `PR2`.`id_person` = `SL`.`investor_id` where (Mutakher > 0))
/var/www/Taqseet/vendor/laravel/framework/src/Illuminate/Database/Connection.php#625
I want to make a condition if the "Mutakher" > 0 give me the raw.
Thanks
I solve this problem
DB::table('sales as SL')
->join('prsn as PR1','PR1.id_person','=','SL.customer_id')
->join('prsn as PR2','PR2.id_person','=','SL.investor_id')
->select('SL.customer_id' ,'SL.investor_id','SL.type_agd','SL.status','SL.id_agd','PR1.full_name_person as Buyer'
,'PR2.full_name_person as Seller','PR1.mobile1_person as Mobile',
DB::raw('(select SUM(money_operation) from opers where agd_operation = SL.id_agd AND ty IN ("3","4","5","6","7","8","9","10","12") AND status = 1 ) as Madfoo3'),
DB::raw('(select SUM(money) from inst where agd_id = SL.id_agd AND date <= "'.DT::Today('Ymd').'" AND type != "D") as Matloob'),
DB::raw('(select (Matloob - Madfoo3 )) Mutakher' ))
->whereRaw('((Matloob - Madfoo3 ) > 0)')
->paginate();