Download the PHP package codewiser/belongs-to-many without Composer

On this page you can find all versions of the php package codewiser/belongs-to-many. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package belongs-to-many

Extended BelongsToMany

A BelongsToMany relation connects two models through a pivot table. In simple cases, the pivot table holds just two columns — the foreign keys. In more complex cases, it holds additional columns that you want to use to constrain the relation.

The problem: when you use the whereHas method on a BelongsToMany relation, the callback receives a Builder instance instead of the Relation instance, so you can't use the wherePivot* methods.

Take a look at the difference. Here we get a Relation instance:

But in whereHas, we get a Builder instance:

Here we're forced to use fully qualified column names to avoid ambiguity.

The solution is to hand the Relation instance to the callback, which is what the HasPivot trait does for custom builders.

With the trait, all *has* builder methods — whereHas, whereDoesntHave, etc. — pass the relation object to the callback instead of a Builder instance, so you can use any wherePivot* method to constrain the intermediate query.

The trait supports relations of all types — BelongsTo, HasMany, BelongsToMany, MorphTo, etc. The relation is handed over only when the callback explicitly declares a type accepting it; otherwise the callback keeps the default Builder instance.

Implementation

Give model a custom builder and apply the HasPivot trait to it:

What the callback receives

Now the whereHas callback may receive the relation object — depending on the type you require for its first parameter. The argument is replaced only when that type accepts the relation instance, so an untyped callback keeps the Builder.

Passing the relation is safe, because it forwards calls to the underlying Builder. Require a plain Builder, or no type at all, and you get the default (and backward compatible) behaviour:

Custom Intermediate Table Builder

If you define a custom intermediate table model, it has a query builder of its own. The extended BelongsToMany object provides a way to access it via the pivot method — a macro registered by this package's PivotServiceProvider:

Since Laravel v13.26.0 added closure support to wherePivot, the pivot method is deprecated:

Notes

Everything above applies to relations of all types: BelongsTo, HasOne, HasMany, HasOneThrough, HasManyThrough, BelongsToMany, MorphToMany (and its inverse), MorphMany, MorphOne and MorphTo.

Handing the relation over to the constraint closure is not a new idea: Builder::with() and Model::load() already do it — their closures receive the relation instance, which forwards calls to the underlying Builder. whereHas() now behaves the same way:


All versions of belongs-to-many with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
laravel/framework Version >=11.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package codewiser/belongs-to-many contains the following files

Loading the files please wait ...