質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

3回答

950閲覧

変数を複数のメソッドで使用したい

Szi

総合スコア1

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

1クリップ

投稿2021/02/22 06:20

編集2021/02/24 08:52

現在laravel側からgoogleカレンダーの予定を操作しようとしております。
予定作成時にイベントIDを取得しております。
登録はうまくいったものの、変更、削除の部分でイベントIDを使用しないといけないのですが
どのように受け渡せばよいか分からず困っております。
どなたかご教授頂ければと思います。
宜しくお願い致します。
参考にしたサイト
Google Calendar API を用いてLaravelアプリから予定を追加する手順
PHPからGoogleカレンダーに予定を追加・変更・削除する
使っているAPIはgooglecalendarAPIです。

class CalenderEventsController extends Controller{ //googleカレンダー APIインスタンス作成 public function getClient(){ $client = new Google_Client(); $client->setApplicationName('Google Calendar API plus Laravel'); $client->setScopes(Google_Service_Calendar::CALENDAR_EVENTS); $client->setAuthConfig(storage_path('app/google-calendar/'JSONファイル')); return $client; }   //登録処理   public function store(Request $request){    /** googleカレンダーとの連携処理 **/  $client = $this->getClient(); $service = new Google_Service_Calendar($client); $calendarId = env('GOOGLE_CALENDAR_ID'); $google_event = new Google_Service_Calendar_Event(); //件名 $google_event->setSummary($request['title']); //内容 $google_event->setDescription($request['memo']); //色 $google_event->setColorId("1"); //開始時間 $start = new Google_Service_Calendar_EventDateTime(); $start->setDateTime($request['startdate']. 'T'.$request['starttime'] . '+09:00'); $google_event->setStart($start); //終了時間 $end = new Google_Service_Calendar_EventDateTime(); $end->setDateTime($request['enddate']. 'T'.$request['endtime'] . '+09:00'); $google_event->setEnd($end); //グーグルカレンダーに予定反映 $result = $service->events->insert($calendarId,$google_event); //イベントIDを取得する $google_event_id = $result->getId();   } //変更処理 public function update(Request request){ $client = $this->getClient(); $service = new Google_Service_Calendar($client); $calendarId = env('GOOGLE_CALENDAR_ID'); $google_event_id = ここに登録時のイベントIDを持ってきたい $google_event = new Google_Service_Calendar_Event(); //件名 $google_event->setSummary($request['title']); //内容 $google_event->setDescription($request['memo']); //色 $google_event->setColorId("1"); //開始時間 $start = new Google_Service_Calendar_EventDateTime(); $start->setDateTime($request['startdate']. 'T'.$request['starttime'] . '+09:00'); $google_event->setStart($start); //終了時間 $end = new Google_Service_Calendar_EventDateTime(); $end->setDateTime($request['enddate']. 'T'.$request['endtime'] . '+09:00'); $google_event->setEnd($end); //グーグルカレンダーに予定反映 $result = $service->events->update($calendarId,$google_event_id,$google_event); } }

追記
app/Services/GoogleCalendarService.php

<?php namespace App\Services; use Google\Exception; use Google_Client; use Google_Service_Calendar; use Google_Service_Calendar_Event; use Illuminate\Config\Repository; class GoogleCalendarService { /** * @var array */ private $config; /** * @var Google_Client */ private $client; /** * @var Google_Service_Calendar */ private $service; /** * @var */ private $calendar_id; /** * GoogleCalendarService constructor. * @param Repository $config * @throws Exception */ public function __construct(Repository $config) {      /***このメソッド内で予定更新時のときに登録された予定を取得する必要があると予測 **/ $this->config = $config['google']; $this->client = new Google_Client(); $this->client->setApplicationName($this->config['app_name']); $this->client->setScopes(Google_Service_Calendar::CALENDAR_EVENTS); $this->client->setAuthConfig(storage_path('app/google-calendar/auth.json')); $this->service = new Google_Service_Calendar($this->client); $this->calendar_id = $this->config['calendar_id']; } /** * @param Google_Service_Calendar_Event $event * @return Google_Service_Calendar_Event */ public function addEvent(Google_Service_Calendar_Event $event): Google_Service_Calendar_Event {      dd($event); /*** 確認 ***/ return $this->service->events->insert($this->calendar_id, $event); } /** * @param Google_Service_Calendar_Event $event * @return Google_Service_Calendar_Event */ public function updateEvent(Google_Service_Calendar_Event $event): Google_Service_Calendar_Event { dd($event); /***確認***/ return $this->service->events->update($this->calendar_id, $event->getId(), $event); } }

コントローラ

<?php namespace App\Http\Controllers; use App\Services\GoogleCalendarService; use Illuminate\Http\Request; class CalendarController extends Controller { private $service; /** * CalendarController constructor. * @param GoogleCalendarService $service */ public function __construct(GoogleCalendarService $service) { $this->service = $service; } /** * @param Request $request */ public function store(Request $request) { $this->service->addEvent($this->makeGoogleEvent($request)); } /** * @param Request $request */ public function update(Request $request) { $this->service->updateEvent($this->makeGoogleEvent($request)); } /** * @param Request $request * @return \Google_Service_Calendar_Event */ private function makeGoogleEvent(Request $request) { /*** ここでイベント更新時の時にもインスタンス化しているのが原因だと考えています。 ***/ $google_event = new \Google_Service_Calendar_Event(); if ($request->has('event_id')) { $google_event->setId($request->event_id); } $google_event->setSummary($request->title); $google_event->setDescription($request->memo); $google_event->setColorId('1'); $start = new \Google_Service_Calendar_EventDateTime(); $start->setTimeZone('Asia/Tokyo'); $start->setDateTime($request->startdate . ' ' . $request->starttime); $google_event->setStart($start); $end = new \Google_Service_Calendar_EventDateTime(); $end->setTimeZone('Asia/Tokyo'); $end->setDateTime($request['enddate'] . ' ' . $request['endtime']); $google_event->setEnd($end); return $google_event; } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

phper.k

2021/02/22 06:36 編集

クラス名はなんと命名してますか? それが明示されてないと、コードを書き直してあげることができない。 というより、間違いが多すぎて書き直す必要が多い。 説明のためにも必要。
Szi

2021/02/22 06:39

クラス名はCalenderEventsController となっております
phper.k

2021/02/22 06:40

質問を修正してください。
Szi

2021/02/22 06:43

失礼しました。 修正しました。
phper.k

2021/02/22 06:46 編集

「登録処理」のところ、コメントになっていないのでシンタックスハイライトが崩れてます。 赤の他人の時間をいただくのですから、細かな部分に気を遣いましょう。 あと、インデントも揃えてください。
Szi

2021/02/22 06:51

大変失礼いたしました。 インデント修正させていただきました。 宜しくお願い致します。
phper.k

2021/02/22 07:58 編集

回答作りながら、コードを拝見していますが、クラスやオブジェクトの概念をちゃんと学習せずに、このコードを書いてませんか?
phper.k

2021/02/22 08:10

参考にしたサイトのURLを明記してください。
phper.k

2021/02/22 08:12

あと、Googleクライアントに使っているライブラリの名称を明記してください
Szi

2021/02/22 08:16

参考にしたサイトのURLとライブラリの名称を追記しました。 宜しくお願い致します。
phper.k

2021/02/22 08:17

API じゃなくて、composer でインストールしたライブラリのこと聞いてます。
Szi

2021/02/22 08:21

失礼しました。 "google/apiclient": "^2.0" のみとなっております。
guest

回答3

0

大幅にリファクタしました。
参考にしたというサイトですが、できはよくない。
controller に env() を使っているのは極めて低質である証拠と言える。
まずは、以下のコードをコピペでも構わないので実行し、丹念に処理を追って理解に努めてください。
キモは、Controller に処理をダラダラとく書くのではなく、Service に切り分けたところ。

/config/service.php に追記

php

1 'google' => [ 2 'calendar_id' => env('GOOGLE_CALENDAR_ID'), 3 'app_name' => env('GOOGLE_CALENDAR_APP_NAME'), 4 ]

app/Providers/AppServiceProvider.php

php

1<?php 2 3namespace App\Providers; 4 5use App\Services\GoogleCalendarService; 6use Illuminate\Support\ServiceProvider; 7 8class AppServiceProvider extends ServiceProvider 9{ 10 /** 11 * Register any application services. 12 * 13 * @return void 14 */ 15 public function register() 16 { 17 // 18 } 19 20 /** 21 * Bootstrap any application services. 22 * 23 * @return void 24 */ 25 public function boot() 26 { 27 // 追記 28 $this->app->bind(GoogleCalendarService::class, function ($app) { 29 return new GoogleCalendarService($app['config']); 30 }); 31 } 32}

app/Services/GoogleCalendarService.php を作成

php

1<?php 2 3namespace App\Services; 4 5use Google\Exception; 6use Google_Client; 7use Google_Service_Calendar; 8use Google_Service_Calendar_Event; 9use Illuminate\Config\Repository; 10 11class GoogleCalendarService 12{ 13 /** 14 * @var array 15 */ 16 private $config; 17 18 /** 19 * @var Google_Client 20 */ 21 private $client; 22 23 /** 24 * @var Google_Service_Calendar 25 */ 26 private $service; 27 28 /** 29 * @var 30 */ 31 private $calendar_id; 32 33 /** 34 * GoogleCalendarService constructor. 35 * @param Repository $config 36 * @throws Exception 37 */ 38 public function __construct(Repository $config) 39 { 40 $this->config = $config['google']; 41 42 $this->client = new Google_Client(); 43 $this->client->setApplicationName($this->config['app_name']); 44 $this->client->setScopes(Google_Service_Calendar::CALENDAR_EVENTS); 45 $this->client->setAuthConfig(storage_path('app/google-calendar/auth.json')); 46 47 $this->service = new Google_Service_Calendar($this->client); 48 49 $this->calendar_id = $this->config['calendar_id']; 50 } 51 52 /** 53 * @param Google_Service_Calendar_Event $event 54 * @return Google_Service_Calendar_Event 55 */ 56 public function addEvent(Google_Service_Calendar_Event $event): Google_Service_Calendar_Event 57 { 58 return $this->service->events->insert($this->calendar_id, $event); 59 } 60 61 /** 62 * @param Google_Service_Calendar_Event $event 63 * @return Google_Service_Calendar_Event 64 */ 65 public function updateEvent(Google_Service_Calendar_Event $event): Google_Service_Calendar_Event 66 { 67 return $this->service->events->update($this->calendar_id, $event->getId(), $event); 68 } 69}

app/Http/Controllers/CalendarController.php

php

1<?php 2 3namespace App\Http\Controllers; 4 5use App\Services\GoogleCalendarService; 6use Illuminate\Http\Request; 7 8class CalendarController extends Controller 9{ 10 private $service; 11 12 /** 13 * CalendarController constructor. 14 * @param GoogleCalendarService $service 15 */ 16 public function __construct(GoogleCalendarService $service) 17 { 18 $this->service = $service; 19 } 20 21 /** 22 * @param Request $request 23 */ 24 public function store(Request $request) 25 { 26 $this->service->addEvent($this->makeGoogleEvent($request)); 27 } 28 29 /** 30 * @param Request $request 31 */ 32 public function update(Request $request) 33 { 34 $this->service->updateEvent($this->makeGoogleEvent($request)); 35 } 36 37 /** 38 * @param Request $request 39 * @return \Google_Service_Calendar_Event 40 */ 41 private function makeGoogleEvent(Request $request) 42 { 43 $google_event = new \Google_Service_Calendar_Event(); 44 45 if ($request->has('event_id')) { 46 $google_event->setId($request->event_id); 47 } 48 49 $google_event->setSummary($request->title); 50 $google_event->setDescription($request->memo); 51 $google_event->setColorId('1'); 52 53 $start = new \Google_Service_Calendar_EventDateTime(); 54 $start->setTimeZone('Asia/Tokyo'); 55 $start->setDateTime($request->startdate . ' ' . $request->starttime); 56 $google_event->setStart($start); 57 58 $end = new \Google_Service_Calendar_EventDateTime(); 59 $end->setTimeZone('Asia/Tokyo'); 60 $end->setDateTime($request['enddate'] . ' ' . $request['endtime']); 61 $google_event->setEnd($end); 62 63 return $google_event; 64 } 65}

投稿2021/02/22 09:17

phper.k

総合スコア3923

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

phper.k

2021/02/22 09:19

GoogleApi アカウント取得してまでは書いてないので、動作確認まではしてません。
phper.k

2021/02/22 16:44

修正依頼にはせっせと反応するわりに、回答ついたら無反応なのかよ。
Szi

2021/02/23 23:37

おはようございます。 予定があり、本日拝見させていただいたところです。 返信に時間が掛かり、大変申し訳ございませんでした。 また詳しくコードを載せていただきありがとうございます。 大変嬉しく思います。
Szi

2021/02/23 23:40

不快な思いをさせてしまい大変申し訳ございません。
Szi

2021/02/24 04:22

更新時にeventIDがnullとなってしまうのですがなぜそのようになるのか分かりません。 教えて頂きたいです。 宜しくお願い致します。
phper.k

2021/02/24 06:11 編集

> 更新時にeventIDがnullとなってしまうのですがなぜそのようになるのか分かりません。 どんなコード書いているのかもわからないのに、原因聞かれても知りません。 null になるのをどうやって確認しているのかもわからない。 確認方法が間違っている可能性もある。 人に聞く前に、伝えるべきことを伝えてから聞いてください、
Szi

2021/02/24 06:29

ありがとうございます。 もう一度確認等をします。
Szi

2021/02/24 08:36

phper.k様が書いたコードをそのまま使用させていただきました。 app/Services/GoogleCalendarService.phpの addEventメソッドのretrun文の前にdd($event)をし、 同じファイルのupdateEventのreturn文の前にdd($event)をしたところ addEventの時にはidに値が入っておりましたが、updateの時にはidの値がnullでした。 コントローラ側のmakeGoogleEventメソッドの中で $google_event = new Google_Service_Calendar_Event();とあり update時もインスタンス化しているのでそれが原因だと思っております。 そうした時にapp/Services/GoogleCalendarService.phpの __constractメソッドの中で既に登録されている予定を取得すればよいと考えておりますが どのように記述をすればよいか分からずにおります。
phper.k

2021/02/24 08:43 編集

日本語文読むより、コード読む方が楽(あなたの手元のコードを再現するための手間がかかる)なんで、コントローラーの実装見せてください。 質問本文に追記してください。
Szi

2021/02/24 08:53

追記させていただきました。 宜しくお願い致します。
phper.k

2021/02/24 08:54 編集

view から event_id の値渡してないだけちゃう?
Szi

2021/02/24 09:00

理解不足で大変申し訳ないのですがview側に値を渡していないとの事でしたがその場合 googleカレンダーに予定が全くない状態の時view側でevent_idがないとエラーが出ると思っております 私の考え方は間違っていますか?
phper.k

2021/02/24 09:03 編集

全くない時は当然じゃない? その時は新規作成になるわけで、event_id は当然、空の値になるでしょう
Szi

2021/02/24 09:07

ということは予定変更画面にevent_idを渡せばよいのでしょうか? updateメソッドのところにはカレンダーを表示しているviewにリダイレクトさせております。
phper.k

2021/02/24 09:09

ごめんなさい。 何言っているのかわかりません。
Szi

2021/02/24 09:15

私の頭の中が整理できていないのでいったん整理してきます。
guest

0

IDをセッションに保持しては。

保存

PHP

1$request->session()->put('key', 'value');

取得

PHP

1$request->session()->get('key');

Laravel公式

投稿2021/03/03 05:53

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

HTML等の画面から対象となるIDを渡す方法を考えたほうがいいと思います。

$requestで受け取っている値の中にevent_idを混ぜるのがいいのでは無いでしょうか?
そのために、メソッドstoreで取得したIDを
<input type=”hidden”>などを用いて画面に埋め込んでみてはどうでしょうか?

投稿2021/02/22 07:26

yamame

総合スコア81

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Szi

2021/02/22 07:35

storeメソッドの最後リダイレクトの部分でIDを渡し、view側で隠すと言う事でしょうか?
yamame

2021/02/24 08:02

一度のwebリクエストに対して処理はひとつなはずなので、 storeメソッドで一度画面が帰っているはずですよね? その後、updateのメソッドを呼び出すためのwebリクエストを発行しているはずです。 そのupdateのメソッドで前回のstoreのIDを使いたいということだと理解しました。 [画面]->{webリクエスト}->storeメソッド->[view]->[画面] と見るなら[view]にIDを渡すが、[画面]には見えないようにしておくということです。 [画面]->{webリクエスト}->updateメソッド->[view]->[画面] のwebリクエストにIDが入るようにすればいいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問