Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Zaheer's avatar

How to do this query in eloquent

Can someone convert this mysql query to Laravel eloquent :

SELECT * FROM journal where code in (select code from accounts where type=1 or type=3);

I tried this but it gives me "Call to undefined method App\Journal::code()"

$rslt = Journal::where('journal.branchid', $branch_id)
     ->whereHas('code', function($query) {
       $query->where('code', '=', \Accounts::input('code')->whereIn('type',[1,3]));})
     ->where(function($q) {$q->where('journal.cancel','!=',1)->orWhereNull('journal.cancel');})
     ->select('code',DB::raw('sum(IFNULL(dr,0) - IFNULL(cr,0)) as total'))->groupBy('code')->Get('code','total');
0 likes
2 replies
edoc's avatar
edoc
Best Answer
Level 24

what about this?

Journal::whereIn('code', function ($q) {
    $q->select('code')->from('accounts')->where('type', 1)->orWhere('type', 3);
})->get();

Please or to participate in this conversation.