Laravel Clone or Reuse Query

Published on Oct 20, 2022

Example of Laravel clone or reuse query.

Code

// 1. query to get all users
$users = User::query();

// 2. filter by date
$users->whereDate('created_at', Carbon::today());

// 3. verified and not verified users
$verified_users = $users->clone()->where('verified', true)->get();
$not_verified_users = $users->clone()->where('verified', false)->get();

return $verified_users;

Video

Wath on YouTube: https://youtu.be/TtIBV_5If3Q