temporary-relationship-trait-for-laravel

给时间时间,让过去过去。
This trait provides a temporary($relationship) method on your model for you to access relationships without adding them to the list of loaded relationships.

If the relationship is already loaded, it is returned directly. If the relationship is not loaded, it will be loaded, removed from the list of loaded relationships, and then returned.

The function is wrapped in Spatie’s once package to add memoization. This allows the same temporary relationship to be called multiple times without multiple DB queries.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

namespace App\Models\Traits;

trait TemporaryRelationships
{
/**
* Returns the value of the given relationship from the current model.
*
* If the relation is already loaded, it is returned directly.
*
* If the relation is not loaded, it is loaded, then removed
* from the list of loaded relations, and then returned.
*
* The function is memoized so accessing the same temporary
* relation within a request will only make one query.
*/
public function temporary(string $relation)
{
return once(function () use ($relation) {
if ($this->relationLoaded($relation)) {
return $this->{$relation};
}

$relationValue = $this->getRelationValue($relation);
$this->unsetRelation($relation);

return $relationValue;
});
}
}

Use it within a model:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

namespace App\Models;

use App\Models\Traits\TemporaryRelations;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use TemporaryRelations;

public function someMethod()
{
return $this->temporary('author');
}

public function author()
{
return $this->belongsTo(User::class);
}
}

https://owenconti.com/posts/temporary-relationship-trait-for-laravel