mirror of
https://github.com/ambieco/laravel-lift.git
synced 2026-05-01 04:02:57 +08:00
Merge branch 'main' into fix-belongsto
This commit is contained in:
@@ -20,15 +20,15 @@ final class BelongsTo implements RelationAttribute
|
||||
public string $relationClass;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, array ...$arguments)
|
||||
public function __construct(string $relationClass, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->arguments = [$relationClass, ...$arguments];
|
||||
|
||||
@@ -20,15 +20,15 @@ final class BelongsToMany implements RelationAttribute
|
||||
public string $relationClass;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, array ...$arguments)
|
||||
public function __construct(string $relationClass, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->arguments = [$relationClass, ...$arguments];
|
||||
|
||||
@@ -20,15 +20,15 @@ final class HasMany implements RelationAttribute
|
||||
public string $relationClass;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, array ...$arguments)
|
||||
public function __construct(string $relationClass, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->arguments = [$relationClass, ...$arguments];
|
||||
|
||||
@@ -25,16 +25,16 @@ final class HasManyThrough implements RelationAttribute
|
||||
public string $throughClass;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string|class-string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param class-string $throughClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, string $throughClass, array ...$arguments)
|
||||
public function __construct(string $relationClass, string $throughClass, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->throughClass = $throughClass;
|
||||
|
||||
@@ -20,15 +20,15 @@ final class HasOne implements RelationAttribute
|
||||
public string $relationClass;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, array ...$arguments)
|
||||
public function __construct(string $relationClass, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->arguments = [$relationClass, ...$arguments];
|
||||
|
||||
@@ -25,16 +25,16 @@ final class HasOneThrough implements RelationAttribute
|
||||
public string $throughClass;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string|class-string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param class-string $throughClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, string $throughClass, array ...$arguments)
|
||||
public function __construct(string $relationClass, string $throughClass, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->throughClass = $throughClass;
|
||||
|
||||
@@ -22,15 +22,15 @@ final class MorphMany implements RelationAttribute
|
||||
public string $morphName;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string|class-string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, string $morphName, array ...$arguments)
|
||||
public function __construct(string $relationClass, string $morphName, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->morphName = $morphName;
|
||||
|
||||
@@ -22,15 +22,15 @@ final class MorphOne implements RelationAttribute
|
||||
public string $morphName;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string|class-string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, string $morphName, array ...$arguments)
|
||||
public function __construct(string $relationClass, string $morphName, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->morphName = $morphName;
|
||||
|
||||
@@ -16,14 +16,14 @@ final class MorphTo implements RelationAttribute
|
||||
public string $morphName;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string|class-string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $morphName, array ...$arguments)
|
||||
public function __construct(string $morphName, string ...$arguments)
|
||||
{
|
||||
$this->morphName = $morphName;
|
||||
$this->arguments = [$morphName, ...$arguments];
|
||||
|
||||
@@ -22,15 +22,15 @@ final class MorphToMany implements RelationAttribute
|
||||
public string $morphName;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string|class-string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, string $morphName, array ...$arguments)
|
||||
public function __construct(string $relationClass, string $morphName, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->morphName = $morphName;
|
||||
|
||||
@@ -22,15 +22,15 @@ final class MorphedByMany implements RelationAttribute
|
||||
public string $morphName;
|
||||
|
||||
/**
|
||||
* @var array<mixed>
|
||||
* @var array<string|class-string>
|
||||
*/
|
||||
public array $arguments = [];
|
||||
|
||||
/**
|
||||
* @param class-string $relationClass
|
||||
* @param array<mixed> ...$arguments
|
||||
* @param array<string> ...$arguments
|
||||
*/
|
||||
public function __construct(string $relationClass, string $morphName, array ...$arguments)
|
||||
public function __construct(string $relationClass, string $morphName, string ...$arguments)
|
||||
{
|
||||
$this->relationClass = $relationClass;
|
||||
$this->morphName = $morphName;
|
||||
|
||||
@@ -7,10 +7,12 @@ namespace WendellAdriel\Lift\Tests\Datasets;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use WendellAdriel\Lift\Attributes\PrimaryKey;
|
||||
use WendellAdriel\Lift\Attributes\Relations\BelongsTo;
|
||||
use WendellAdriel\Lift\Attributes\Relations\HasMany;
|
||||
use WendellAdriel\Lift\Attributes\Rules;
|
||||
use WendellAdriel\Lift\Lift;
|
||||
|
||||
#[BelongsTo(BookCase::class)]
|
||||
#[HasMany(Price::class, 'custom_id', 'id')]
|
||||
class Book extends Model
|
||||
{
|
||||
use Lift;
|
||||
|
||||
27
tests/Datasets/Price.php
Normal file
27
tests/Datasets/Price.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WendellAdriel\Lift\Tests\Datasets;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use WendellAdriel\Lift\Attributes\PrimaryKey;
|
||||
use WendellAdriel\Lift\Attributes\Relations\BelongsTo;
|
||||
use WendellAdriel\Lift\Attributes\Rules;
|
||||
use WendellAdriel\Lift\Lift;
|
||||
|
||||
#[BelongsTo(Book::class, 'custom_id', 'id')]
|
||||
class Price extends Model
|
||||
{
|
||||
use Lift;
|
||||
|
||||
#[PrimaryKey]
|
||||
public int $id;
|
||||
|
||||
#[Rules(['required', 'decimal:2'])]
|
||||
public float $price;
|
||||
|
||||
protected $fillable = [
|
||||
'price',
|
||||
];
|
||||
}
|
||||
@@ -12,6 +12,7 @@ use WendellAdriel\Lift\Tests\Datasets\LibraryBook;
|
||||
use WendellAdriel\Lift\Tests\Datasets\Manufacturer;
|
||||
use WendellAdriel\Lift\Tests\Datasets\Phone;
|
||||
use WendellAdriel\Lift\Tests\Datasets\Post;
|
||||
use WendellAdriel\Lift\Tests\Datasets\Price;
|
||||
use WendellAdriel\Lift\Tests\Datasets\Role;
|
||||
use WendellAdriel\Lift\Tests\Datasets\Seller;
|
||||
use WendellAdriel\Lift\Tests\Datasets\Tag;
|
||||
@@ -295,3 +296,25 @@ it('will not add unnecessary keys', function () {
|
||||
expect($workBook->save())->toBeTrue()
|
||||
->and($libraryBook->save())->toBeTrue();
|
||||
});
|
||||
|
||||
it('loads a relation with arguments', function () {
|
||||
$book = Book::create([
|
||||
'name' => fake()->name,
|
||||
]);
|
||||
|
||||
$price = $book->prices()->create([
|
||||
'price' => fake()->randomFloat(2),
|
||||
]);
|
||||
|
||||
expect($book->prices)->toHaveCount(1)
|
||||
->and($book->prices->first()->id)->toBe($price->id)
|
||||
->and($price->book->id)->toBe($book->id)
|
||||
->and($price->custom_id)->toBe($book->id);
|
||||
|
||||
$book = Book::query()->find($book->id);
|
||||
expect($book->prices)->toHaveCount(1)
|
||||
->and($book->prices->first()->id)->toBe($price->id);
|
||||
|
||||
$price = Price::query()->find($price->id);
|
||||
expect($price->book->id)->toBe($price->id);
|
||||
});
|
||||
|
||||
@@ -153,6 +153,11 @@ abstract class TestCase extends BaseTestCase
|
||||
$table->foreignId('user_id')->nullable()->constrained();
|
||||
$table->foreignId('book_case_id')->nullable()->constrained();
|
||||
$table->string('name');
|
||||
|
||||
Schema::create('prices', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('custom_id')->nullable()->constrained();
|
||||
$table->decimal('price');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user