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

質問編集履歴

1

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

2021/10/26 02:57

投稿

yren
yren

スコア5

title CHANGED
File without changes
body CHANGED
@@ -19,6 +19,10 @@
19
19
  ### 該当のソースコード
20
20
 
21
21
  ```php
22
+ //ItemController.php
23
+
24
+ use App\Models\Item;
25
+
22
26
  public function exchanged(string $id) {
23
27
  //交換するために交渉に出した商品
24
28
  $item = Photo::with(['item', 'user'])->where('id', $id)->first();
@@ -44,6 +48,75 @@
44
48
  }
45
49
  ```
46
50
 
51
+ ```php
52
+ //app/Models/Item.php
53
+
54
+ class Item extends Model
55
+ {
56
+ use SoftDeletes;
57
+
58
+ protected $fillable = [
59
+ 'name', 'description', 'genre', 'what_you_want', 'quality', 'post_day', 'negotiation', 'finished'
60
+ ];
61
+
62
+ protected $hidden = [
63
+ self::CREATED_AT, self::UPDATED_AT
64
+ ];
65
+
66
+
67
+ public function user() {
68
+ return $this->belongsTo(User::class);
69
+ }
70
+
71
+ public function chats() {
72
+ return $this->hasMany(Chat::class);
73
+ }
74
+
75
+ public function trade() {
76
+ return $this->hasOne(Trade::class);
77
+ }
78
+
79
+ public function photos() {
80
+ return $this->hasMany(Photo::class);
81
+ }
82
+ }
83
+ ```
84
+
85
+ ```php
86
+ //create_items_tables.php
87
+
88
+ class CreateItemsTable extends Migration
89
+ {
90
+ /**
91
+ * Run the migrations.
92
+ *
93
+ * @return void
94
+ */
95
+ public function up()
96
+ {
97
+ Schema::create('items', function (Blueprint $table) {
98
+ $table->bigIncrements('id');
99
+ $table->string('name', 20)->nullable();
100
+ $table->string('genre')->nullable();
101
+ $table->string('quality')->nullable();
102
+ $table->string('post_day')->nullable();
103
+ $table->string('what_you_want')->nullable();
104
+ $table->string('description')->nullable();
105
+ $table->integer('negotiation')->default(0);
106
+ $table->integer('finished')->default(0);
107
+ $table->softDeletes();
108
+ $table->timestamps();
109
+
110
+ $table->foreignId('user_id')
111
+ ->constrained()
112
+ ->onDelete('cascade')
113
+ ->onUpdate('cascade');
114
+
115
+ });
116
+ }
117
+ ```
118
+
119
+
47
120
  ### 試したこと
48
121
 
49
122
  $item_idの部分を$item->item->idなど別の書き方で書いてみましたが結果は変わりませんでした。