1. Go to this page and download the library: Download nejcc/laravel-querylayer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nejcc / laravel-querylayer example snippets
use Nejcc\LaravelQuerylayer\Facades\LaravelQuerylayer;
use App\Models\User;
// Get a repository instance for the User model
// The same instance will be returned for subsequent calls with the same model
$userRepository = LaravelQuerylayer::repository(User::class);
// Basic operations
$users = $userRepository->where(['is_active' => true])->paginate(15);
$user = $userRepository->find(1);
$activeUsers = $userRepository->where(['is_active' => true])->get();
// Pagination
$paginatedUsers = $userRepository->paginate();
// Create and update
$newUser = $userRepository->create([
'name' => 'John Doe',
'email' => '[email protected]'
]);
$userRepository->update(1, ['name' => 'Jane Doe']);
// Delete
$userRepository->delete(1);
// Eager loading relationships
$userWithPosts = $userRepository->with('posts')->find(1);
// Soft deletes - if your model uses SoftDeletes
$trashedUser = $userRepository->withTrashed()->find(5);
$onlyTrashedUsers = $userRepository->onlyTrashed()->paginate(10);
$userRepository->restore(1);
$userRepository->forceDelete(1);
// Transactions
$userRepository->transaction(function () use ($userRepository) {
// Execute multiple operations in a transaction
$user = $userRepository->create(['name' => 'Transaction User']);
$userRepository->update($user->id, ['name' => 'Updated User']);
return $user;
});
// Or use the convenient methods
$user = $userRepository->createOrFail(['name' => 'Safe Create']);
$success = $userRepository->updateOrFail(1, ['name' => 'Safe Update']);
use Nejcc\LaravelQuerylayer\Repositories\BaseRepository;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
class UserRepository extends BaseRepository
{
protected function model(): string
{
return User::class;
}
/**
* Get active admin users with pagination
*/
public function getActiveAdminsPaginated(?int $perPage = null): LengthAwarePaginator
{
return $this->getActiveAdminsQuery()->paginate($perPage);
}
/**
* Get query builder for active admin users
*/
public function getActiveAdminsQuery(): Builder
{
return $this->query()
->where('is_active', true)
->where('role', 'admin');
}
}
// Chain methods for a single query
$adminUsers = $userRepository
->with('posts')
->where(['role' => 'admin'])
->paginate(15); // Pagination instead of all() for large datasets
// For limited dataset, you can use get()
$recentPosts = $postRepository
->query()
->orderBy('created_at', 'desc')
->limit(5)
->get();
// Query scopes are automatically reset after execution
// So this query will NOT
// Get raw database query builder for direct table operations
$usersTable = $userRepository->db();
// Perform complex SQL operations
$activeUsersByRole = $userRepository->db()
->select('role', $userRepository->db()->raw('COUNT(*) as count'))
->where('is_active', true)
->groupBy('role')
->orderBy('count', 'desc')
->get();
// Complex joins and aggregates
$userStats = $userRepository->db()
->select(
'users.id',
'users.name',
$userRepository->db()->raw('COUNT(posts.id) as total_posts'),
$userRepository->db()->raw('SUM(CASE WHEN posts.is_published = 1 THEN 1 ELSE 0 END) as published_posts')
)
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->groupBy('users.id', 'users.name')
->having('total_posts', '>', 0)
->orderBy('total_posts', 'desc')
->get();
// Useful for complex search functionality
$query = $userRepository->db();
if ($request->has('search')) {
$query->where('name', 'like', "%{$request->search}%");
}
if ($request->has('role')) {
$query->where('role', $request->role);
}
$results = $query->orderBy('name')->get();
// Direct database updates or bulk operations
$affected = $userRepository->db()
->whereIn('id', $userIds)
->update(['is_active' => false]);
use Nejcc\LaravelQuerylayer\Contracts\ModelInterface;
use Nejcc\LaravelQuerylayer\Traits\HasRepository;
class User extends Model implements ModelInterface
{
use HasRepository;
protected static function getRepositoryClass(): string
{
return UserRepository::class;
}
}
// Create a new user
$user = User::createRecord([
'name' => 'John Doe',
'email' => '[email protected]'
]);
// Find a user
$user = User::findById(1);
$user = User::findByColumn('email', '[email protected]');
// Get all users
$users = User::getAll();
// Get paginated users
$users = User::getPaginated(10);
// Get users with conditions
$users = User::getWhere(['role' => 'admin']);
// Update a user
User::updateRecord(1, ['name' => 'Jane Doe']);
// Delete a user
User::deleteRecord(1);
// Get the query builder
$query = User::getQuery();
// Get the repository instance
$repository = User::getRepository();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.