File tree Expand file tree Collapse file tree 1 file changed +87
-0
lines changed Expand file tree Collapse file tree 1 file changed +87
-0
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,93 @@ $ php artisan make:jsontoclass
2424````
25254 . Check your files under your specified file location
2626
27+
28+ ## Example
29+
30+ Input - PHP Array:
31+
32+ ```` php
33+ 'message' => [
34+ 'author' => [
35+ 'first_name' => '',
36+ 'last_name' => '',
37+ ],
38+ 'text' => '',
39+ 'date' => '2019-01-01'
40+ ]
41+ ````
42+
43+ or JSON
44+
45+ ```` json
46+ {
47+ "message" :{
48+ "author" : {
49+ "first_name" : " " ,
50+ "last_name" : " "
51+ },
52+ "text" : " " ,
53+ "date" : " 2019-01-01" ,
54+ }
55+ }
56+ ````
57+
58+ Output - Classes:
59+
60+ #### Message Class
61+ ```` php
62+ <?php
63+
64+ namespace App\Test;
65+
66+ class Message
67+ {
68+ public $author;
69+
70+ public $text;
71+
72+ public $date;
73+
74+
75+ public function toArray(): array
76+ {
77+ return [
78+ 'author' => collect($this->author)->map(function (Author $data){
79+ return $data->toArray();
80+ })->toArray(),
81+ 'text' => $this->text,
82+ 'date' => $this->date,
83+ ];
84+ }
85+ }
86+
87+ ````
88+
89+ #### Author Class
90+ ```` php
91+ <?php
92+
93+ namespace App\Test;
94+
95+ class Author
96+ {
97+ public $firstName;
98+
99+ public $lastName;
100+
101+
102+ public function toArray(): array
103+ {
104+ return [
105+ 'first_name' => $this->firstName,
106+ 'last_name' => $this->lastName,
107+ ];
108+ }
109+ }
110+
111+ ````
112+
113+
27114## License
28115
29116MIT. Please see the [ license file] ( license.md ) for more information.
You can’t perform that action at this time.
0 commit comments