表題の通りなのですが、
フォームからPOST送信しmysql保存した後のid(インクリメントid)を取得したいのですが、
下記コードを実行すると、 mysql保存までは確認できるのですが、
dd表示が null表示になってしまいます。
いくつもググって見たのですが原因がわからず・・
ご知見ある方の助言をいただければ幸いです。
よろしくお願いいたします。
コントローラ
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Customer; use App\Http\Requests; public function store(Request $request) { $customer = new Customer(); $customer->customer_name = $request->customer_name; $customer->save(); $last_insert_id = $customer->id; dd($last_insert_id); }
2018/08/08追記
コメント頂いた内容を試してみました。
・変数無しで dd($last_insert_id);
→ null
・$customerを取得
dd($customer->toArray()); array:5 [▼ "customer_name" => "test0902" "visible_flag" => 1 "updated_at" => "2018-08-08 09:02:26" "created_at" => "2018-08-08 09:02:26" "customer_id" => 54 ]
指定していないcustomer_id が含まれていたのでプログラムどこか間違っていそうです。。
migration
1 public function up() 2 { 3 Schema::create('customers', function (Blueprint $table) { 4 $table->increments('id'); 5 $table->integer('customer_id'); 6 $table->string('customer_name'); 7 $table->boolean('visible_flag'); 8 $table->integer('sortNo'); 9 $table->timestamps(); 10 }); 11 }
model
class Customer extends Model { // protected $primaryKey = "customer_id"; // public function tasks() { return $this->hasMany('App\Task'); } }
テーブル
mysql
1show columns from customers; 2 3+---------------+------------------+------+-----+---------+----------------+ 4| Field | Type | Null | Key | Default | Extra | 5+---------------+------------------+------+-----+---------+----------------+ 6| id | int(10) unsigned | NO | PRI | NULL | auto_increment | 7| customer_id | int(11) | NO | | NULL | | 8| customer_name | varchar(255) | NO | | NULL | | 9| created_at | timestamp | YES | | NULL | | 10| updated_at | timestamp | YES | | NULL | | 11| visible_flag | tinyint(1) | YES | | NULL | | 12| sort_no | int(11) | YES | | NULL | | 13+---------------+------------------+------+-----+---------+----------------+

回答3件
あなたの回答
tips
プレビュー