回答編集履歴
3
戻しておく
answer
CHANGED
@@ -1,1 +1,203 @@
|
|
1
|
+
自分ならこうしますという形で、回答します。
|
2
|
+
# ER
|
3
|
+

|
4
|
+
# migrations
|
5
|
+
```php
|
6
|
+
<?php
|
7
|
+
use Illuminate\Database\Migrations\Migration;
|
8
|
+
use Illuminate\Database\Schema\Blueprint;
|
9
|
+
use Illuminate\Support\Facades\Schema;
|
10
|
+
class CreateRecipesTable extends Migration
|
11
|
+
{
|
12
|
+
/**
|
13
|
+
* Run the migrations.
|
14
|
+
*
|
15
|
+
* @return void
|
16
|
+
*/
|
17
|
+
public function up()
|
18
|
+
{
|
19
|
+
Schema::create('recipes', function (Blueprint $table) {
|
20
|
+
$table->bigIncrements('id');
|
21
|
+
$table->string('name');
|
22
|
+
$table->unsignedBigInteger('created_by');
|
23
|
+
$table->timestamps();
|
24
|
+
$table->foreign('created_by')->references('id')->on('users');
|
25
|
+
});
|
26
|
+
}
|
27
|
+
/**
|
28
|
+
* Reverse the migrations.
|
29
|
+
*
|
30
|
+
* @return void
|
31
|
+
*/
|
32
|
+
public function down()
|
33
|
+
{
|
34
|
+
Schema::dropIfExists('recipes');
|
35
|
+
}
|
36
|
+
}
|
37
|
+
```
|
38
|
+
```php
|
39
|
+
<?php
|
40
|
+
use Illuminate\Database\Migrations\Migration;
|
41
|
+
use Illuminate\Database\Schema\Blueprint;
|
42
|
+
use Illuminate\Support\Facades\Schema;
|
1
|
-
|
43
|
+
class CreateCommentsTable extends Migration
|
44
|
+
{
|
45
|
+
/**
|
46
|
+
* Run the migrations.
|
47
|
+
*
|
48
|
+
* @return void
|
49
|
+
*/
|
50
|
+
public function up()
|
51
|
+
{
|
52
|
+
Schema::create('comments', function (Blueprint $table) {
|
53
|
+
$table->bigIncrements('id');
|
54
|
+
$table->unsignedBigInteger('recipe_id');
|
55
|
+
$table->string('content');
|
56
|
+
$table->unsignedBigInteger('created_by');
|
57
|
+
$table->timestamps();
|
58
|
+
$table->foreign('recipe_id')->references('id')->on('recipes');
|
59
|
+
$table->foreign('created_by')->references('id')->on('users');
|
60
|
+
});
|
61
|
+
}
|
62
|
+
/**
|
63
|
+
* Reverse the migrations.
|
64
|
+
*
|
65
|
+
* @return void
|
66
|
+
*/
|
67
|
+
public function down()
|
68
|
+
{
|
69
|
+
Schema::dropIfExists('comments');
|
70
|
+
}
|
71
|
+
}
|
72
|
+
```
|
73
|
+
# models
|
74
|
+
```php
|
75
|
+
<?php
|
76
|
+
namespace App;
|
77
|
+
use Illuminate\Database\Eloquent\Model;
|
78
|
+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
79
|
+
use Illuminate\Database\Eloquent\Relations\HasMany;
|
80
|
+
class Recipe extends Model
|
81
|
+
{
|
82
|
+
protected $fillable = [
|
83
|
+
'name',
|
84
|
+
'createdBy'
|
85
|
+
];
|
86
|
+
/**
|
87
|
+
* @return HasMany
|
88
|
+
*/
|
89
|
+
public function comments()
|
90
|
+
{
|
91
|
+
return $this->hasMany(Comment::class);
|
92
|
+
}
|
93
|
+
/**
|
94
|
+
* @return BelongsTo
|
95
|
+
*/
|
96
|
+
public function createdBy()
|
97
|
+
{
|
98
|
+
return $this->belongsTo(User::class, 'created_by');
|
99
|
+
}
|
100
|
+
}
|
101
|
+
```
|
102
|
+
```php
|
103
|
+
<?php
|
104
|
+
namespace App;
|
105
|
+
use Illuminate\Database\Eloquent\Model;
|
106
|
+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
107
|
+
class Comment extends Model
|
108
|
+
{
|
109
|
+
protected $fillable = [
|
110
|
+
'content',
|
111
|
+
'createdBy'
|
112
|
+
];
|
113
|
+
/**
|
114
|
+
* @return BelongsTo
|
115
|
+
*/
|
116
|
+
public function recipe()
|
117
|
+
{
|
118
|
+
return $this->belongsTo(Recipe::class);
|
119
|
+
}
|
120
|
+
/**
|
121
|
+
* @return BelongsTo
|
122
|
+
*/
|
123
|
+
public function createdBy()
|
124
|
+
{
|
125
|
+
return $this->belongsTo(User::class, 'created_by');
|
126
|
+
}
|
127
|
+
}
|
128
|
+
```
|
129
|
+
# controllers
|
130
|
+
```php
|
131
|
+
<?php
|
132
|
+
namespace App\Http\Controllers;
|
133
|
+
use App\Recipe;
|
134
|
+
use Illuminate\View\View;
|
135
|
+
class RecipeController extends Controller
|
136
|
+
{
|
137
|
+
/**
|
138
|
+
* @return View
|
139
|
+
*/
|
140
|
+
public function index()
|
141
|
+
{
|
142
|
+
$recipes = Recipe::query()
|
143
|
+
->with(['comments', 'createdBy'])
|
144
|
+
->paginate();
|
145
|
+
return view('recipes.index', compact('recipes'));
|
146
|
+
}
|
147
|
+
/**
|
148
|
+
* @param Recipe $recipe
|
149
|
+
* @return View
|
150
|
+
*/
|
151
|
+
public function show(Recipe $recipe)
|
152
|
+
{
|
153
|
+
return view('recipes.show', compact('recipe'));
|
154
|
+
}
|
155
|
+
}
|
156
|
+
```
|
157
|
+
```php
|
158
|
+
<?php
|
159
|
+
namespace App\Http\Controllers;
|
160
|
+
use App\Comment;
|
161
|
+
use App\Recipe;
|
162
|
+
use Illuminate\Http\RedirectResponse;
|
163
|
+
use Illuminate\Http\Request;
|
164
|
+
use Illuminate\Support\Facades\Auth;
|
165
|
+
class CommentController extends Controller
|
166
|
+
{
|
167
|
+
/**
|
168
|
+
* @param Request $request
|
169
|
+
* @param Recipe $recipe
|
170
|
+
* @return RedirectResponse
|
171
|
+
*/
|
172
|
+
public function store(Request $request, Recipe $recipe)
|
173
|
+
{
|
174
|
+
$recipe->comments()->save(new Comment([
|
175
|
+
'content' => $request->get('content'),
|
176
|
+
'created_by' => Auth::id()
|
177
|
+
]));
|
178
|
+
return redirect()->route('recipes', compact('recipe'));
|
179
|
+
}
|
180
|
+
}
|
181
|
+
```
|
182
|
+
# routing
|
183
|
+
```php
|
184
|
+
Route::resource('recipes', 'RecipeController');
|
185
|
+
Route::post('comments/{recipe}', 'CommentController@store')->name('comments.store');
|
186
|
+
```
|
187
|
+
# blade
|
188
|
+
```php
|
189
|
+
@section('content')
|
190
|
+
<h1>レシピID:{{ $recipe->id }}の詳細ページ</h1>
|
191
|
+
@auth()
|
192
|
+
{!! Form::open(['route' => 'comments.store']) !!}
|
193
|
+
<div class="form-group">
|
194
|
+
{!! Form::textarea('content',['class' => 'form-control']) !!}
|
195
|
+
{!! Form::submit('投稿', ['class' => 'btn btn-primary btn-block']) !!}
|
196
|
+
</div>
|
197
|
+
{!! Form::close() !!}
|
198
|
+
@endauth
|
199
|
+
@foreach($recipe->comments as $comment)
|
200
|
+
<p>{{ $comment->content }}</p>
|
201
|
+
@endforeach
|
202
|
+
@endsection
|
203
|
+
```
|
2
修正
answer
CHANGED
@@ -1,247 +1,1 @@
|
|
1
|
-
自分ならこうしますという形で、回答します。
|
2
|
-
|
3
|
-
# ER
|
4
|
-
|
5
|
-

|
6
|
-
|
7
|
-
# migrations
|
8
|
-
|
9
|
-
```php
|
10
|
-
<?php
|
11
|
-
|
12
|
-
use Illuminate\Database\Migrations\Migration;
|
13
|
-
use Illuminate\Database\Schema\Blueprint;
|
14
|
-
use Illuminate\Support\Facades\Schema;
|
15
|
-
|
16
|
-
class CreateRecipesTable extends Migration
|
17
|
-
{
|
18
|
-
/**
|
19
|
-
* Run the migrations.
|
20
|
-
*
|
21
|
-
* @return void
|
22
|
-
*/
|
23
|
-
public function up()
|
24
|
-
{
|
25
|
-
Schema::create('recipes', function (Blueprint $table) {
|
26
|
-
$table->bigIncrements('id');
|
27
|
-
$table->string('name');
|
28
|
-
$table->unsignedBigInteger('created_by');
|
29
|
-
$table->timestamps();
|
30
|
-
|
31
|
-
$table->foreign('created_by')->references('id')->on('users');
|
32
|
-
});
|
33
|
-
}
|
34
|
-
|
35
|
-
/**
|
36
|
-
* Reverse the migrations.
|
37
|
-
*
|
38
|
-
* @return void
|
39
|
-
*/
|
40
|
-
public function down()
|
41
|
-
{
|
42
|
-
Schema::dropIfExists('recipes');
|
43
|
-
}
|
44
|
-
}
|
45
|
-
```
|
46
|
-
|
47
|
-
```php
|
48
|
-
<?php
|
49
|
-
|
50
|
-
use Illuminate\Database\Migrations\Migration;
|
51
|
-
use Illuminate\Database\Schema\Blueprint;
|
52
|
-
use Illuminate\Support\Facades\Schema;
|
53
|
-
|
54
|
-
|
1
|
+
時間かけて書いた回答に対して、質問者からノーリアクションで、ナメてると思うので消した。
|
55
|
-
{
|
56
|
-
/**
|
57
|
-
* Run the migrations.
|
58
|
-
*
|
59
|
-
* @return void
|
60
|
-
*/
|
61
|
-
public function up()
|
62
|
-
{
|
63
|
-
Schema::create('comments', function (Blueprint $table) {
|
64
|
-
$table->bigIncrements('id');
|
65
|
-
$table->unsignedBigInteger('recipe_id');
|
66
|
-
$table->string('content');
|
67
|
-
$table->unsignedBigInteger('created_by');
|
68
|
-
$table->timestamps();
|
69
|
-
|
70
|
-
$table->foreign('recipe_id')->references('id')->on('recipes');
|
71
|
-
$table->foreign('created_by')->references('id')->on('users');
|
72
|
-
});
|
73
|
-
}
|
74
|
-
|
75
|
-
/**
|
76
|
-
* Reverse the migrations.
|
77
|
-
*
|
78
|
-
* @return void
|
79
|
-
*/
|
80
|
-
public function down()
|
81
|
-
{
|
82
|
-
Schema::dropIfExists('comments');
|
83
|
-
}
|
84
|
-
}
|
85
|
-
```
|
86
|
-
|
87
|
-
# models
|
88
|
-
|
89
|
-
```php
|
90
|
-
<?php
|
91
|
-
|
92
|
-
namespace App;
|
93
|
-
|
94
|
-
use Illuminate\Database\Eloquent\Model;
|
95
|
-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
96
|
-
use Illuminate\Database\Eloquent\Relations\HasMany;
|
97
|
-
|
98
|
-
class Recipe extends Model
|
99
|
-
{
|
100
|
-
protected $fillable = [
|
101
|
-
'name',
|
102
|
-
'createdBy'
|
103
|
-
];
|
104
|
-
|
105
|
-
/**
|
106
|
-
* @return HasMany
|
107
|
-
*/
|
108
|
-
public function comments()
|
109
|
-
{
|
110
|
-
return $this->hasMany(Comment::class);
|
111
|
-
}
|
112
|
-
|
113
|
-
/**
|
114
|
-
* @return BelongsTo
|
115
|
-
*/
|
116
|
-
public function createdBy()
|
117
|
-
{
|
118
|
-
return $this->belongsTo(User::class, 'created_by');
|
119
|
-
}
|
120
|
-
}
|
121
|
-
```
|
122
|
-
|
123
|
-
```php
|
124
|
-
<?php
|
125
|
-
|
126
|
-
namespace App;
|
127
|
-
|
128
|
-
use Illuminate\Database\Eloquent\Model;
|
129
|
-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
130
|
-
|
131
|
-
class Comment extends Model
|
132
|
-
{
|
133
|
-
protected $fillable = [
|
134
|
-
'content',
|
135
|
-
'createdBy'
|
136
|
-
];
|
137
|
-
|
138
|
-
/**
|
139
|
-
* @return BelongsTo
|
140
|
-
*/
|
141
|
-
public function recipe()
|
142
|
-
{
|
143
|
-
return $this->belongsTo(Recipe::class);
|
144
|
-
}
|
145
|
-
|
146
|
-
/**
|
147
|
-
* @return BelongsTo
|
148
|
-
*/
|
149
|
-
public function createdBy()
|
150
|
-
{
|
151
|
-
return $this->belongsTo(User::class, 'created_by');
|
152
|
-
}
|
153
|
-
}
|
154
|
-
```
|
155
|
-
|
156
|
-
# controllers
|
157
|
-
|
158
|
-
```php
|
159
|
-
<?php
|
160
|
-
|
161
|
-
namespace App\Http\Controllers;
|
162
|
-
|
163
|
-
use App\Recipe;
|
164
|
-
use Illuminate\View\View;
|
165
|
-
|
166
|
-
class RecipeController extends Controller
|
167
|
-
{
|
168
|
-
/**
|
169
|
-
* @return View
|
170
|
-
*/
|
171
|
-
public function index()
|
172
|
-
{
|
173
|
-
$recipes = Recipe::query()
|
174
|
-
->with(['comments', 'createdBy'])
|
175
|
-
->paginate();
|
176
|
-
return view('recipes.index', compact('recipes'));
|
177
|
-
}
|
178
|
-
|
179
|
-
/**
|
180
|
-
* @param Recipe $recipe
|
181
|
-
* @return View
|
182
|
-
*/
|
183
|
-
public function show(Recipe $recipe)
|
184
|
-
{
|
185
|
-
return view('recipes.show', compact('recipe'));
|
186
|
-
}
|
187
|
-
}
|
188
|
-
```
|
189
|
-
|
190
|
-
```php
|
191
|
-
<?php
|
192
|
-
|
193
|
-
namespace App\Http\Controllers;
|
194
|
-
|
195
|
-
use App\Comment;
|
196
|
-
use App\Recipe;
|
197
|
-
use Illuminate\Http\RedirectResponse;
|
198
|
-
use Illuminate\Http\Request;
|
199
|
-
use Illuminate\Support\Facades\Auth;
|
200
|
-
|
201
|
-
class CommentController extends Controller
|
202
|
-
{
|
203
|
-
/**
|
204
|
-
* @param Request $request
|
205
|
-
* @param Recipe $recipe
|
206
|
-
* @return RedirectResponse
|
207
|
-
*/
|
208
|
-
public function store(Request $request, Recipe $recipe)
|
209
|
-
{
|
210
|
-
$recipe->comments()->save(new Comment([
|
211
|
-
'content' => $request->get('content'),
|
212
|
-
'created_by' => Auth::id()
|
213
|
-
]));
|
214
|
-
|
215
|
-
return redirect()->route('recipes', compact('recipe'));
|
216
|
-
}
|
217
|
-
}
|
218
|
-
```
|
219
|
-
|
220
|
-
# routing
|
221
|
-
|
222
|
-
```php
|
223
|
-
Route::resource('recipes', 'RecipeController');
|
224
|
-
Route::post('comments/{recipe}', 'CommentController@store')->name('comments.store');
|
225
|
-
```
|
226
|
-
|
227
|
-
# blade
|
228
|
-
|
229
|
-
```php
|
230
|
-
@section('content')
|
231
|
-
<h1>レシピID:{{ $recipe->id }}の詳細ページ</h1>
|
232
|
-
|
233
|
-
@auth()
|
234
|
-
{!! Form::open(['route' => 'comments.store']) !!}
|
235
|
-
<div class="form-group">
|
236
|
-
{!! Form::textarea('content',['class' => 'form-control']) !!}
|
237
|
-
{!! Form::submit('投稿', ['class' => 'btn btn-primary btn-block']) !!}
|
238
|
-
</div>
|
239
|
-
{!! Form::close() !!}
|
240
|
-
@endauth
|
241
|
-
|
242
|
-
@foreach($recipe->comments as $comment)
|
243
|
-
<p>{{ $comment->content }}</p>
|
244
|
-
@endforeach
|
245
|
-
|
246
|
-
@endsection
|
247
|
-
```
|
1
追記
answer
CHANGED
@@ -222,4 +222,26 @@
|
|
222
222
|
```php
|
223
223
|
Route::resource('recipes', 'RecipeController');
|
224
224
|
Route::post('comments/{recipe}', 'CommentController@store')->name('comments.store');
|
225
|
+
```
|
226
|
+
|
227
|
+
# blade
|
228
|
+
|
229
|
+
```php
|
230
|
+
@section('content')
|
231
|
+
<h1>レシピID:{{ $recipe->id }}の詳細ページ</h1>
|
232
|
+
|
233
|
+
@auth()
|
234
|
+
{!! Form::open(['route' => 'comments.store']) !!}
|
235
|
+
<div class="form-group">
|
236
|
+
{!! Form::textarea('content',['class' => 'form-control']) !!}
|
237
|
+
{!! Form::submit('投稿', ['class' => 'btn btn-primary btn-block']) !!}
|
238
|
+
</div>
|
239
|
+
{!! Form::close() !!}
|
240
|
+
@endauth
|
241
|
+
|
242
|
+
@foreach($recipe->comments as $comment)
|
243
|
+
<p>{{ $comment->content }}</p>
|
244
|
+
@endforeach
|
245
|
+
|
246
|
+
@endsection
|
225
247
|
```
|