You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 12, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+16-44Lines changed: 16 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,12 @@
1
-

1
+
# Laravel Loop Functions
2
+
[](https://packagist.org/packages/michael-rubel/laravel-loop-functions)
[](https://packagist.org/packages/michael-rubel/laravel-model-mapper)
Assign Eloquent model attributes to class properties:
31
30
```php
32
-
$this->mapModelAttributes($model);
31
+
$this->attributesToProperties($model);
33
32
```
34
33
35
-
This is especially handy with Livewire components when you want to map your model data to the view.
36
-
37
-
For example:
34
+
Assign array key values to class properties:
38
35
```php
39
-
class CompanyProfile extends Component
40
-
{
41
-
use WithModelMapping;
42
-
43
-
/**
44
-
* Frontend properties.
45
-
*
46
-
* @var string|null
47
-
*/
48
-
public ?string $name = null;
49
-
public ?string $tax_number = null;
50
-
public ?string $address = null;
51
-
52
-
/**
53
-
* @param \App\Models\Company $company
54
-
*
55
-
* @return void
56
-
*/
57
-
public function mount(Company $company): void
58
-
{
59
-
$this->mapModelAttributes($company);
60
-
}
61
-
}
36
+
$this->arrayToProperties($data);
62
37
```
63
38
64
-
## Why?
65
-
Why should I use it like this instead of just passing the model to the view? It's because the models are huge objects and you probably shouldn't expose them to the frontend for security and performance reasons. Another thing is primitive view variables are highly customizable through view composers, while it's harder to decorate in the case of using models.
66
-
67
39
## Logging
68
40
As the package doesn't throw an error in case of failed assignment (for example type incompatibility), you may wish to log such an event. The package has failed assignment logging disabled by default, but you can turn it on by publishing the config:
0 commit comments