質問編集履歴

1

モデル、テーブルのコード追加

2021/10/26 02:57

投稿

yren
yren

スコア5

test CHANGED
File without changes
test CHANGED
@@ -40,6 +40,14 @@
40
40
 
41
41
  ```php
42
42
 
43
+ //ItemController.php
44
+
45
+
46
+
47
+ use App\Models\Item;
48
+
49
+
50
+
43
51
  public function exchanged(string $id) {
44
52
 
45
53
  //交換するために交渉に出した商品
@@ -90,6 +98,144 @@
90
98
 
91
99
 
92
100
 
101
+ ```php
102
+
103
+ //app/Models/Item.php
104
+
105
+
106
+
107
+ class Item extends Model
108
+
109
+ {
110
+
111
+ use SoftDeletes;
112
+
113
+
114
+
115
+ protected $fillable = [
116
+
117
+ 'name', 'description', 'genre', 'what_you_want', 'quality', 'post_day', 'negotiation', 'finished'
118
+
119
+ ];
120
+
121
+
122
+
123
+ protected $hidden = [
124
+
125
+ self::CREATED_AT, self::UPDATED_AT
126
+
127
+ ];
128
+
129
+
130
+
131
+
132
+
133
+ public function user() {
134
+
135
+ return $this->belongsTo(User::class);
136
+
137
+ }
138
+
139
+
140
+
141
+ public function chats() {
142
+
143
+ return $this->hasMany(Chat::class);
144
+
145
+ }
146
+
147
+
148
+
149
+ public function trade() {
150
+
151
+ return $this->hasOne(Trade::class);
152
+
153
+ }
154
+
155
+
156
+
157
+ public function photos() {
158
+
159
+ return $this->hasMany(Photo::class);
160
+
161
+ }
162
+
163
+ }
164
+
165
+ ```
166
+
167
+
168
+
169
+ ```php
170
+
171
+ //create_items_tables.php
172
+
173
+
174
+
175
+ class CreateItemsTable extends Migration
176
+
177
+ {
178
+
179
+ /**
180
+
181
+ * Run the migrations.
182
+
183
+ *
184
+
185
+ * @return void
186
+
187
+ */
188
+
189
+ public function up()
190
+
191
+ {
192
+
193
+ Schema::create('items', function (Blueprint $table) {
194
+
195
+ $table->bigIncrements('id');
196
+
197
+ $table->string('name', 20)->nullable();
198
+
199
+ $table->string('genre')->nullable();
200
+
201
+ $table->string('quality')->nullable();
202
+
203
+ $table->string('post_day')->nullable();
204
+
205
+ $table->string('what_you_want')->nullable();
206
+
207
+ $table->string('description')->nullable();
208
+
209
+ $table->integer('negotiation')->default(0);
210
+
211
+ $table->integer('finished')->default(0);
212
+
213
+ $table->softDeletes();
214
+
215
+ $table->timestamps();
216
+
217
+
218
+
219
+ $table->foreignId('user_id')
220
+
221
+ ->constrained()
222
+
223
+ ->onDelete('cascade')
224
+
225
+ ->onUpdate('cascade');
226
+
227
+
228
+
229
+ });
230
+
231
+ }
232
+
233
+ ```
234
+
235
+
236
+
237
+
238
+
93
239
  ### 試したこと
94
240
 
95
241