Francisunoxx Francisunoxx - 1 year ago 94
PHP Question

What's the cause of Call to undefined method Illuminate\Database\Query\Expression::whereRaw()?

I'm trying to perform a query where I need to count

column with a value of 1 in my
. Here how I did it in the SQL query.

SELECT, documents.isApprove,
(SELECT COUNT(*) FROM approvals_document WHERE approvals_document.isApprove = 1 and approvals_document.document_id = as approvedBy
FROM documents;


id | isApprove | approvedBy
96 0 3

When I'm performing this in Laravel using
expression. It throws me a error.

Call to undefined method Illuminate\Database\Query\Expression::whereRaw()

Here how I performed it in Laravel way.

public function count()
$count = DB::table('documents')
->select('', 'documents.isApprove', DB::raw('COUNT(FROM approvals_document) as approvedBy')
->whereRaw('approvals_document.isApprove = 1 and approvals_document.document_id ='))

Any help how did I get this problem? Any help would appreciated!

Update: I followed aynber tips using left join but I got this error.


Answer Source

You have your parenthesis mixed up. You need an extra one after your select, not after your whereRaw:

$count = DB::table('documents')
->select('', 'documents.isApprove', DB::raw('COUNT(approvals_document.*) as approvedBy')) // Close your DB::raw and your select here
->whereRaw('approvals_document.isApprove = 1 and approvals_document.document_id =')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download