{"id":408,"date":"2024-11-01T17:20:00","date_gmt":"2024-11-01T08:20:00","guid":{"rendered":"https:\/\/syrup-factory.com\/memo\/?p=408"},"modified":"2024-11-02T10:47:59","modified_gmt":"2024-11-02T01:47:59","slug":"laravel%e3%81%a7%e6%9b%b4%e6%96%b0%e6%99%82%e3%83%ad%e3%83%83%e3%82%af%e3%82%92%e3%81%8b%e3%81%91%e3%82%8b","status":"publish","type":"post","link":"https:\/\/syrup-factory.com\/memo\/archives\/408","title":{"rendered":"laravel\u3067\u66f4\u65b0\u6642\u30ed\u30c3\u30af\u3092\u304b\u3051\u308b"},"content":{"rendered":"\n<p>laravel\u3067\u66f4\u65b0\u6642\u30ed\u30c3\u30af\u3092\u304b\u3051\u308b\u969b\u3001\u30ec\u30b3\u30fc\u30c9\u30ed\u30c3\u30af\u3068\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af\u3067\u306f\u66f8\u304d\u65b9\u304c\u7570\u306a\u308b\u30fb\u4e21\u8005\u3092\u540c\u6642\u306b\u4f7f\u3048\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u52d5\u4f5c\u691c\u8a3c \uff1a laravel 10 \/ mysql<\/p>\n\n\n\n<h2>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3<\/h2>\n\n\n\n<p>\u30ec\u30b3\u30fc\u30c9\u66f4\u65b0\u6642\u306b\u30ed\u30c3\u30af\u3092\u304b\u3051\u3001\u66f4\u65b0\u3059\u308b\u3002\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u3067\u5b9f\u884c\u3059\u308b\u3002<\/p>\n\n\n\n<p>\u2193DB\u30d5\u30a1\u30b6\u30fc\u30c9\u306etransaction\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3002\u30b3\u30df\u30c3\u30c8\u3084\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u304c\u81ea\u52d5\u3067\u884c\u308f\u308c\u308b\u3002<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$params = [&#039;studentId&#039; =&gt; &#039;student001&#039;, &#039;studentName&#039; =&gt; &#039;\u3066\u3059\u3068\u82b1\u5b50&#039;];\nDB::transaction(function () use ($params) {\n    \/\/ \u6307\u5b9a\u306e\u751f\u5f92\u3092\u53d6\u5f97\u30fb\u30ed\u30c3\u30af\u3059\u308b\u2605\n    \/\/ \u8a72\u5f53\u306e\u751f\u5f92\u306e\u9078\u629e\u8b1b\u5ea7\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u30ed\u30c3\u30af\u3059\u308b\u2605\u2605\n\n    \/\/ \u30ec\u30b3\u30fc\u30c9\u66f4\u65b0\u3059\u308b\n}, 3);\n<\/pre>\n\n\n<h2>\u5bfe\u8c61\u30ec\u30b3\u30fc\u30c9\u306e\u5c02\u6709\u30ed\u30c3\u30af \u2605<\/h2>\n\n\n\n<ul><li>\u3042\u308b\u751f\u5f92\u30ec\u30b3\u30fc\u30c9\u306b\u5bfe\u3057\u3066\u306e\u5c02\u6709\u30ed\u30c3\u30af\u3002\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u7d42\u308f\u308b\u307e\u3067\u53c2\u7167\u66f4\u65b0\u4e0d\u53ef\u3002<\/li><\/ul>\n\n\n\n<p>\u2193 Model\uff08\u30af\u30a8\u30ea\u30d3\u30eb\u30c0\uff09\u5229\u7528\u306e\u5834\u5408\u3002OK\u3001\u30ed\u30c3\u30af\u304b\u304b\u308b<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::transaction(function () use ($params) {\n    \/\/ \u6307\u5b9a\u306e\u751f\u5f92\u3092\u53d6\u5f97\u30fb\u30ed\u30c3\u30af\u3059\u308b\n    $student = Student::where(&#039;student_id&#039;, $params[&#039;studentId&#039;])-&gt;lockForUpdate()-&gt;first();\n    \/\/ \u6307\u5b9a\u306e\u751f\u5f92\u3092\u66f4\u65b0\n    $student-&gt;student_name = $params[&#039;studentName&#039;];\n    $student-&gt;save();\n}, 3);\n<\/pre>\n\n\n<p>\u2193 DB\u30af\u30e9\u30b9\uff08SQL\u30af\u30a8\u30ea\uff09\u5229\u7528\u306e\u5834\u5408\u3002OK\u3001\u30ed\u30c3\u30af\u304b\u304b\u308b<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::transaction(function () use ($params) {\n    \/\/ \u6307\u5b9a\u306e\u751f\u5f92\u3092\u53d6\u5f97\u30fb\u30ed\u30c3\u30af\u3059\u308b\n    $sql = &quot;SELECT * FROM students WHERE student_id = :studentId FOR UPDATE&quot;;\n    $result = DB::select($sql, [&#039;studentId&#039; =&gt; $params[&#039;studentId&#039;]]);\n    \/\/ \u6307\u5b9a\u306e\u751f\u5f92\u3092\u66f4\u65b0\n    $sqlu = &quot;UPDATE students SET student_name = :studentName WHERE student_id = :studentId&quot;;\n    $resultu = DB::update($sqlu, $params);\n}, 3);\n<\/pre>\n\n\n<h2>\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af \u2605\u2605<\/h2>\n\n\n\n<ul><li>\u3042\u308b\u751f\u5f92\u306e\u3001\u9078\u629e\u8b1b\u5ea7\u8907\u6570\u30ec\u30b3\u30fc\u30c9\u306e\u8ffd\u52a0\u30fb\u66f4\u65b0\u30fb\u524a\u9664\u51e6\u7406\u3002<br>\u8b1b\u5ea7\u306e\u4e88\u7d04\u304c\u6e80\u5e2d\u304b\u7b49\u306e\u53c2\u7167\u3082\u3001\u4ef6\u6570\u304c\u5909\u52d5\u3059\u308b\u305f\u3081\u51e6\u7406\u304c\u7d42\u308f\u308b\u307e\u3067\u306f\u4e0d\u53ef\u3002\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af\u3068\u3059\u308b\u3002<\/li><li>Eloquent\u306e\u30af\u30a8\u30ea\u30d3\u30eb\u30c0\u3092\u4f7f\u3046\u65b9\u6cd5\u306f\u306a\u3055\u305d\u3046\u3002\u751f\u30af\u30a8\u30ea\u5b9f\u884c\u3059\u308b\u3002<\/li><\/ul>\n\n\n\n<p>\uff08\u53c2\u8003\uff09<br>https:\/\/qiita.com\/zackey2\/items\/079128df6567091b8a1c<\/p>\n\n\n\n<p>\u2193 OK\u3001\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af\u304b\u304b\u308b<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::transaction(function () use ($params) {\n    \/\/ \u81ea\u52d5\u30b3\u30df\u30c3\u30c8\u3092OFF\u306b\u3059\u308b\n    DB::unprepared(&#039;SET AUTOCOMMIT = 0&#039;);\n    \/\/ \u9078\u629e\u8b1b\u5ea7\u30c6\u30fc\u30d6\u30eb\u3092\u30ed\u30c3\u30af\u3059\u308b\n    DB::unprepared(&#039;LOCK TABLES selected_lectures WRITE&#039;);\n    \/\/ \u9078\u629e\u8b1b\u5ea7\u306e\u8ffd\u52a0\u30fb\u66f4\u65b0\u30fb\u524a\u9664\n    \/\/\uff08\u7565\uff09\n}, 3);\n<\/pre>\n\n\n<h2>\u300c\u5bfe\u8c61\u30ec\u30b3\u30fc\u30c9\u306e\u5c02\u6709\u30ed\u30c3\u30af\uff0b\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af\u300d\u306f \u30a8\u30e9\u30fc\u306b\u306a\u308b<\/h2>\n\n\n\n<p>\u4e0a\u8a182\u3064\u3001\u5bfe\u8c61\u751f\u5f92\u306e\u30ec\u30b3\u30fc\u30c9\u3068\u3001\u305d\u306e\u751f\u5f92\u306e\u9078\u629e\u8b1b\u5ea7\u30c6\u30fc\u30d6\u30eb \u3092\u540c\u6642\u306b\u30ed\u30c3\u30af\u3059\u308b\u3002<br>\u5358\u7d14\u306b2\u3064\u3092\u4f75\u8a18\u3059\u308b\u3068\u30fb\u30fbNG\u3001\u30a8\u30e9\u30fc\u306b\u306a\u3063\u305f\u3002<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$param = [];\nDB::transaction(function () use ($params) {\n    DB::unprepared(&#039;SET AUTOCOMMIT = 0&#039;);\n    \/\/ \u9078\u629e\u8b1b\u5ea7\u30c6\u30fc\u30d6\u30eb\u3092\u30ed\u30c3\u30af\u3059\u308b\n    DB::unprepared(&#039;LOCK TABLES selected_lectures WRITE&#039;);\n\n    \/\/ \u6307\u5b9a\u306e\u751f\u5f92\u3092\u53d6\u5f97\u30fb\u30ed\u30c3\u30af\u3059\u308b\u2605\n    $sql = &quot;SELECT * FROM students WHERE student_id = :studentId FOR UPDATE&quot;;\n    $result = DB::select($sql, [&#039;studentId&#039; =&gt; $params[&#039;studentId&#039;]]);\n\n    \/\/ \u6307\u5b9a\u306e\u751f\u5f92\u3092\u66f4\u65b0\n    $sqlu = &quot;UPDATE students SET student_name = :studentName WHERE student_id = :studentId&quot;;\n    $resultu = DB::update($sqlu, $params);\n\n    \/\/ \u9078\u629e\u8b1b\u5ea7\u306e\u8ffd\u52a0\u30fb\u66f4\u65b0\u30fb\u524a\u9664\n    \/\/\uff08\u7565\uff09\n}, 3);\n<\/pre>\n\n\n<p>DB::update()\u3067\u3001students\u304c\u30ed\u30c3\u30af\u3055\u308c\u3066\u306a\u3044\u3068\u8a00\u308f\u308c\u308b\u3002\u2605\u304c\u52b9\u3044\u3066\u3044\u306a\u3044\u6a21\u69d8<br><code>SQLSTATE[HY000]: General error: 1100 Table 'students' was not locked with LOCK TABLES<\/code><\/p>\n\n\n\n<h2>\u8907\u6570\u30c6\u30fc\u30d6\u30eb\u3092\u30ed\u30c3\u30af\uff1aOK<\/h2>\n\n\n\n<p>students\u3082\u3001\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af\u6307\u5b9a\u3059\u308b\u3068OK\u3060\u3063\u305f\u3002<br>\u30ed\u30c3\u30af\u7bc4\u56f2\u304c\u5927\u304d\u304f\u306a\u308b\u306e\u3067students\u306f\u7279\u5b9a\u306e\u30ec\u30b3\u30fc\u30c9\u306e\u307f\u306e\u30ed\u30c3\u30af\u3067\u3088\u3044\u306e\u3060\u304c\u3001\u3067\u304d\u306a\u3044\u3063\u307d\u3044\u3002<\/p>\n\n\n\n<p>\u2193 OK\u3001\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af\u304b\u304b\u308b<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::transaction(function () use ($params) {\n    DB::unprepared(&#039;SET AUTOCOMMIT = 0&#039;);\n    \/\/ \u9078\u629e\u8b1b\u5ea7\u30c6\u30fc\u30d6\u30eb\u3001\u751f\u5f92\u30c6\u30fc\u30d6\u30eb\u3082\u30ed\u30c3\u30af\u3059\u308b\n    DB::unprepared(&#039;LOCK TABLES selected_lectures WRITE, students WRITE&#039;);\n\n    \/\/ FOR UPDATE\u306a\u3057\u3067\u53d6\u5f97\n    $sql = &quot;SELECT * FROM students WHERE student_id = :studentId&quot;;\n    $result = DB::select($sql, [&#039;studentId&#039; =&gt; $params[&#039;studentId&#039;]]);\n\n    \/\/ \u6307\u5b9a\u306e\u751f\u5f92\u3092\u66f4\u65b0\n    $sqlu = &quot;UPDATE students SET student_name = :studentName WHERE student_id = :studentId&quot;;\n    $resultu = DB::update($sqlu, $params);\n\n    \/\/ \u9078\u629e\u8b1b\u5ea7\u306e\u8ffd\u52a0\u30fb\u66f4\u65b0\u30fb\u524a\u9664\n    \/\/\uff08\u7565\uff09\n}, 3);\n<\/pre>\n\n\n<h2>\u304a\u307e\u3051\uff1a\u81ea\u52d5\u30b3\u30df\u30c3\u30c8\u6307\u5b9a\u306a\u3057\u306e\u30a8\u30e9\u30fc<\/h2>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::unprepared(&#039;SET AUTOCOMMIT = 0&#039;);\n<\/pre>\n\n\n<p>\u304c\u306a\u3044\u3068\u3001<br><code>There is no active transaction<\/code><br>\u306e\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002<br>DB::update()\u6642\u70b9\u3067\u81ea\u52d5\u30b3\u30df\u30c3\u30c8\u30fb\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u7d42\u4e86\u3057\u3001DB::transaction()\u629c\u3051\u308b\u6642\u306b\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304c\u306a\u3044\uff01\u3068\u3044\u3046\u3053\u3068\u307f\u305f\u3044\u3002<\/p>\n\n\n\n<h2>\u304a\u307e\u3051\uff1a\u30ed\u30c3\u30af\u4e2d\u3001model\u304b\u3089\u30ea\u30f3\u30af\u30c6\u30fc\u30d6\u30eb\u3092\u53c2\u7167\u3059\u308b\u3068\u30a8\u30e9\u30fc<\/h2>\n\n\n\n<p>\u4e88\u7d04\u30b7\u30b9\u30c6\u30e0\u306e\u5b9f\u30b3\u30fc\u30c9\u3001\u30e2\u30c7\u30eb\u3092\u5229\u7528\u3057\u305f\u5834\u5408\u306b\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u3002<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\n public static function findBystudentId(studentId$studentId, bool $lockForUpdate = false) : ?StudentEntity\n  {\n\n    if ($lockForUpdate === true) {\n      DB::unprepared(&#039;SET AUTOCOMMIT = 0&#039;);\n       \/\/\u751f\u5f92\u30c6\u30fc\u30d6\u30eb\u3068\u3001\u9078\u629e\u8b1b\u5ea7\u30c6\u30fc\u30d6\u30eb\u3082\u30ed\u30c3\u30af\u3059\u308b\n      DB::unprepared(&#039;LOCK TABLES students WRITE, selected_lectures WRITE&#039;);\n    }\n\n    $student = Student::where(&#039;student_id&#039;, $studentId-&gt;toString())-&gt;first();\n    if (!$student) {\n      return $student;\n    }\n\n    \/\/\uff08\u7565\uff09\n\n    return $student-&gt;toEntity();  \/\/\u2605\u2605\u2605\n  }\n<\/pre>\n\n\n<p><code>General error: 1100 Table 'lectures' was not locked with LOCK TABLES at \/var\/www\/html\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php:423)<\/code><\/p>\n\n\n\n<p>\u6700\u5f8c\u306e\u2605\u2605\u2605\u5185\u306e\u51e6\u7406\u3067\u3001\u547c\u3093\u3067\u3044\u308bmodel\u306b\u8b1b\u5ea7\u30c6\u30fc\u30d6\u30eb\uff08lectures\uff09\u3078\u306e\u30ea\u30f3\u30af\u304c\u3042\u3063\u305f\u306e\u304c\u539f\u56e0\u3002<br>\u53c2\u7167\u3057\u3066\u3044\u308b\u3060\u3051\u306a\u306e\u306b\u30fb\u30fb\u30fb<br>\uff08\u30ea\u30f3\u30af\u3057\u3066\u3044\u308b\u3068\u4fdd\u5b58\u3082\u53ef\u80fd\u306a\u306e\u304b\uff1f\u3060\u304b\u3089\u30ed\u30c3\u30af\u3092\u8981\u6c42\u3055\u308c\u3066\u3044\u308b\uff1f\uff1f \u672a\u8abf\u67fb\u3002\uff09<\/p>\n\n\n\n<p>\u89e3\u6c7a\u65b9\u6cd5\uff1aLOCK TABLES \u306b \u300clectures READ\u300d\u3092\u8ffd\u52a0\u3057\u305f\u3002\u7121\u7528\u306a\u30ed\u30c3\u30af\u304c\u5897\u3048\u305f.<br>model\u306e\u30ea\u30f3\u30af\u306a\u3069laravel\u306f\u81ea\u52d5\u3067\u4fbf\u5229\u306a\u6a5f\u80fd\u3082\u591a\u3044\u3051\u3069\u3001\u6761\u4ef6\u304c\u91cd\u306a\u308b\u3068\u4e0d\u90fd\u5408\u304c\u51fa\u3066\u304f\u308b\u3002\u4ed5\u65b9\u306a\u3044\u3068\u8a00\u3048\u3070\u4ed5\u65b9\u306a\u3044\u3002<br>\u203b\u3082\u3057\u304b\u3057\u305f\u3089\u30ea\u30f3\u30af\u3092\u7121\u52b9\u306b\u3059\u308b\u65b9\u6cd5\u304c\u3042\u308b\u306e\u304b\u3082\u3057\u308c\u306a\u3044\u3002\u672a\u8abf\u67fb<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nDB::unprepared(&#039;LOCK TABLES students WRITE, selected_lectures WRITE, lectures READ&#039;);\n<\/pre>\n\n\n<p>model:\u30ea\u30f3\u30af\u3092\u53c2\u7167\u3057\u3066\u3044\u308b\u90e8\u5206<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nnamespace App\\Models;\n\nclass SelectedLecture extends Model\n\n\n    \/\/ \u30ea\u30f3\u30af\n    public function lecture()\n    {\n        return $this-&gt;belongsTo(Lecture::class, &#039;lecture_id&#039;, &#039;lecture_id&#039;);\n    }\n\n\n    public function toEntity(): SelectedLectureEntity\n    {\n        \/\/\u7565\n\n        if ($this-&gt;lecture) {\u3000\/\/\u30b3\u30b3!!!!!\n            $selectedLectureEntity-&gt;setLecture($this-&gt;lecture-&gt;toEntity());\n        }\n        return $selectedLectureEntity;\n    }\n<\/pre>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>laravel\u3067\u66f4\u65b0\u6642\u30ed\u30c3\u30af\u3092\u304b\u3051\u308b\u969b\u3001\u30ec\u30b3\u30fc\u30c9\u30ed\u30c3\u30af\u3068\u30c6\u30fc\u30d6\u30eb\u30ed\u30c3\u30af\u3067\u306f\u66f8\u304d\u65b9\u304c\u7570\u306a\u308b\u30fb\u4e21\u8005\u3092\u540c\u6642 &#8230;<\/p>\n<p> <a class=\"continue-reading-link\" href=\"https:\/\/syrup-factory.com\/memo\/archives\/408\"><span>Continue reading<\/span><i class=\"crycon-right-dir\"><\/i><\/a> <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,5],"tags":[],"_links":{"self":[{"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/posts\/408"}],"collection":[{"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/comments?post=408"}],"version-history":[{"count":8,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/posts\/408\/revisions"}],"predecessor-version":[{"id":418,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/posts\/408\/revisions\/418"}],"wp:attachment":[{"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/media?parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/categories?post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/syrup-factory.com\/memo\/wp-json\/wp\/v2\/tags?post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}