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

質問編集履歴

3

追記します

2018/08/08 00:13

投稿

aoki_monpro
aoki_monpro

スコア45

title CHANGED
File without changes
body CHANGED
@@ -13,8 +13,6 @@
13
13
 
14
14
  コントローラ
15
15
  ```
16
-
17
-
18
16
  <?php
19
17
 
20
18
  namespace App\Http\Controllers;
@@ -44,6 +42,8 @@
44
42
  → null
45
43
 
46
44
  ・$customerを取得
45
+ ```
46
+
47
47
  dd($customer->toArray());
48
48
  array:5 [▼
49
49
  "customer_name" => "test0902"
@@ -52,7 +52,9 @@
52
52
  "created_at" => "2018-08-08 09:02:26"
53
53
  "customer_id" => 54
54
54
  ]
55
+ ```
55
56
 
57
+
56
58
  指定していないcustomer_id が含まれていたのでプログラムどこか間違っていそうです。。
57
59
 
58
60
  ```migration

2

変数なしでddをかけてみました。

2018/08/08 00:12

投稿

aoki_monpro
aoki_monpro

スコア45

title CHANGED
File without changes
body CHANGED
@@ -35,4 +35,69 @@
35
35
  dd($last_insert_id);
36
36
 
37
37
  }
38
+ ```
39
+
40
+ 2018/08/08追記
41
+ コメント頂いた内容を試してみました。
42
+
43
+ ・変数無しで dd($last_insert_id);
44
+ → null
45
+
46
+ ・$customerを取得
47
+ dd($customer->toArray());
48
+ array:5 [▼
49
+ "customer_name" => "test0902"
50
+ "visible_flag" => 1
51
+ "updated_at" => "2018-08-08 09:02:26"
52
+ "created_at" => "2018-08-08 09:02:26"
53
+ "customer_id" => 54
54
+ ]
55
+
56
+ 指定していないcustomer_id が含まれていたのでプログラムどこか間違っていそうです。。
57
+
58
+ ```migration
59
+ public function up()
60
+ {
61
+ Schema::create('customers', function (Blueprint $table) {
62
+ $table->increments('id');
63
+ $table->integer('customer_id');
64
+ $table->string('customer_name');
65
+ $table->boolean('visible_flag');
66
+ $table->integer('sortNo');
67
+ $table->timestamps();
68
+ });
69
+ }
70
+ ```
71
+
72
+ model
73
+ ```
74
+ class Customer extends Model
75
+ {
76
+ //
77
+ protected $primaryKey = "customer_id";
78
+ //
79
+ public function tasks()
80
+ {
81
+ return $this->hasMany('App\Task');
82
+ }
83
+
84
+ }
85
+
86
+ ```
87
+
88
+ テーブル
89
+ ```mysql
90
+ show columns from customers;
91
+
92
+ +---------------+------------------+------+-----+---------+----------------+
93
+ | Field | Type | Null | Key | Default | Extra |
94
+ +---------------+------------------+------+-----+---------+----------------+
95
+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
96
+ | customer_id | int(11) | NO | | NULL | |
97
+ | customer_name | varchar(255) | NO | | NULL | |
98
+ | created_at | timestamp | YES | | NULL | |
99
+ | updated_at | timestamp | YES | | NULL | |
100
+ | visible_flag | tinyint(1) | YES | | NULL | |
101
+ | sort_no | int(11) | YES | | NULL | |
102
+ +---------------+------------------+------+-----+---------+----------------+
38
103
  ```

1

id ・・ インクリメントidのことです。

2018/08/08 00:11

投稿

aoki_monpro
aoki_monpro

スコア45

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  表題の通りなのですが、
2
2
 
3
- フォームからPOST送信しmysql保存した後のidを取得したいのですが、
3
+ フォームからPOST送信しmysql保存した後のid(インクリメントid)を取得したいのですが、
4
4
  下記コードを実行すると、 mysql保存までは確認できるのですが、
5
5
  dd表示が null表示になってしまいます。
6
6