YEAR(begin_date) unknown column in laravel - php

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();

Related

Error in Laravel pagination with complex select statement

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?

How to write mysql query in laravel 5.4?

I am new to laravel and i want to write mysql query in laravel 5.4.
query is like :
note: avoid column names..
SELECT *
FROM (SELECT DISTINCT *
FROM messages
WHERE (from_id=1 OR to_id=1)
ORDER BY created DESC) as m
GROUP BY `from_id`
I tried but gives error.
$messages = DB::table('messagetbl')
->select('*')
->Where(function($query) use ($userid){
$query->distinct()
->where('senderid',$userid)
->orWhere('receiverid',$userid)
->orderBy('datetime','desc');
})
->groupBy('senderid')
->get();
Error:
SQLSTATE[42000]: Syntax error or access violation: 1055
Expression #1 of SELECT list is not in GROUP BY clause and contains
nonaggregated column 'db.messagetbl.id' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by (SQL: select * from messagetbl where
(senderid = 8 or receiverid = 8) group by senderid)
Thanks in advance.
I believe something like this:
DB::table('messages')
->select('*')
->where('form_id', '=', 1)
->orWhere('to_id', '=', 1)
->orderBy('created', 'desc')
->groupBy('form_id')
->distinct()
->get();

Laravel query builder: error in mysql where clause

Here is my join query
$data['userslist'] = DB::table('users')
->select('users.*','user_roles.role_name')
->join('user_roles','users.user_type=user_roles.role_id')
->where('users.user_type','!=',1)
->get();
But i will get the following error in where clause
check the manual that corresponds to your MariaDB server version for
the right syntax to use near 'where `users`.`user_type` != ?' at
line 1 (SQL: select `users`.*, `user_roles`.`role_name` from `users`
inner join `user_roles` on `users`.`user_type=user_roles`.`role_id`
where users.user_type != 1)
You try to fetch users.user_type where value in not equal to 1. For this, you need to use '<>' in where clause
Now your query is as follow :
$data['userslist'] = DB::table('users')
->select('users.*','user_roles.role_name')
->join('user_roles', 'users.user_type', '=', 'user_roles.role_id')
->where('users.user_type','<>',1)
->get();
I hope you will get your solution.

Column not found: 1054 Unknown column - Laravel 5.1 Join

I have three tables. work_orders, customers, and aircraft. In the work_orders table there are two fields customer_id and aircraft_id. I'm trying to retrieve the customers and aircraft data through the use of join. I am getting an array of errors, but they all seem to be pointed in the same direction and that is that the table cannot be found.
Here is my WorkOrderController index:
public function index(WorkOrder $workorder)
{
$workorder_array = $workorder
->join('work_orders as work', 'work.aircraft_id', '=', 'aircraft.id')
->join('work_orders as workorder', 'workorder.customer_id', '=', 'customers.id')
->select('work_orders.opened_date', 'customers.mobile', 'aircraft.year')
->get();
return view('work-orders.index', compact('workorder_array'));
}
And with this I get the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'aircraft.id' in 'on clause' (SQL: select work_orders.opened_date from work_orders inner join work_orders as work on aircraft.id = work.aircraft_id inner join work_orders as workorder on workorder.customer_id = customers.id)
I've tried switching work.aircraft_id with aircraft.id, because the Laravel Docs show it in that order, but that doesn't make any difference either. The only way I can get rid of this error is to remove my join statements.
I had the wrong table in the join statement. Here's what I should have had:
public function index(WorkOrder $workorder)
{
$workorder_array = $workorder
->join('aircraft', 'work_orders.aircraft_id', '=', 'aircraft.id')
->join('customers', 'work_orders.customer_id', '=', 'customers.id')
->select('*')
->get();
return view('work-orders.index', compact('workorder_array'));
}

Laravel Query Builder join doesn't affect query

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.

Categories