someonefamous/laravel-findby
Add dynamic finders to Eloquent models: call User::findByLastName('Smith') or User::findAllByFirstName('Bob') instead of where()->first()/get(). Works with any snake_cased field name via the FindBy trait.
composer require someonefamous/laravel-findby
use SomeoneFamous\FindBy\Traits\FindBy;
class User extends Model
{
use FindBy;
}
$user = User::findByLastName('Smith'); // Single record
$users = User::findAllByFirstName('Bob'); // Collection
Replace repetitive where()->first() or where()->get() calls with cleaner syntax:
// Before
$admin = User::where('role', 'admin')->where('status', 'active')->first();
// After
$admin = User::findByRole('admin')->findByStatus('active');
Chaining Methods:
$user = User::findByEmail('john@example.com')
->findByStatus('active')
->findByRole('admin');
Note: Each findBy* call adds a where clause.
Dynamic Field Lookups:
$field = 'last_name';
$value = 'Smith';
$user = User::findBy($field, $value); // Equivalent to `findByLastName`
Bulk Operations:
$bobs = User::findAllByFirstName('Bob'); // Collection
$activeUsers = User::findAllByStatus('active'); // Collection
Integration with Existing Queries:
$query = User::where('deleted_at', null);
$query->findByLastName('Smith'); // Appends `where('last_name', 'Smith')`
findById, findByEmail). Avoid overusing for complex queries.findBy* over findAllBy* when you expect a single result (returns null if not found).orWhere for conditional logic:
User::findByLastName('Smith')->orWhere('first_name', 'John');
Namespace Changes (v2.0.0+):
use SomeoneFamous\FindBy\FindBy; will fail. Update to:
use SomeoneFamous\FindBy\Traits\FindBy;
Method Name Collisions:
findBySomething, the trait’s dynamic method will override it. Rename your method or use the explicit findBy($field, $value) syntax.Case Sensitivity:
findByLastName → last_name). Ensure your database columns match this convention.Performance:
findBy* call adds a where clause. For large datasets, chain methods carefully to avoid excessive query building:
// Inefficient (multiple queries if not optimized)
User::findByLastName('Smith')->findByFirstName('John');
// Better (single query)
User::where('last_name', 'Smith')->where('first_name', 'John')->first();
Check Generated SQL: Use Laravel’s query logging to verify the trait’s behavior:
DB::enableQueryLog();
$user = User::findByLastName('Smith');
dd(DB::getQueryLog());
Override Dynamic Methods: If the trait’s dynamic method generation conflicts with your needs, disable it by adding this to your model:
protected $disableFindByDynamicMethods = true;
Then manually define methods like:
public function scopeFindByCustomField($query, $value)
{
return $query->where('custom_field', $value);
}
Custom Scope Support: Extend the trait to support custom scopes:
// In your model
public function scopeFindByCustomScope($query, $value)
{
return $query->where('custom_column', $value)->orWhere('other_column', $value);
}
Modify Query Logic:
Override the findBy method in your model to add logic:
public function findBy($field, $value)
{
return $this->newQuery()->where($field, $value)->firstOrFail();
}
Add Validation: Validate input before querying:
public function findByEmail($email)
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Invalid email');
}
return $this->newQuery()->where('email', $email)->first();
}
How can I help you explore Laravel packages today?