PHPackages                             jedrzej/withable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jedrzej/withable

ActiveLibrary[Database &amp; ORM](/categories/database)

jedrzej/withable
================

Withable trait for Laravel's Eloquent models

0.0.6(6y ago)25203.4k↓57%3[3 issues](https://github.com/jedrzej/withable/issues)[1 PRs](https://github.com/jedrzej/withable/pulls)2MITPHP

Since Aug 17Pushed 3y ago2 watchersCompare

[ Source](https://github.com/jedrzej/withable)[ Packagist](https://packagist.org/packages/jedrzej/withable)[ Docs](https://github.com/jedrzej/withable)[ RSS](/packages/jedrzej-withable/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (5)Versions (7)Used By (2)

Withable trait for Laravel's Eloquent models
============================================

[](#withable-trait-for-laravels-eloquent-models)

This package adds possibility to dynamically eager load Eloquent models relations in Laravel 4/5/6 using request parameters.

You could also find those packages useful:

- [Searchable](https://github.com/jedrzej/searchable) - Allows filtering your models using request parameters
- [Sortable](https://github.com/jedrzej/sortable) - Allows sorting your models using request parameters
- [Pimpable](https://github.com/jedrzej/pimpable) - A meta package that combines Sortable, Searchable and Withable behaviours

Composer install
----------------

[](#composer-install)

Add the following line to `composer.json` file in your project:

```
"jedrzej/withable": "0.0.6"

```

or run the following in the commandline in your project's root folder:

```
composer require "jedrzej/withable" "0.0.6"

```

Setting up withable models
--------------------------

[](#setting-up-withable-models)

In order to make an Eloquent model withable, add the trait to the model and define a list of relations that can be eagerly loaded using request paramaters. You can either define a `$withable` property or implement a `getWithableRelations` method if you want to execute some logic to define list loadable relations.

```
use Jedrzej\Withable\WithableTrait;

class Post extends Eloquent
{
	use WithableTrait;

	// either a property holding a list of loadable relations...
	protected $withable = ['owner', 'forum'];

	// ...or a method that returns a list of loadable relations
	protected function getWithableRelations()
	{
		return ['owner', 'forum'];
	}
}
```

In order to make all relations loadable put an asterisk `*` in the list:

```
protected $withable = ['*'];
```

Loading relations
-----------------

[](#loading-relations)

`WithableTrait` adds a `withRelations()` scope to the model - you can pass it a list of relations to load as you would pass to Eloquent's `with()` method:

```
// return all posts with the user who created them and forum where they were posted
Post::withRelations(['owner', 'forum'])->get();
// return all posts with the user who created them
Post::withRelations('owner')->get();
```

or it will use `Request::all()` as default. In the URL you can pass a list of relations to load or a single relation in the `with` parameter:

```
// return all posts with the user who created them and forum where they were posted by appending to the URL
?with[]=owner&with[]=forum
// return all posts with the user who created them
?with=owner
//and then calling
Post::withRelations()->get();
```

Loading scoped relations
------------------------

[](#loading-scoped-relations)

If you have defined local scopes in a related model, you can load this relation with the scope applied. E.g. in order to eagerly load posts with their approved comments:

```
// define local scope in Comment model
public function scopeApproved($query) {
  $query->whereIsApproved(true);
}

// append the following to the URL in order to load "comments" relation with "approved" local scope applied
?with=comments:approved

// load the posts in your controller
Post::withRelations()->get();
```

Additional configuration
------------------------

[](#additional-configuration)

If you are using `with` request parameter for other purpose, you can change the name of the parameter that will be interpreted as a list of relations to load by setting a `$withParameterName` property in your model, e.g.:

```
protected $withParameterName = 'relations';
```

If you need to execute additional logic to define a list of relations to load (e.g. permission checking), you can implement `getWithRelationsList()` method in your model and make that return list of relations:

```
public function getWithRelationsList() {
  return Request::get('relations');
}
```

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity43

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~296 days

Recently: every ~369 days

Total

6

Last Release

2493d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/291904?v=4)[Jędrzej Kuryło](/maintainers/jedrzej)[@jedrzej](https://github.com/jedrzej)

---

Top Contributors

[![jedrzej](https://avatars.githubusercontent.com/u/291904?v=4)](https://github.com/jedrzej "jedrzej (11 commits)")[![jedrzej-x-team-com](https://avatars.githubusercontent.com/u/120025182?v=4)](https://github.com/jedrzej-x-team-com "jedrzej-x-team-com (1 commits)")[![pablomaurer](https://avatars.githubusercontent.com/u/2413156?v=4)](https://github.com/pablomaurer "pablomaurer (1 commits)")

---

Tags

eager-loadingeloquentlaravelphprequestapilaraveleloquentloadeager

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/jedrzej-withable/health.svg)

```
[![Health](https://phpackages.com/badges/jedrzej-withable/health.svg)](https://phpackages.com/packages/jedrzej-withable)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.4M96](/packages/mongodb-laravel-mongodb)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k32.6M46](/packages/kirschbaum-development-eloquent-power-joins)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[jedrzej/pimpable

Laravel 4/5/6 package that allows to dynamically filter, sort and eager load relations for your models using request parameters

105188.2k1](/packages/jedrzej-pimpable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
