How to reuse parts of Eloquent builder in Laravel

The solution for “How to reuse parts of Eloquent builder in Laravel” can be found here. The following code will assist you in solving the problem.

$shared = TableA::join(‘tableb’, ‘tablea.bid’, ‘=’, tableb.id)
->join(‘talbec’, ‘tableb.cid’, ‘=’, tablec.id)
->join(‘tabled’, ‘tablec.did’, ‘=’, tabled.id);

$a = $shared->clone()->where(‘tablea_col’, ‘=’, true)->get();

$b = shared->clone()->where(‘tableb_col’, ‘=’, true)->get();
before Laravel 8, use

$a = clone $shared;
$b = clone $shared;
$a = $a->where(‘tablea_col’, ‘=’, true)->get();
$b = $b->where(‘tableb_col’, ‘=’, true)->get();

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in