8000 [12.x] Adds `EagerLoad` attribute and `initializeOnQueue` hook by timacdonald · Pull Request #55590 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Adds EagerLoad attribute and initializeOnQueue hook #55590

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 19 commits into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rename to eager load
  • Loading branch information
timacdonald committed Apr 30, 2025
commit 7c7804d7ab76b000c5511f8f2206ea82460342cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
class LoadRelationships
class EagerLoad
{
public function __construct(public array $relations)
{
Expand Down
9 changes: 6 additions & 3 deletions src/Illuminate/Queue/SerializesModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\Attributes\LoadRelationships;
use Illuminate\Queue\Attributes\EagerLoad;
use Illuminate\Queue\Attributes\WithoutRelations;
use ReflectionClass;
use ReflectionProperty;
Expand Down Expand Up @@ -96,8 +96,11 @@ public function __unserialize(array $values)

$property->setValue($this, $value);

if (($value instanceof Model || $value instanceof Collection) && ($attributes = $property->getAttributes(LoadRelationships::class))) {
$relations = $attributes[0]->getArguments()[0];
if (
($value instanceof Model || $value instanceof Collection) &&
($attribute = $property->getAttributes(EagerLoad::class)[0] ?? null)
) {
$relations = $attribute->getArguments()[0];

$value->load($relations);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/Queue/ModelSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Queue\Attributes\LoadRelationships;
use Illuminate\Queue\Attributes\EagerLoad;
use Illuminate\Queue\Attributes\WithoutRelations;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -672,7 +672,7 @@ class ModelSerializationLoadRelationships
use SerializesModels;

public function __construct(
#[LoadRelationships(['roles'])]
#[EagerLoad(['roles'])]
public $property
) {
//
Expand All @@ -685,7 +685,7 @@ class ModelSerializationWithoutRelationsLoadRelationships
use SerializesModels;

public function __construct(
#[LoadRelationships(['lines'])]
#[EagerLoad(['lines'])]
public $property
) {
//
Expand Down
Loading
0