Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Accessible Laravel Package

delormejonathan/accessible

Accessible is a PHP library that uses docblock annotations to automate class behavior: generates getters/setters, validates setter arguments with Symfony Assert, initializes properties in constructors, and manages collections and associations with add/remove helpers.

View on GitHub
Deep Wiki
Context7

How to manage associations

Accessible can also manage the relations between your classes. This feature resides in the two following annotation:

  • [@Referenced](https://github.com/Referenced): Indicates that the associated object (or objects if the property is a collection) has a reference to the current object.
  • [@InCollection](https://github.com/InCollection): Indicates that the associated object (or objects if the property is a collection) has a collection in which the current object resides.

Here is a simple example of how it can be used:

use Accessible\Annotation\Inverted;

class Student
{
  /**
   * [@Access](https://github.com/Access)({Access:GET, Access::SET})
   * [@Referenced](https://github.com/Referenced)(className=Bag::class, propertyName="owner")
   */
  private $bag;
}

class Bag
{
  /**
   * [@Access](https://github.com/Access)({Access:GET, Access::SET})
   */
  private $owner;
}

$student = new Student();
$bag = new Bag();

$student->setBag($bag);
$bag->getStudent(); // -> $student

In this example, when a new value to Student#bag, the property Bag#owner of the previous bag will be set to null, and to $student in the new bag.

If you add another [@Referenced](https://github.com/Referenced) annotation in Bag#owner, modifying this property will also modify Student#bag.

Note that if you have a Many to One or a Many to Many relationship, the properties defined as collections must have a [@ListBehavior](https://github.com/ListBehavior) or a [@SetBehavior](https://github.com/SetBehavior) annotation.

Types of associations

This section gives examples of how you can manage the relations between your classes, following the relationship vocabulary you can find with Doctrine.

One to One relationship

In this example, a student has a bag, and a bag belongs to a student.

use Accessible\Annotation\Inverted;

class Student
{
  /**
   * [@Access](https://github.com/Access)({Access:GET, Access::SET})
   * [@Referenced](https://github.com/Referenced)(className=Bag::class, propertyName="owner")
   */
  private $bag;
}

class Bag
{
  /**
   * [@Access](https://github.com/Access)({Access:GET, Access::SET})
   * [@Referenced](https://github.com/Referenced)(className=Student::class, propertyName="bag")
   */
  private $owner;
}

$student = new Student();
$bag = new Bag();

$student->setBag($bag);
$bag->getOwner(); // -> $student

$bag->setOwner(new Student());
$student->getBag(); // -> null

One to Many / Many to One relationship

In this example, a student has several books, and a book belongs to a student.

use Accessible\Annotation\Inverted;
use Accessible\Annotation\Mapped;

class Student
{
  /**
   * [@Access](https://github.com/Access)({Access:GET, Access::SET})
   * [@ListBehavior](https://github.com/ListBehavior)
   * [@Referenced](https://github.com/Referenced)(className=Book::class, propertyName="owner")
   * [@Initialize](https://github.com/Initialize)({})
   */
  private $books;
}

class Book
{
  /**
   * [@Access](https://github.com/Access)({Access:GET, Access::SET})
   * [@InCollection](https://github.com/InCollection)(className=Student::class, propertyName="books")
   */
  private $owner;
}

$student = new Student();
$book = new Book();

$student->addBook($book);
$book->getOwner(); // -> $student

$book->setOwner(new Student());
$student->getBag(); // -> []

Many to Many relationship

In this example, a student has several teachers, and a teacher teaches to several students.

use Accessible\Annotation\Mapped;

class Student
{
  /**
   * [@Access](https://github.com/Access)({Access:GET, Access::SET})
   * [@SetBehavior](https://github.com/SetBehavior)
   * [@InCollection](https://github.com/InCollection)(className=Teacher::class, propertyName="students")
   * [@Initialize](https://github.com/Initialize)({})
   */
  private $teachers;
}

class Teacher
{
  /**
   * [@Access](https://github.com/Access)({Access:GET, Access::SET})
   * [@SetBehavior](https://github.com/SetBehavior)
   * [@InCollection](https://github.com/InCollection)(className=Student::class, propertyName="teachers")
   * [@Initialize](https://github.com/Initialize)({})
   */
  private $students;
}

$student = new Student();
$teacher = new Teacher();

$student->addTeacher($teacher);
$teacher->getStudents(); // -> [$student]

$teacher->removeStudent($student);
$student->getTeachers(); // -> []

Update an associated object manually

If you want to manage your class associations manually, you can use the updatePropertyAssociation() method. Here is an example of how to use it:

class Student
{
  /**
   * [@Access](https://github.com/Access)({Access:GET, Access::SET})
   * [@Referenced](https://github.com/Referenced)(className=Bag::class, propertyName="owner")
   */
  private $bag;

  public function setBag(Bag $bag)
  {
    $valuesToUpdate = [
      'oldValue' => $this->bag,
      'newValue' => $bag
    ];
    $this->bag = $bag;
    $this->updatePropertyAssociation('bag', $valuesToUpdate);
  }
}

Note that $valuesToUpdate could also only be composed of one of these two values (and could even be empty).

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
roundly-consulting/kubernetes-api-for-laravel
roundly-consulting/plausible-for-laravel
gvp/gvp-laravel-template
metasyncsite/laravel-metasync-client
roundly-consulting/onboarding-for-laravel
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo