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

質問編集履歴

1

対象のコントローラーの誤りを修正と、違うエラーの発生についての編集

2020/05/12 02:40

投稿

Keight
Keight

スコア9

title CHANGED
@@ -1,1 +1,1 @@
1
- 【Laravel】退会していないのに「このユーザーは退会しました」と出てしまう
1
+ 【Laravel】退会していないのに「このユーザーは退会しました」と出てしまう、投稿ページにアクセスできない
body CHANGED
@@ -1,25 +1,126 @@
1
1
  会員登録したユーザーが記事を投稿し、そのユーザーが退会を行った場合、記事の詳細ページに「このユーザーは退会しました」と表示させる仕組みにしてあるつもりなのですが・・・。
2
2
  ![イメージ説明](25c4dee2df6ef8a1b85cac482e8bef1a.png)
3
3
  ---
4
- **UsersController**
4
+ ~~**UsersController@show**~~
5
+
6
+ ~~public function show($id)~~
7
+ ~~{~~
8
+ ~~$user = User::find($id);~~
9
+ ~~$lectures = $user->lectures()->orderBy('created_at', 'desc')->paginate(4);~~
10
+
11
+ ~~$data = [~~
12
+ ~~'user' => $user,~~
13
+ ~~'lectures' => $lectures,~~
14
+ ~~]; return view('users.show', $data);~~
15
+ ~~}~~
16
+ 当初、上記のUsersControllerを載せていましたが、今回の対象はLecturesControllerでした、申し訳ありません。
17
+ ---
18
+
19
+ **LecturesController@show**
5
20
  ```PHP
6
- public function show($id)
21
+ public function show($id) {
22
+
7
- {
23
+ $lecture = Lecture::find($id);
8
24
  $user = User::find($id);
25
+
9
- $lectures = $user->lectures()->orderBy('created_at', 'desc')->paginate(4);
26
+ return view('lectures.show', [
10
-
11
- $data = [
27
+ 'lecture' => $lecture,
12
28
  'user' => $user,
13
- 'lectures' => $lectures,
14
- ]; return view('users.show', $data);
29
+ ]);
15
30
  }
16
31
  ```
17
- ---
32
+
18
33
  **lectures/show.blade.php**
19
34
  ```HTML
35
+ @extends('layouts.app')
36
+
37
+ @section('content')
38
+
39
+ <div class="container">
40
+ <div class="head">レクチャー詳細</div>
41
+
42
+ <div class="row ml-3 mt-5 pt-4">
43
+
44
+ <div class="col-9 mx-auto">
45
+ <article class="cont_inner">
46
+ @if ($lecture)
47
+ @if ($lecture->image)
48
+ <figure class="show_post_thumbnail"><div class="new_area-img text-center"><img class="show_image" src="{{ asset('storage/'.$lecture->image) }}"></div></figure>
49
+ @else
50
+ <figure class="show_post_thumbnail"><div class="new_area-img text-center"><img src="../img/noimage.gif"></div></figure>
51
+ @endif
52
+ <section class="post_meta-all multiline line-8">
53
+ <div class="pick_title mb-2">{{ $lecture->title }}</div>
54
+ <time>{{ $lecture->updated_at }}</time>
55
+ <div>
56
+ @if ($lecture->category_name == 'ピッチング')
57
+ <span class="tag-pitching">{{ $lecture->category_name }}</span>
58
+ @elseif ($lecture->category_name == 'バッティング')
59
+ <span class="tag-batting">{{ $lecture->category_name }}</span>
60
+ @elseif ($lecture->category_name == '守備')
61
+ <span class="tag-fielding">{{ $lecture->category_name }}</span>
62
+ @elseif ($lecture->category_name == '走塁')
63
+ <span class="tag-running">{{ $lecture->category_name }}</span>
64
+ @elseif ($lecture->category_name == 'メンタル')
65
+ <span class="tag-mental">{{ $lecture->category_name }}</span>
66
+ @elseif ($lecture->category_name == 'その他')
67
+ <span class="tag-other">{{ $lecture->category_name }}</span>
68
+ @endif
20
- @if ($user === null)
69
+ @if ($user === null)
21
- <span class="d-inline-block ml-2 small text-info">投稿者:このユーザーは退会しました</span></div>
70
+ <span class="d-inline-block ml-2 small text-info">投稿者:このユーザーは退会しました</span></div>
22
- @else
71
+ @else
23
- <span class="d-inline-block ml-2 small text-info">投稿者:<a href="{{ route('users.show', ['id' => $user->id]) }}">{{ $user->name }}</a></span></div>
72
+ <span class="d-inline-block ml-2 small text-info">投稿者:<a href="{{ route('users.show', ['id' => $user->id]) }}">{{ $user->name }}</a></span></div>
24
- @endif
73
+ @endif
74
+ <div class="mt-3 mb-5">{{ $lecture->content }}</div>
75
+ </section>
76
+ @endif
77
+ </article>
78
+ @if (Auth::id() === $lecture->user_id)
79
+ <div class="text-center mt-4">
80
+ {!! Form::open(['route' => ['lectures.destroy', $lecture->id], 'method' => 'delete']) !!}
81
+ {!! Form::submit('削除する', ['class' => 'btn btn-danger btn-sm del_check']) !!}
82
+ {!! Form::close() !!}
83
+ </div>
84
+ @endif
85
+ </div>
86
+ @endsection
25
- ```
87
+ ```
88
+
89
+
90
+ それと、「投稿する」のページにアクセスすると、エラーが出るようになりました。
91
+
92
+ ![イメージ説明](bba576f190aecfde21f09f1f03985668.gif)
93
+
94
+ 「このユーザーは退会しました」と出るものを修正しようと試みる前は問題なくアクセスできていたので、何かの拍子にそうなってしまったのかと思うのですが、ルーティングやコントローラーのcreate部分は特に触っていないですし、一応確認してみても、個人的には何らおかしいところは見受けられませんでした。
95
+
96
+ ---
97
+ **web.php**
98
+ ```PHP
99
+ // ユーザ機能
100
+ Route::group(['middleware' => ['auth']], function () {
101
+ Route::resource('users', 'UsersController', ['only' => ['show']]);
102
+ Route::get('lectures/create', 'LecturesController@create')->name('lectures.create');
103
+ Route::post('lectures', 'LecturesController@store')->name('lectures.store');
104
+ Route::delete('lectures/{id}', 'LecturesController@destroy')->name('lectures.destroy');
105
+ Route::delete('users/{id}', 'UsersController@destroy')->name('users.destroy');
106
+ });
107
+ ```
108
+
109
+ ---
110
+ **LecturesController@create**
111
+ ```PHP
112
+ public function create() {
113
+
114
+ $lecture = new Lecture;
115
+
116
+ return view('lectures.create', [
117
+ 'lecture' => $lecture,
118
+ ]);
119
+ }
120
+ ```
121
+
122
+ ---
123
+ また、エラー内容を訳すと「非オブジェクトのプロパティ「user_id」を取得しようとしています」とのことで、tinkerにてあらためてLectureデータのカラムを確認しても、「user_id」は存在しています。
124
+ ![イメージ説明](34576aabe3e51611ffdbf7f0e1f98495.png)
125
+
126
+ いろいろとすみません、よろしくお願いいたしますm(_ _)m