質問するログイン新規登録

質問編集履歴

8

コントローラを追記しました

2021/02/24 08:52

投稿

Szi
Szi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -144,6 +144,8 @@
144
144
  */
145
145
  public function __construct(Repository $config)
146
146
  {
147
+      /***このメソッド内で予定更新時のときに登録された予定を取得する必要があると予測 **/
148
+
147
149
  $this->config = $config['google'];
148
150
 
149
151
  $this->client = new Google_Client();
@@ -162,7 +164,7 @@
162
164
  */
163
165
  public function addEvent(Google_Service_Calendar_Event $event): Google_Service_Calendar_Event
164
166
  {
165
-      dd($event); //確認
167
+      dd($event); /*** 確認 ***/
166
168
  return $this->service->events->insert($this->calendar_id, $event);
167
169
  }
168
170
 
@@ -172,8 +174,80 @@
172
174
  */
173
175
  public function updateEvent(Google_Service_Calendar_Event $event): Google_Service_Calendar_Event
174
176
  {
175
- dd($event); //確認
177
+ dd($event); /***確認***/
176
178
  return $this->service->events->update($this->calendar_id, $event->getId(), $event);
177
179
  }
178
180
  }
181
+ ```
182
+
183
+
184
+
185
+ コントローラ
186
+ ```ここに言語を入力
187
+ <?php
188
+
189
+ namespace App\Http\Controllers;
190
+
191
+ use App\Services\GoogleCalendarService;
192
+ use Illuminate\Http\Request;
193
+
194
+ class CalendarController extends Controller
195
+ {
196
+ private $service;
197
+
198
+ /**
199
+ * CalendarController constructor.
200
+ * @param GoogleCalendarService $service
201
+ */
202
+ public function __construct(GoogleCalendarService $service)
203
+ {
204
+ $this->service = $service;
205
+ }
206
+
207
+ /**
208
+ * @param Request $request
209
+ */
210
+ public function store(Request $request)
211
+ {
212
+ $this->service->addEvent($this->makeGoogleEvent($request));
213
+ }
214
+
215
+ /**
216
+ * @param Request $request
217
+ */
218
+ public function update(Request $request)
219
+ {
220
+ $this->service->updateEvent($this->makeGoogleEvent($request));
221
+ }
222
+
223
+ /**
224
+ * @param Request $request
225
+ * @return \Google_Service_Calendar_Event
226
+ */
227
+ private function makeGoogleEvent(Request $request)
228
+ {
229
+ /*** ここでイベント更新時の時にもインスタンス化しているのが原因だと考えています。 ***/
230
+ $google_event = new \Google_Service_Calendar_Event();
231
+
232
+ if ($request->has('event_id')) {
233
+ $google_event->setId($request->event_id);
234
+ }
235
+
236
+ $google_event->setSummary($request->title);
237
+ $google_event->setDescription($request->memo);
238
+ $google_event->setColorId('1');
239
+
240
+ $start = new \Google_Service_Calendar_EventDateTime();
241
+ $start->setTimeZone('Asia/Tokyo');
242
+ $start->setDateTime($request->startdate . ' ' . $request->starttime);
243
+ $google_event->setStart($start);
244
+
245
+ $end = new \Google_Service_Calendar_EventDateTime();
246
+ $end->setTimeZone('Asia/Tokyo');
247
+ $end->setDateTime($request['enddate'] . ' ' . $request['endtime']);
248
+ $google_event->setEnd($end);
249
+
250
+ return $google_event;
251
+ }
252
+ }
179
253
  ```

7

追記をしました

2021/02/24 08:52

投稿

Szi
Szi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -98,4 +98,82 @@
98
98
  $result = $service->events->update($calendarId,$google_event_id,$google_event);
99
99
  }
100
100
  }
101
+ ```
102
+
103
+
104
+
105
+ 追記
106
+ app/Services/GoogleCalendarService.php
107
+ ```ここに言語を入力
108
+ <?php
109
+
110
+ namespace App\Services;
111
+
112
+ use Google\Exception;
113
+ use Google_Client;
114
+ use Google_Service_Calendar;
115
+ use Google_Service_Calendar_Event;
116
+ use Illuminate\Config\Repository;
117
+
118
+ class GoogleCalendarService
119
+ {
120
+ /**
121
+ * @var array
122
+ */
123
+ private $config;
124
+
125
+ /**
126
+ * @var Google_Client
127
+ */
128
+ private $client;
129
+
130
+ /**
131
+ * @var Google_Service_Calendar
132
+ */
133
+ private $service;
134
+
135
+ /**
136
+ * @var
137
+ */
138
+ private $calendar_id;
139
+
140
+ /**
141
+ * GoogleCalendarService constructor.
142
+ * @param Repository $config
143
+ * @throws Exception
144
+ */
145
+ public function __construct(Repository $config)
146
+ {
147
+ $this->config = $config['google'];
148
+
149
+ $this->client = new Google_Client();
150
+ $this->client->setApplicationName($this->config['app_name']);
151
+ $this->client->setScopes(Google_Service_Calendar::CALENDAR_EVENTS);
152
+ $this->client->setAuthConfig(storage_path('app/google-calendar/auth.json'));
153
+
154
+ $this->service = new Google_Service_Calendar($this->client);
155
+
156
+ $this->calendar_id = $this->config['calendar_id'];
157
+ }
158
+
159
+ /**
160
+ * @param Google_Service_Calendar_Event $event
161
+ * @return Google_Service_Calendar_Event
162
+ */
163
+ public function addEvent(Google_Service_Calendar_Event $event): Google_Service_Calendar_Event
164
+ {
165
+      dd($event); //確認
166
+ return $this->service->events->insert($this->calendar_id, $event);
167
+ }
168
+
169
+ /**
170
+ * @param Google_Service_Calendar_Event $event
171
+ * @return Google_Service_Calendar_Event
172
+ */
173
+ public function updateEvent(Google_Service_Calendar_Event $event): Google_Service_Calendar_Event
174
+ {
175
+ dd($event); //確認
176
+ return $this->service->events->update($this->calendar_id, $event->getId(), $event);
177
+ }
178
+ }
101
179
  ```

6

追記をしました。

2021/02/24 08:47

投稿

Szi
Szi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,7 @@
7
7
  参考にしたサイト
8
8
  [Google Calendar API を用いてLaravelアプリから予定を追加する手順](https://qiita.com/sakashin10291029/items/c5f7540ab6a32283b619)
9
9
  [PHPからGoogleカレンダーに予定を追加・変更・削除する](https://jipulog.com/google-calendar-php/)
10
-
10
+ 使っているAPIはgooglecalendarAPIです。
11
11
  ```
12
12
  class CalenderEventsController extends Controller{
13
13
  //googleカレンダー APIインスタンス作成

5

参考サイトのリンクを追加しました。

2021/02/22 08:16

投稿

Szi
Szi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -4,6 +4,9 @@
4
4
  どのように受け渡せばよいか分からず困っております。
5
5
  どなたかご教授頂ければと思います。
6
6
  宜しくお願い致します。
7
+ 参考にしたサイト
8
+ [Google Calendar API を用いてLaravelアプリから予定を追加する手順](https://qiita.com/sakashin10291029/items/c5f7540ab6a32283b619)
9
+ [PHPからGoogleカレンダーに予定を追加・変更・削除する](https://jipulog.com/google-calendar-php/)
7
10
 
8
11
  ```
9
12
  class CalenderEventsController extends Controller{

4

インデントを直しました

2021/02/22 08:14

投稿

Szi
Szi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -6,11 +6,9 @@
6
6
  宜しくお願い致します。
7
7
 
8
8
  ```
9
- class CalenderEventsController extends Controller
9
+ class CalenderEventsController extends Controller{
10
- {
11
10
  //googleカレンダー APIインスタンス作成
12
- public function getClient()
11
+ public function getClient(){
13
- {
14
12
 
15
13
  $client = new Google_Client();
16
14
 
@@ -24,46 +22,45 @@
24
22
 
25
23
  }
26
24
 
27
- //登録処理
25
+   //登録処理
28
- public function store(Request $request){
26
+   public function store(Request $request){
29
- /** googleカレンダーとの連携処理 **/
27
+    /** googleカレンダーとの連携処理 **/
30
28
 
31
- $client = $this->getClient();
29
+  $client = $this->getClient();
32
- $service = new Google_Service_Calendar($client);
30
+ $service = new Google_Service_Calendar($client);
33
- $calendarId = env('GOOGLE_CALENDAR_ID');
31
+ $calendarId = env('GOOGLE_CALENDAR_ID');
32
+ $google_event = new Google_Service_Calendar_Event();
34
33
 
34
+ //件名
35
- $google_event = new Google_Service_Calendar_Event();
35
+ $google_event->setSummary($request['title']);
36
36
 
37
- //件名
37
+ //内容
38
- $google_event->setSummary($request['title']);
38
+ $google_event->setDescription($request['memo']);
39
39
 
40
- //内容
40
+ //
41
- $google_event->setDescription($request['memo']);
41
+ $google_event->setColorId("1");
42
42
 
43
- //
43
+ //開始時間
44
+ $start = new Google_Service_Calendar_EventDateTime();
45
+ $start->setDateTime($request['startdate']. 'T'.$request['starttime'] . '+09:00');
44
- $google_event->setColorId("1");
46
+ $google_event->setStart($start);
45
47
 
46
- //開始時間
47
- $start = new Google_Service_Calendar_EventDateTime();
48
- $start->setDateTime($request['startdate']. 'T'.$request['starttime'] . '+09:00');
49
- $google_event->setStart($start);
50
48
 
51
49
 
50
+ //終了時間
51
+ $end = new Google_Service_Calendar_EventDateTime();
52
+ $end->setDateTime($request['enddate']. 'T'.$request['endtime'] . '+09:00');
53
+ $google_event->setEnd($end);
52
54
 
53
- //終了時間
55
+ //グーグルカレンダーに予定反映
54
- $end = new Google_Service_Calendar_EventDateTime();
56
+ $result = $service->events->insert($calendarId,$google_event);
55
- $end->setDateTime($request['enddate']. 'T'.$request['endtime'] . '+09:00');
56
- $google_event->setEnd($end);
57
57
 
58
- //グーグルカレンダーに予定反映
59
- $result = $service->events->insert($calendarId,$google_event);
60
-
61
- //イベントIDを取得する
58
+ //イベントIDを取得する
62
- $google_event_id = $result->getId();
59
+ $google_event_id = $result->getId();
63
- }
60
+   }
64
61
 
65
- //変更処理
62
+ //変更処理
66
- public function update(Request request){
63
+ public function update(Request request){
67
64
  $client = $this->getClient();
68
65
  $service = new Google_Service_Calendar($client);
69
66
  $calendarId = env('GOOGLE_CALENDAR_ID');
@@ -96,6 +93,6 @@
96
93
 
97
94
  //グーグルカレンダーに予定反映
98
95
  $result = $service->events->update($calendarId,$google_event_id,$google_event);
99
- }
96
+ }
100
97
  }
101
98
  ```

3

間違えているところがあったので修正しました。

2021/02/22 06:50

投稿

Szi
Szi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  }
26
26
 
27
- 登録処理
27
+ //登録処理
28
28
  public function store(Request $request){
29
29
  /** googleカレンダーとの連携処理 **/
30
30
 

2

クラス名を追加しました

2021/02/22 06:47

投稿

Szi
Szi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,9 @@
6
6
  宜しくお願い致します。
7
7
 
8
8
  ```
9
+ class CalenderEventsController extends Controller
10
+ {
9
- //googleカレンダー APIインスタンス作成
11
+ //googleカレンダー APIインスタンス作成
10
12
  public function getClient()
11
13
  {
12
14
 
@@ -22,8 +24,8 @@
22
24
 
23
25
  }
24
26
 
25
- 登録処理
27
+ 登録処理
26
- public function store(Request $request){
28
+ public function store(Request $request){
27
29
  /** googleカレンダーとの連携処理 **/
28
30
 
29
31
  $client = $this->getClient();
@@ -58,10 +60,10 @@
58
60
 
59
61
  //イベントIDを取得する
60
62
  $google_event_id = $result->getId();
61
- }
63
+ }
62
64
 
63
- //変更処理
65
+ //変更処理
64
- public function update(Request request){
66
+ public function update(Request request){
65
67
  $client = $this->getClient();
66
68
  $service = new Google_Service_Calendar($client);
67
69
  $calendarId = env('GOOGLE_CALENDAR_ID');
@@ -94,6 +96,6 @@
94
96
 
95
97
  //グーグルカレンダーに予定反映
96
98
  $result = $service->events->update($calendarId,$google_event_id,$google_event);
99
+ }
97
100
  }
98
-
99
101
  ```

1

間違えているところがあったので修正しました。

2021/02/22 06:42

投稿

Szi
Szi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  $client->setScopes(Google_Service_Calendar::CALENDAR_EVENTS);
18
18
 
19
- $client->setAuthConfig(storage_path('app/google-calendar/angelic-bazaar-304502-c516422b2649.json'));
19
+ $client->setAuthConfig(storage_path('app/google-calendar/'JSONファイル'));
20
20
 
21
21
  return $client;
22
22
 
@@ -66,11 +66,11 @@
66
66
  $service = new Google_Service_Calendar($client);
67
67
  $calendarId = env('GOOGLE_CALENDAR_ID');
68
68
 
69
- $google_event_id = "5pc2nmu4a97vosoea37mju2f7k";
69
+ $google_event_id = ここに登録時のイベントIDを持ってきたい
70
70
 
71
71
  $google_event = new Google_Service_Calendar_Event();
72
- $event_ID =
73
72
 
73
+
74
74
  //件名
75
75
  $google_event->setSummary($request['title']);
76
76
 
@@ -93,12 +93,7 @@
93
93
  $google_event->setEnd($end);
94
94
 
95
95
  //グーグルカレンダーに予定反映
96
- //$result = $service->events->update($calendarId,$google_event_id,$google_event);
96
+ $result = $service->events->update($calendarId,$google_event_id,$google_event);
97
97
  }
98
98
 
99
- ```
99
+ ```
100
-     登録処理の時に書いてある
101
-     //イベントIDを取得する
102
- $google_event_id = $result->getId();
103
-
104
- このgoogle_event_idの値を変更処理の時に宣言し、使用したいのですがどのようにすればよいかわかりません。