Skip to content

[12.x] Add Eloquent Relationship Attributes #56415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes\Relations;

use Attribute;
use Illuminate\Support\Str;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class BelongsTo implements RelationAttribute
{
use HasArguments;

/**
* @var class-string
*/
public string $related;

/**
* @var array<string>
*/
public array $arguments = [];

private ?string $name;

/**
* @param class-string $related
* @param array<string> ...$arguments
*/
public function __construct(string $related, ?string $name = null, string ...$arguments)
{
$this->related = $related;
$this->name = $name;
$this->arguments = [$related, ...$arguments];

$this->arguments = array_pad($this->arguments, 4, null);

if ($this->arguments[1] === null) {
$this->arguments[1] = Str::snake(class_basename($this->related)).'_id';
}

if ($this->arguments[2] === null) {
$this->arguments[2] = 'id';
}

$this->arguments[3] = $this->relationName();
}

public function relationName(): string
{
return $this->name ?? Str::singular(Str::camel(class_basename($this->related)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes\Relations;

use Attribute;
use Illuminate\Support\Str;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class BelongsToMany implements RelationAttribute
{
use HasArguments;

/**
* @var class-string
*/
public string $related;

/**
* @var array<string>
*/
public array $arguments = [];

private ?string $name;

/**
* @param class-string $related
* @param array<string> ...$arguments
*/
public function __construct(string $related, ?string $name = null, string ...$arguments)
{
$this->related = $related;
$this->name = $name;
$this->arguments = [$related, ...$arguments];
}

public function relationName(): string
{
return $this->name ?? Str::plural(Str::camel(class_basename($this->related)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes\Relations;

/**
* @internal
*/
trait HasArguments
{
/**
* @return array<mixed>
*/
public function relationArguments(): array
{
return $this->arguments;
}
}
40 changes: 40 additions & 0 deletions src/Illuminate/Database/Eloquent/Attributes/Relations/HasMany.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes\Relations;

use Attribute;
use Illuminate\Support\Str;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class HasMany implements RelationAttribute
{
use HasArguments;

/**
* @var class-string
*/
public string $related;

/**
* @var array<string>
*/
public array $arguments = [];

private ?string $name;

/**
* @param class-string $related
* @param array<string> ...$arguments
*/
public function __construct(string $related, ?string $name = null, string ...$arguments)
{
$this->related = $related;
$this->name = $name;
$this->arguments = [$related, ...$arguments];
}

public function relationName(): string
{
return $this->name ?? Str::plural(Str::camel(class_basename($this->related)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes\Relations;

use Attribute;
use Illuminate\Support\Str;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class HasManyThrough implements RelationAttribute
{
use HasArguments;

/**
* @var class-string
*/
public string $related;

/**
* @var class-string
*/
public string $through;

/**
* @var array<string|class-string>
*/
public array $arguments = [];

private ?string $name;

/**
* @param class-string $related
* @param class-string $through
* @param array<string> ...$arguments
*/
public function __construct(string $related, string $through, ?string $name = null, string ...$arguments)
{
$this->related = $related;
$this->through = $through;
$this->name = $name;
$this->arguments = [$related, $through, ...$arguments];
}

public function relationName(): string
{
return $this->name ?? Str::plural(Str::camel(class_basename($this->related)));
}
}
40 changes: 40 additions & 0 deletions src/Illuminate/Database/Eloquent/Attributes/Relations/HasOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes\Relations;

use Attribute;
use Illuminate\Support\Str;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class HasOne implements RelationAttribute
{
use HasArguments;

/**
* @var class-string
*/
public string $related;

/**
* @var array<string>
*/
public array $arguments = [];

private ?string $name;

/**
* @param class-string $related
* @param array<string> ...$arguments
*/
public function __construct(string $related, ?string $name = null, string ...$arguments)
{
$this->related = $related;
$this->name = $name;
$this->arguments = [$related, ...$arguments];
}

public function relationName(): string
{
return $this->name ?? Str::singular(Str::camel(class_basename($this->related)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes\Relations;

use Attribute;
use Illuminate\Support\Str;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class HasOneThrough implements RelationAttribute
{
use HasArguments;

/**
* @var class-string
*/
public string $related;

/**
* @var class-string
*/
public string $through;

/**
* @var array<string|class-string>
*/
public array $arguments = [];

private ?string $name;

/**
* @param class-string $related
* @param class-string $through
* @param array<string> ...$arguments
*/
public function __construct(string $related, string $through, ?string $name = null, string ...$arguments)
{
$this->related = $related;
$this->through = $through;
$this->name = $name;
$this->arguments = [$related, $through, ...$arguments];
}

public function relationName(): string
{
return $this->name ?? Str::singular(Str::camel(class_basename($this->related)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes\Relations;

use Attribute;
use Illuminate\Support\Str;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class MorphMany implements RelationAttribute
{
use HasArguments;

/**
* @var class-string
*/
public string $related;

public string $morphName;

/**
* @var array<string|class-string>
*/
public array $arguments = [];

private ?string $name;

/**
* @param class-string $related
* @param array<string> ...$arguments
*/
public function __construct(string $related, string $morphName, ?string $name = null, string ...$arguments)
{
$this->related = $related;
$this->morphName = $morphName;
$this->name = $name;
$this->arguments = [$related, $morphName, ...$arguments];
}

public function relationName(): string
{
return $this->name ?? Str::plural(Str::camel(class_basename($this->related)));
}
}
43 changes: 43 additions & 0 deletions src/Illuminate/Database/Eloquent/Attributes/Relations/MorphOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Illuminate\Database\Eloquent\Attributes\Relations;

use Attribute;
use Illuminate\Support\Str;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
final class MorphOne implements RelationAttribute
{
use HasArguments;

/**
* @var class-string
*/
public string $related;

public string $morphName;

/**
* @var array<string|class-string>
*/
public array $arguments = [];

private ?string $name;

/**
* @param class-string $related
* @param array<string> ...$arguments
*/
public function __construct(string $related, string $morphName, ?string $name = null, string ...$arguments)
{
$this->related = $related;
$this->morphName = $morphName;
$this->name = $name;
$this->arguments = [$related, $morphName, ...$arguments];
}

public function relationName(): string
{
return $this->name ?? Str::singular(Str::camel(class_basename($this->related)));
}
}
Loading