2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravelにてidの値を取得できない

Posted at

Laravelのモデルidに文字列を使うとidを取得した際にidの値が0になる。

test_model
CREATE TABLE `test_model` (
  `id` varchar(5) NOT NULL,
  `user` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
COMMIT;

INSERT INTO `test_model`(`id`, `user`) VALUES ('id1','user1');

TestModelを作成

php artisan make:model TestModel
TestModel
class TestModel extends Model
{
    protected $table = 'test_model';
}
>>> $obj = TestModel::first();
=> App\Models\TestModel {
     id: "id1",
     user: "user1",
   }
>>> $obj->id;
=> 0

モデルに以下を追加するとidが正常に取得できる。

public $incrementing = false;
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?