{"id":699,"date":"2018-07-05T17:57:28","date_gmt":"2018-07-05T08:57:28","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=699"},"modified":"2018-07-05T18:41:55","modified_gmt":"2018-07-05T09:41:55","slug":"laravel%e3%83%bb%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%ae%e3%83%87%e3%83%bc%e3%82%bf%e6%93%8d%e4%bd%9c%ef%bc%88%e8%bf%bd%e5%8a%a0%ef%bc%8f%e5%a4%89%e6%9b%b4%ef%bc%8f%e5%89%8a","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=699","title":{"rendered":"Laravel\u30fb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u64cd\u4f5c\uff08\u8ffd\u52a0\uff0f\u5909\u66f4\uff0f\u524a\u9664\uff09\u3059\u308b\u5168\u5b9f\u4f8b"},"content":{"rendered":"<p>\u3055\u3066\u3001\u524d\u56de\u306f\u8a18\u4e8b\u5185\u5bb9\u304c20,000\u6587\u5b57\u3092\u8d8a\u3048\u308b\u9577\u7de8\u8a18\u4e8b\u300c<a href=\"https:\/\/blog.capilano-fw.com\/?p=665\">Laravel\u30fb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u5168\u5b9f\u4f8b<\/a>\u300d\u3092\u304a\u5c4a\u3051\u3057\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306f\u305d\u306e\u7d9a\u7de8\u3068\u3057\u3066<strong>\u300cDB\u306e\u30c7\u30fc\u30bf\u306e\u8ffd\u52a0\uff0f\u7de8\u96c6\uff0f\u524a\u9664\u300d<\/strong>\u306b\u7126\u70b9\u3092\u5f53\u3066\u3066\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u203b\u5b9f\u3092\u8a00\u3046\u3068\u3053\u306e\uff12\u3064\u306f\u5143\u3005\u3072\u3068\u3064\u306e\u8a18\u4e8b\u306b\u3059\u308b\u3064\u3082\u308a\u3067\u3057\u305f\u304c\u3042\u307e\u308a\u306b\u3082\u81a8\u5927\u306b\u306a\u308a\u3059\u304e\u308b\u3053\u3068\u304c\u308f\u304b\u3063\u305f\u306e\u3067\u3001\u6025\u907d\u5206\u5272\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3067\u306f\u3001\u65e9\u901f\u3072\u3068\u3064\u305a\u3064\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-427 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/24.png\" alt=\"\" width=\"182\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/24.png 182w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/24-171x300.png 171w\" sizes=\"auto, (max-width: 182px) 100vw, 182px\" \/><\/p>\n<p>\u3010\u74b0\u5883\u3011<\/p>\n<p>Laravel 5.6<br \/>\nMySQL 5.7<\/p>\n<h1>DB\u306b\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b<\/h1>\n<h2>insert()\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u8ffd\u52a0\u3059\u308b<\/h2>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b\u306b\u306finsert()\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>\\DB::table('items')-&gt;<strong>insert([<\/strong>\r\n<strong>    'name' =&gt; '\u540d\u524d'<\/strong>\r\n<strong>]<\/strong>);<\/pre>\n<p>\u307e\u305a\u3001\\DB::table()\u3067\u3069\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u3046\u304b\u3092\u6307\u5b9a\u3057\u3001\u305d\u3057\u3066\u30a2\u30ed\u30fc\u6f14\u7b97\u5b50\u300c<strong>-&gt;<\/strong>\u300d\u3092\u4f7f\u3063\u3066insert()\u3092\u3064\u306a\u3052\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bEloquent\u30e2\u30c7\u30eb\u3067\u3082\u540c\u3058\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre>\\App\\Item::<strong>insert([<\/strong>\r\n<strong>    'name' =&gt; '\u540d\u524d'<\/strong>\r\n<strong>]);<\/strong><\/pre>\n<p>\u305d\u3057\u3066\u3001insert()\u306e\u5f15\u6570\u306f\u914d\u5217\u3067\u3001<\/p>\n<ul>\n<li><strong>key\u304c\u30ab\u30e9\u30e0\u540d<\/strong><\/li>\n<li><strong>value\u304c\u4fdd\u5b58\u3059\u308b\u5185\u5bb9<\/strong><\/li>\n<\/ul>\n<p>\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u30c7\u30fc\u30bf\u8ffd\u52a0\u5f8c\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-700\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/added_item.png\" alt=\"\" width=\"440\" height=\"150\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/added_item.png 440w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/added_item-300x102.png 300w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/p>\n<p>\u8d64\u67a0\u306e\u90e8\u5206\u304c\u65b0\u3057\u304f\u8ffd\u52a0\u3055\u308c\u305f\u5185\u5bb9\u3067\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u898b\u3066\u3082\u3089\u3048\u3070\u5206\u304b\u308b\u3068\u304a\u308a\u300c<strong>created_at<\/strong>\u300d\u3068\u300c<strong>updated_at<\/strong>\u300d\u304cNULL\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u3053\u304cinsert()\u3092\u4f7f\u3046\u6642\u306e\u6ce8\u610f\u70b9\u3067\u3059\u3002\u3064\u307e\u308a\u3001insert()\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u8ffd\u52a0\u3059\u308b\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u660e\u793a\u7684\u306b\u65e5\u4ed8\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3057\u3066\u3042\u3052\u306a\u3044\u3068NULL\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u3059\u3002<\/p>\n<pre>\\App\\Item::<strong>insert([<\/strong>\r\n<strong>    'name' =&gt; '\u540d\u524d',<\/strong>\r\n<strong>    'created_at' =&gt; now(),<\/strong>\r\n<strong>    'updated_at' =&gt; now()<\/strong>\r\n<strong>]<\/strong>);<\/pre>\n<p>\u305d\u306e\u305f\u3081\u3001\u79c1\u306e\u5834\u5408\u306finsert()\u3088\u308a\u3082\u6b21\u3067\u7d39\u4ecb\u3059\u308bsave()\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u8ffd\u52a0\u3059\u308b\u3053\u3068\u306e\u65b9\u304c\u591a\u3044\u3067\u3059\u3002<\/p>\n<p>\u3057\u304b\u3082\u3001\u3082\u3046\u3072\u3068\u3064\u91cd\u8981\u306a\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<strong>insert()\u306f\u3001\u30a4\u30d9\u30f3\u30c8\u306e\u81ea\u52d5\u5b9f\u884c\u304c\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/strong><\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30e2\u30c7\u30eb\u5185\u306b\u30c7\u30fc\u30bf\u8ffd\u52a0\u5f8c\u306b\u300cItemAdded\u300d\u3068\u3044\u3046\u30a4\u30d9\u30f3\u30c8\u3092\u5b9f\u884c\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u305f\u3068\u3057\u3066\u3082\u3001insert()\u306e\u5834\u5408\u306f\u7121\u8996\u3055\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App;\r\n\r\nuse App\\Events\\ItemAdded;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass Item extends Model\r\n{\r\n<strong>    protected $dispatchesEvents = [<\/strong>\r\n<strong>        'created' =&gt; ItemAdded::class<\/strong>\r\n<strong>    ];<\/strong><\/pre>\n<p>\u305f\u3060\u3057\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u624b\u52d5\u3067\u30a4\u30d9\u30f3\u30c8\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\uff08\u30cd\u30fc\u30e0\u30b9\u30da\u30fc\u30b9\u306b\u6c17\u3092\u3064\u3051\u3066\u304f\u3060\u3055\u3044\uff09<\/p>\n<pre><strong>event(new ItemAdded);<\/strong><\/pre>\n<p>\u306a\u304a\u3001insert()\u3068\u4f3c\u305f\u30e1\u30bd\u30c3\u30c9\u306binsertGetId()\u3082\u3042\u308a\u307e\u3059\u3002\u3053\u3061\u3089\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8ffd\u52a0\u3057\u305f\u30c7\u30fc\u30bf\u306eID\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>$new_id<\/strong> = \\App\\Item::<strong>insertGetId(<\/strong>[\r\n    'name' =&gt; '\u540d\u524d',\r\n    'created_at' =&gt; now(),\r\n    'updated_at' =&gt; now()\r\n]<strong>)<\/strong>;<\/pre>\n<h2>save()\u3067\u30c7\u30fc\u30bf\u8ffd\u52a0\u3059\u308b<\/h2>\n<p>\u524d\u306e\u9805\u76ee\u3067\u7d39\u4ecb\u3057\u305finsert()\u3067\u306f\u4ee5\u4e0b\u306e\uff12\u70b9\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3057\u305f\u3002<\/p>\n<ul>\n<li><strong>\u300ccreated_at\u300d\u3068\u300cupdated_at\u300d\u3092\u660e\u793a\u7684\u306b\u6307\u5b9a\u3057\u306a\u3044\u3068NULL\u306b\u306a\u308b\u3002<\/strong><\/li>\n<li><strong>\u30a4\u30d9\u30f3\u30c8\u304c\u5b9f\u884c\u3067\u304d\u306a\u3044\u3002<\/strong><\/li>\n<\/ul>\n<p>\u3053\u308c\u3092\u89e3\u6c7a\u3057\u3066\u304f\u308c\u308b\u306e\u304csave()\u3092\u4f7f\u3063\u305f\u30c7\u30fc\u30bf\u8ffd\u52a0\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$item = new \\App\\Item;\r\n$item-&gt;name = '\u540d\u524d';\r\n$item-&gt;<strong>save();<\/strong><\/pre>\n<p>\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u300cname\u300d\u306a\u3069\u30c7\u30fc\u30bf\u683c\u7d0d\u3002\u305d\u3057\u3066save()\u3092\u547c\u3076\u3002<\/p>\n<p>\u3053\u306e\u3084\u308a\u65b9\u3060\u3068\u3001\u81ea\u52d5\u3067\u300c<strong>created_at<\/strong>\u300d\u300c<strong>updated_at<\/strong>\u300d\u306b\u65e5\u4ed8\u304c\u5165\u529b\u3055\u308c\u307e\u3059\u3057\uff08\u3064\u307e\u308a\u5fd8\u308c\u308b\u3053\u3068\u304c\u306a\u304f\u306a\u308b\uff09\u3001\u30e2\u30c7\u30eb\u5185\u306e\u300c$dispatchesEvents\u300d\u3067\u8a2d\u5b9a\u3057\u305f\u30a4\u30d9\u30f3\u30c8\u3082\u81ea\u52d5\u7684\u306b\u5b9f\u884c\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u3069\u3046\u3057\u3066\u3082\u914d\u5217\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u8ffd\u52a0\u3057\u305f\u3044\u5834\u5408\u3084\u305d\u3082\u305d\u3082\u30a4\u30d9\u30f3\u30c8\u3092\u5b9f\u884c\u3057\u305f\u304f\u306a\u3044\u5834\u5408\u3067\u306a\u3044\u9650\u308a\u3001insert()\u3088\u308asave()\u3092\u4f7f\u3046\u306e\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002<\/p>\n<p>\u8ffd\u52a0\u5f8c\u306e\u30c6\u30fc\u30d6\u30eb\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-704\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/added_item_2.png\" alt=\"\" width=\"440\" height=\"150\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/added_item_2.png 440w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/added_item_2-300x102.png 300w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/p>\n<p>\u3061\u306a\u307f\u306b\u3001save()\u306b\u306f\u5f15\u6570\u3092\u5165\u308c\u308b\u3053\u3068\u304c\u3067\u304d\u3066\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u300ctouch\uff08\u30b3\u30fc\u30c9\u306e\u4e0b\u3067\u8aac\u660e\uff09\u300d\u3092\u7121\u52b9\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$item-&gt;save([\r\n    <strong>'touch' =&gt; false<\/strong>\r\n]);<\/pre>\n<p>\u203b\u3053\u306e\u5834\u5408\u306e\u300ctouch\u300d\u3068\u306f\u3001\u4f8b\u3048\u3070\u30d6\u30ed\u30b0\u8a18\u4e8b\u306e\u300cPost\u300d\u30c6\u30fc\u30d6\u30eb\u3068\u30b3\u30e1\u30f3\u30c8\u300cComment\u300d\u30c6\u30fc\u30d6\u30eb\u306e\u95a2\u4fc2\u3092\u8003\u3048\u308b\u3068\u5206\u304b\u308a\u3084\u3059\u3044\u3067\u3059\u3002\u3082\u3057\u3042\u308b\u8a18\u4e8b\u306b\u30b3\u30e1\u30f3\u30c8\u304c\u6295\u7a3f\u3055\u308c\u305f\u3089\u3001\u305d\u306e\u8a18\u4e8b\u30c7\u30fc\u30bf\uff08\u3064\u307e\u308a\u89aa\u30c6\u30fc\u30d6\u30eb\uff09\u306e\u300cupdated_at\u300d\u3082\u6700\u65b0\u306e\u65e5\u4ed8\u306b\u3057\u305f\u3044\u3067\u3059\u3088\u306d\u3002\u3053\u308c\u304ctouch\u3067\u3059\u3002\u305f\u3060\u3057\u3001\u300cComment\u300d\u5185\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306bbelongsTo\u3084belongsMany\u306a\u3069\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3001\u305d\u3057\u3066<strong>$touches<\/strong>\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App;\r\n\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass Comment extends Model\r\n{\r\n    <strong>protected $touches = ['post'];<\/strong>\r\n\r\n    \/\/ Relationship\r\n<strong>    public function post() {<\/strong>\r\n\r\n<strong>        return $this-&gt;belongsTo('App\\Post');<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<h2>\u30c7\u30fc\u30bf\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3092\u8003\u616e\u3057\u305f\u30c7\u30fc\u30bf\u8ffd\u52a0<\/h2>\n<h3>firstOrCreate()\u3067\u30c7\u30fc\u30bf\u8ffd\u52a0<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u3082\u3057\u30c7\u30fc\u30bf\u304c\u3042\u308c\u3070\u53d6\u5f97\u3059\u308b\u3051\u3069\u3001\u306a\u3044\u5834\u5408\u306f\u30c7\u30fc\u30bf\u8ffd\u52a0\u3057\u305f\u3044\u300d\u5834\u5408\u306b\u306f<strong>firstOrCreate()<\/strong>\u304c\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$item = \\App\\Item::<strong>firstOrCreate(['name' =&gt; '\u9234\u6728'])<\/strong>;<\/pre>\n<p>\u3053\u306e\u5834\u5408\u3001\u300cname\u300d\u306b\u300c\u9234\u6728\u300d\u304c\u3059\u3067\u306b\u5b58\u5728\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u4f55\u3082\u8ffd\u52a0\u305b\u305a\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u307e\u3059\u304c\u3001\u3082\u3057\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u65b0\u3057\u304f\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3057\u3066\u305d\u306e\u30c7\u30fc\u30bf\u3092<strong>$item<\/strong>\u306b\u683c\u7d0d\u3057\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u5f15\u6570\u3092\u5897\u3084\u3059\u3053\u3068\u3067\u6761\u4ef6\u3092\u8907\u6570\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$item = \\App\\Item::<strong>firstOrCreate(<\/strong>\r\n<strong>    ['name' =&gt; '\u65b0\u3057\u3044\u540d\u524d'],<\/strong>\r\n<strong>    ['deleted_at' =&gt; null]<\/strong>\r\n<strong>)<\/strong>;<\/pre>\n<h3>firstOrNew()\u3067\u30c7\u30fc\u30bf\u8ffd\u52a0<\/h3>\n<p>\u524d\u9805\u76ee\u306efirstOrCreate()\u3068\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u3061\u3089\u306fsave()\u3092\u5b9f\u884c\u3059\u308b\u307e\u3067\u30c7\u30fc\u30bf\u8ffd\u52a0\u3055\u308c\u306a\u3044\u306e\u304c\u7279\u5fb4\u3067\u3059\u3002<\/p>\n<pre>$item = \\App\\Item::<strong>firstOrNew(['name' =&gt; '\u65b0\u3057\u3044\u540d\u524d'])<\/strong>;\r\n<strong>$item-&gt;save(); \/\/ \u3053\u3053\u3067\u4fdd\u5b58\u3055\u308c\u308b<\/strong><\/pre>\n<p>\u3064\u307e\u308a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5f8c\u304b\u3089\u9055\u3046\u30ab\u30e9\u30e0\u306e\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3057\u305f\u3044\u5834\u5408\u306b\u4f7f\u3046\u3068\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre>$item = \\App\\Item::firstOrNew(['name' =&gt; '\u65b0\u3057\u3044\u540d\u524d']);\r\n<strong>$item-&gt;product_code = 'xxx';<\/strong>\r\n$item-&gt;save();<\/pre>\n<h1>DB\u306e\u30c7\u30fc\u30bf\u3092\u5909\u66f4\u3059\u308b<\/h1>\n<p>\u3059\u3067\u306b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u5b58\u5728\u3057\u3066\u3044\u308b\u30c7\u30fc\u30bf\u3092\u5909\u66f4\u3059\u308b\u5834\u5408\u306fupdate()\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001ID\u304c\u300c\uff11\u300d\u306e\u300cname\u300d\u30ab\u30e9\u30e0\u3092\u5909\u66f4\u3059\u308b\u5834\u5408\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>\\DB::table('items')\r\n    <strong>-&gt;where('id', 1)<\/strong>\r\n<strong>    -&gt;update([<\/strong>\r\n<strong>        'name' =&gt; '\u540d\u524dxxx2'<\/strong>\r\n<strong>    ]);<\/strong><\/pre>\n<p>\u3053\u3053\u3067\u91cd\u8981\u306a\u306e\u306f\u3001<strong>where()<\/strong>\u3067ID\u756a\u53f7\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>insert()\u306e\u5834\u5408\u306f\u3001\u5f15\u6570\u306b\u300cid\u300d\u304c\u5fc5\u8981\u3042\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u304cupdate()\u306e\u5834\u5408\u306f\u3069\u306e\u30c7\u30fc\u30bf\u3092\u5909\u66f4\u3059\u308b\u304b\u3092\u6307\u5b9a\u3057\u306a\u3044\u3068\u884c\u3051\u306a\u3044\u306e\u3067\uff08\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\uff09where\u306f\u5fd8\u308c\u305a\u306b\u8a18\u8ff0\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093\u3001where()\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8907\u6570\u30c7\u30fc\u30bf\u306b\u3082\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u3066\u3001\u305d\u306e\u5834\u5408\u306f<strong>\u8a72\u5f53\u3059\u308b\u5168\u30c7\u30fc\u30bf\u304c\u5909\u66f4\u3055\u308c\u307e\u3059\u3002<\/strong><\/p>\n<pre>\\DB::table('items')\r\n    <strong>-&gt;where('id', '&lt;', 5) \/\/ id\u304c\uff15\u3088\u308a\u5c11\u306a\u3044\u30c7\u30fc\u30bf\u5168\u3066\u3092\u5909\u66f4<\/strong>\r\n    -&gt;update([\r\n        'name' =&gt; '\u65b0\u3057\u3044\u540d\u524d'\r\n    ]);<\/pre>\n<p>\u3082\u3061\u308d\u3093\u3001Eloquent\u30e2\u30c7\u30eb\u3067\u3082\u540c\u3058\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>\\App\\Item::where('id', 1)-&gt;<strong>update(['name' =&gt; '\u65b0\u3057\u3044\u540d\u524d'])<\/strong>;<\/pre>\n<p>\u305f\u3060\u3057\u3001<strong>update()<\/strong>\u3082<strong>insert()<\/strong>\u3068\u540c\u3058\u304f\u3001<\/p>\n<ul>\n<li><strong>updated_at\u304c\u81ea\u52d5\u3067\u66f4\u65b0\u3055\u308c\u306a\u3044<\/strong><\/li>\n<li><strong>\u30a4\u30d9\u30f3\u30c8\u304c\u81ea\u52d5\u3067\u5b9f\u884c\u3055\u308c\u306a\u3044<\/strong><\/li>\n<\/ul>\n<p>\u3068\u3044\u3063\u305f\u30c7\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>save()\u3067\u30c7\u30fc\u30bf\u3092\u5909\u66f4\u3059\u308b<\/h2>\n<p>\u3082\u3057update()\u306e\u30c7\u30e1\u30ea\u30c3\u30c8\u304c\u304c\u6c17\u306b\u306a\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306bsave()\u3092\u4f7f\u3063\u305f\u30b3\u30fc\u30c9\u3067\u5bfe\u5fdc\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>save()\u3067\u8907\u6570\u30c7\u30fc\u30bf\u3092\u5909\u66f4\u3059\u308b<\/h3>\n<pre><strong>$items = \\App\\Item::where('id', '&lt;', 5)-&gt;get();<\/strong>\r\n\r\n<strong>foreach ($items as $item) {<\/strong>\r\n\r\n<strong>    $item-&gt;name = '\u65b0\u3057\u3044\u540d\u524d';<\/strong>\r\n<strong>    $item-&gt;save();<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<h3>save()\u3067\uff11\u3064\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u5909\u66f4\u3059\u308b<\/h3>\n<pre><strong>$item = \\App\\Item::where('id', 1)-&gt;first();<\/strong>\r\n<strong>$item-&gt;name = '\u65b0\u3057\u3044\u540d\u524d';<\/strong>\r\n<strong>$item-&gt;save();<\/strong><\/pre>\n<p>\u3053\u308c\u306a\u3089\u3001\u300c<strong>updated_at<\/strong>\u300d\u306f\u81ea\u52d5\u7684\u306b\u4fdd\u5b58\u65e5\u6642\u304c\u66f4\u65b0\u3055\u308c\u307e\u3059\u3057\u3001\u30a4\u30d9\u30f3\u30c8\u3082\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002<\/p>\n<h2>JSON\u30ab\u30e9\u30e0\u306e\u5909\u66f4\u3092\u3059\u308b<\/h2>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306aJSON\u30ab\u30e9\u30e0\uff08MySQL\u306f5.7.8\u304b\u3089\u6709\u52b9\uff09\u306e\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30a2\u30ed\u30fc\u6f14\u7b97\u5b50\u300c<strong>-&gt;<\/strong>\u300d\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u5909\u66f4\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>\\App\\User::where('id', 1)-&gt;update(['<strong>network-&gt;os<\/strong>' =&gt; 'Ubuntu']);<\/pre>\n<p>\u5b9f\u884c\u5f8c\u306e\u30c6\u30fc\u30d6\u30eb\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-708\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/db_json_2.png\" alt=\"\" width=\"421\" height=\"73\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/db_json_2.png 421w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/db_json_2-300x52.png 300w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/p>\n<h2>\u6570\u5b57\u30c7\u30fc\u30bf\u3092\u64cd\u4f5c\u3059\u308b<\/h2>\n<h3>increment()\u3067\u6570\u5b57\u30c7\u30fc\u30bf\u3092\u5897\u3084\u3059<\/h3>\n<p>increment()\u3092\u4f7f\u3048\u3070\u3001\u6570\u5b57\u30c7\u30fc\u30bf\u3092\u7c21\u5358\u306b\u5897\u3084\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u6ce8\u6587\u6570\u3092\u5897\u3084\u3059\u30b3\u30fc\u30c9\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>\\App\\Item::<strong>increment('ordered_count')<\/strong>;<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u300cordered_count\u300d\u304c+\uff11\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u5143\u306e\u6570\u5b57\u304c\u300c10\u300d\u3060\u3063\u305f\u3068\u3057\u305f\u3089\u3001\u300c11\u300d\u306b\u306a\u308b\u308f\u3051\u3067\u3059\u306d\u3002<\/p>\n<p>\u307e\u305f\u3001\u8db3\u3057\u305f\u3044\u6570\u5b57\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>\\App\\Item::<strong>increment('ordered_count', 5);<\/strong><\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001\u305d\u306e\u4ed6\u306e\u30ab\u30e9\u30e0\u5185\u5bb9\u3092\u540c\u6642\u306b\u5909\u66f4\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>\\App\\User::<strong>increment('ordered_count', 5, ['name' =&gt; '\u65b0\u3057\u3044\u540d\u524d']);<\/strong><\/pre>\n<h3>decrement()\u3067\u6570\u5b57\u30c7\u30fc\u30bf\u3092\u6e1b\u3089\u3059<\/h3>\n<p>increment()\u306e\u9006\u3067decrement()\u306f\u6570\u5b57\u30c7\u30fc\u30bf\u3092\u6e1b\u3089\u3059\u5f79\u5272\u3092\u3057\u307e\u3059\u3002<\/p>\n<pre>\\App\\Item::<strong>decrement('ordered_count')<\/strong>;<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067\u3001\u300cordered_count\u300d\u304c-\uff11\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u5143\u306e\u6570\u5b57\u304c\u300c10\u300d\u3060\u3063\u305f\u3068\u3057\u305f\u3089\u3001\u300c9\u300d\u306b\u306a\u308b\u308f\u3051\u3067\u3059\u306d\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093\u3001\u3053\u3061\u3089\u3082\u7279\u5b9a\u306e\u6570\u5b57\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>\\App\\Item::<strong>decrement('ordered_count', 5)<\/strong>;<\/pre>\n<p>\u305d\u3057\u3066\u3001\u3053\u3061\u3089\u3082\u4ed6\u306e\u30ab\u30e9\u30e0\u3092\u4e00\u7dd2\u306b\u5909\u66f4\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>\\App\\User::<strong>decrement('ordered_count', 5, ['name' =&gt; '\u65b0\u3057\u3044\u540d\u524d']);<\/strong><\/pre>\n<h2>\u30c7\u30fc\u30bf\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u3092\u8003\u616e\u3057\u305f\u5909\u66f4<\/h2>\n<p>\u3082\u3057\u3059\u3067\u306b\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u30c7\u30fc\u30bf\u3092\u5909\u66f4\u3057\u3001\u306a\u3044\u306a\u3089\u30c7\u30fc\u30bf\u8ffd\u52a0\u3057\u305f\u3044\u5834\u5408\u3001<strong>updateOrCreate()<\/strong>\u304c\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$item = \\App\\Item::updateOrCreate(\r\n    ['name' =&gt; '\u9234\u6728'],\r\n    ['name' =&gt; '\u4f50\u85e4']\r\n);<\/pre>\n<p>\u3053\u306e\u5834\u5408\u306e\u610f\u5473\u306f\u3001<\/p>\n<ol>\n<li><strong>\u300cname\u300d\u30ab\u30e9\u30e0\u304c\u300c\u9234\u6728\u300d\u306b\u306a\u3063\u3066\u3044\u308b\u30c7\u30fc\u30bf\u3092\u898b\u3064\u3051\u305f\u3089\u3001\u305d\u306e\u30c7\u30fc\u30bf\u306e\u300cname\u300d\u3092\u300c\u4f50\u85e4\u300d\u3078\u5909\u66f4\u3059\u308b\u3002<\/strong><\/li>\n<li><strong>\u3067\u3082\u3001\u3082\u3057\u30c7\u30fc\u30bf\u304c\u306a\u3044\u306a\u3089\u300c\u4f50\u85e4\u300d\u3068\u3044\u3046\u30c7\u30fc\u30bf\u3092\u65b0\u898f\u8ffd\u52a0\u3059\u308b<\/strong><\/li>\n<\/ol>\n<p>\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001\u691c\u7d22\u6761\u4ef6\u3092\u5897\u3084\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u7b2c\uff11\u5f15\u6570\u306e\u4e2d\u8eab\u3092\u5897\u3084\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>$item = \\App\\Item::updateOrCreate(\r\n    <strong>['name' =&gt; '\u5143\u306e\u540d\u524d', 'deleted_at' =&gt; null],<\/strong>\r\n    ['name' =&gt; '\u5909\u66f4\u3055\u308c\u305f\u540d\u524d']\r\n);<\/pre>\n<h1>DB\u306e\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b<\/h1>\n<h2>delete()\u3067\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b<\/h2>\n<h3>\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b<\/h3>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u3059\u3067\u306b\u5b58\u5728\u3057\u3066\u3044\u308b\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b\u306b\u306f<strong>delete()<\/strong>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>\\DB::table('items')-&gt;delete();<\/strong><\/pre>\n<p>\u3053\u306e\u4f8b\u306f\u300citems\u300d\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3057\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u7279\u5225\u306a\u7406\u7531\u304c\u306a\u3044\u5834\u5408\u306f\u30c6\u30fc\u30d6\u30eb\u3092\u521d\u671f\u5316\u3057\u305f\u3044\u5834\u5408\u3001<strong>truncate()<\/strong>\u3092\u4f7f\u3046\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>\\DB::table('items')-&gt;truncate();<\/strong><\/pre>\n<p>\u306a\u305c\u306a\u3089\u3001<strong>truncate()<\/strong>\u306fid\u306e\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\uff08\u81ea\u52d5\u63a1\u756a\uff09\u3092\uff11\u306b\u623b\u3057\u3066\u304f\u308c\u308b\u304b\u3089\u3067\u3059\u3002\u3064\u307e\u308a\u3001<strong>delete()<\/strong>\u3067\u5168\u30c7\u30fc\u30bf\u3092\u6d88\u3057\u305f\u5834\u5408\u3001\u305d\u306e\u5f8c\u30c7\u30fc\u30bf\u8ffd\u52a0\u3057\u305f\u3089id\u304c\u300c\uff11\u300d\u304b\u3089\u59cb\u307e\u3089\u306a\u3044\u3068\u3044\u3046\u53ef\u80fd\u6027\u304c\u3067\u3066\u304d\u307e\u3059\u3002<\/p>\n<h3>\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u3060\u3051\u524a\u9664\u3059\u308b<\/h3>\n<p>\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u524a\u9664\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bwhere()\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>\\App\\Item::<strong>where('id', 2)<\/strong>-&gt;delete();<\/pre>\n<h2>destroy()\u3067ID\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u524a\u9664\u3059\u308b<\/h2>\n<p>destroy()\u3092\u4f7f\u3048\u3070\u3001id\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u524a\u9664\u304c\u53ef\u80fd\u3067\u3059\u3002find()\u306e\u524a\u9664\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u3059\u306d\u3002<\/p>\n<p>\u3067\u306f\u3001\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$id = 1;\r\n\\App\\Item::<strong>destroy($id);<\/strong><\/pre>\n<p>ID\u756a\u53f7\u3092\u5f15\u6570\u3068\u3057\u3066\u5165\u308c\u308b\u3060\u3051\u3067\u524a\u9664\u304c\u3067\u304d\u308b\u306e\u3067\u3001\u30b3\u30fc\u30c9\u304c\u3068\u3066\u3082\u77ed\u304f\u306a\u308a\u307e\u3059\u306d\u3002\u307e\u305f\u3001ID\u756a\u53f7\u306e\u914d\u5217\u3092\u5f15\u6570\u3068\u3057\u3066\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u6570\u30c7\u30fc\u30bf\u306e\u524a\u9664\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre>\\App\\Item::<strong>destroy([2, 3])<\/strong>;<\/pre>\n<p>\u306a\u304a\u3001destroy()\u306e\u3044\u3044\u3068\u3053\u308d\u306fdelete()\u3068\u540c\u3058\u304f\u3001<strong>\u524a\u9664\u30a4\u30d9\u30f3\u30c8\u304c\u81ea\u52d5\u3067\u5b9f\u884c\u3055\u308c\u308b<\/strong>\u3068\u3053\u308d\u3067\u3059\u3002\u3057\u304b\u3082\u3001\u4e0a\u8a18\u306e\u3088\u3046\u306a\u8907\u6570\u524a\u9664\u306b\u3082\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u306e\u3067\u30a4\u30d9\u30f3\u30c8\u307e\u308f\u308a\u3092\u304d\u3061\u3093\u3068\u5b9f\u884c\u3057\u305f\u3044\u5834\u5408\u306fdestroy()\u3082\u9078\u629e\u80a2\u306e\u3072\u3068\u3064\u306b\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>\u524a\u9664\u30a4\u30d9\u30f3\u30c8\u3092\u5b9f\u884c\u3059\u308b\u306b\u306f<\/h2>\n<p>\u3082\u3057\u3001\u30e2\u30c7\u30eb\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u300c\u524a\u9664\u30a4\u30d9\u30f3\u30c8\u300d\u3092\u8ffd\u52a0\u3057\u3066\u3044\u305f\u3068\u3057\u3066\u3082\u3001get()\u3084first()\u306a\u3069\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u524d\u306bdelete()\u3092\u5b9f\u884c\u3057\u3066\u3057\u307e\u3063\u305f\u3089\u524a\u9664\u30a4\u30d9\u30f3\u30c8\u306f\u5b9f\u884c\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App;\r\n\r\nuse App\\Events\\ItemAdded;\r\nuse App\\Events\\ItemDeleted;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass Item extends Model\r\n{\r\n    protected $dispatchesEvents = [\r\n        <strong>'deleted' =&gt; ItemDeleted::class,<\/strong>\r\n    ];<\/pre>\n<p>\u524a\u9664\u30a4\u30d9\u30f3\u30c8\u3092\u5b9f\u884c\u3057\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b<strong>get()<\/strong>\u3084<strong>first()<\/strong>\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u3066\u304a\u304f\u304b\u3001<strong>destroy()<\/strong>\u3067\u30c7\u30fc\u30bf\u524a\u9664\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>\u8907\u6570\u306e\u524a\u9664\u30a4\u30d9\u30f3\u30c8\u3092\u5b9f\u884c\u3059\u308b\u5834\u5408<\/h3>\n<pre>$items = \\App\\Item::<strong>get()<\/strong>;\r\n\r\nforeach ($items as $item) {\r\n\r\n    <strong>$item-&gt;delete();<\/strong>\r\n\r\n}<\/pre>\n<h3>\u3072\u3068\u3064\u3060\u3051\u524a\u9664\u30a4\u30d9\u30f3\u30c8\u3092\u5b9f\u884c\u3059\u308b\u5834\u5408<\/h3>\n<pre>$item = \\App\\Item::<strong>first()<\/strong>;\r\n<strong>$item-&gt;delete();<\/strong><\/pre>\n<h2>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\uff08\u8ad6\u7406\u524a\u9664\uff09\u3092\u5b9f\u884c\u3059\u308b<\/h2>\n<p>Laravel\u306b\u306fEloquent\u30e2\u30c7\u30eb\u306b\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u6a5f\u80fd\u304c\u5099\u308f\u3063\u3066\u3044\u307e\u3059\u3002\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u306f\u4e07\u304c\u4e00\u306a\u306b\u304b\u306e\u624b\u9055\u3044\u3067\u30c7\u30fc\u30bf\u524a\u9664\u3057\u3066\u3057\u307e\u3063\u3066\u3082\u3001\u7269\u7406\u7684\u306b\u30c7\u30fc\u30bf\u304c\u306a\u304f\u306a\u308b\u308f\u3051\u3067\u306f\u306a\u3044\u306e\u3067\u3044\u3064\u3067\u3082\u5fa9\u6d3b\u304c\u53ef\u80fd\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u3082\u3057\u30c7\u30fc\u30bf\u304c\u7279\u306b\u5927\u4e8b\u306a\u5834\u5408\u306f\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u306e\u5229\u7528\u3092\u691c\u8a0e\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u306e\u4f7f\u3044\u65b9<\/h3>\n<p>\u307e\u305a\u30c6\u30fc\u30d6\u30eb\u306b\u524a\u9664\u3055\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u65e5\u4ed8\u30ab\u30e9\u30e0\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\u3001\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u5185\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306bsoftDeletes()\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u5c02\u7528\u306e\u300c<strong>deleted_at<\/strong>\u300d\u3068\u3044\u3046\u30ab\u30e9\u30e0\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>Schema::create('items', function (Blueprint $table) {\r\n    $table-&gt;increments('id');\r\n    $table-&gt;string('name');\r\n    <strong>$table-&gt;softDeletes();<\/strong>\r\n    $table-&gt;timestamps();\r\n});<\/pre>\n<p>\u3053\u308c\u3067\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001DB\u306e\u30c6\u30fc\u30d6\u30eb\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-713\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/soft_delete.png\" alt=\"\" width=\"485\" height=\"97\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/soft_delete.png 485w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/soft_delete-300x60.png 300w\" sizes=\"auto, (max-width: 485px) 100vw, 485px\" \/><\/p>\n<p>\u306a\u304a\u3001\u30ab\u30e9\u30e0\u306e\u4e2d\u8eab\u306f\u3001\u524a\u9664\u3055\u308c\u305f\u3089\u305d\u306e\u65e5\u6642\u304c\u4fdd\u5b58\u3055\u308c\u3001\u524a\u9664\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u306fNULL\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306b\u3001Eloquent\u30e2\u30c7\u30eb\u5185\u306b\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3092\u4f7f\u3046\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002\u5fc5\u8981\u3068\u306a\u308b\u306e\u306f\u300cSoftDeletes\u300d\u30c8\u30ec\u30a4\u30c8\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u3060\u3051\u3067\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App;\r\n\r\nuse App\\Events\\ItemAdded;\r\nuse App\\Events\\ItemDeleted;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n<strong>use Illuminate\\Database\\Eloquent\\SoftDeletes;<\/strong>\r\n\r\nclass Item extends Model\r\n{\r\n    <strong>use SoftDeletes;<\/strong><\/pre>\n<p>\u3053\u308c\u3067\u6e96\u5099\u306f\u5b8c\u4e86\u3067\u3059\u3002<br \/>\n\u3067\u306f\u3001\u5b9f\u969b\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3066\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3057\u3001\u30c6\u30fc\u30d6\u30eb\u5185\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>\\App\\Item::destroy(1);<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-714\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/soft_delete_2.png\" alt=\"\" width=\"536\" height=\"97\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/soft_delete_2.png 536w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/soft_delete_2-300x54.png 300w\" sizes=\"auto, (max-width: 536px) 100vw, 536px\" \/><\/p>\n<p>\u3046\u307e\u304f\u524a\u9664\u3055\u308c\u305f\u65e5\u6642\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3057\u305f\u3002\u3053\u308c\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u901a\u5e38\u901a\u308aget()\u306a\u3069\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u3066\u3082ID\u756a\u53f7\u300c\uff11\u300d\u306f\u542b\u307e\u308c\u306a\u3044\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>$items = \\App\\Item::get();<\/strong><\/pre>\n<h3>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3057\u305f\u30c7\u30fc\u30bf\u306e\u53d6\u5f97<\/h3>\n<p>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u300c\u542b\u3081\u3066\u300d\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f\u3001withTrashed()\u3001\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30c7\u30fc\u30bf\u300c\u3060\u3051\u300d\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306fonlyTrashed()\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$items = \\App\\Item::<strong>withTrashed()<\/strong>-&gt;get();<\/pre>\n<pre>$items = \\App\\Item::<strong>onlyTrashed()<\/strong>-&gt;get();<\/pre>\n<h3>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3092\u89e3\u9664\u3059\u308b<\/h3>\n<p>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3057\u305f\u30c7\u30fc\u30bf\u3092\u5143\u306b\u623b\u3059\u306b\u306f\u3001<strong>restore()<\/strong>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>$items = \\App\\Item::withTrashed()\r\n    -&gt;where('id', 1)\r\n    <strong>-&gt;restore()<\/strong>;<\/pre>\n<p>\u306a\u304a\u3001where()\u3092\u5fd8\u308c\u3066\u3057\u307e\u3046\u3068\u5168\u30c7\u30fc\u30bf\u304c\u5fa9\u6d3b\u3057\u3066\u3057\u307e\u3046\u306e\u3067\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>\\App\\Item::withTrashed()-&gt;restore();<\/strong><\/pre>\n<h3>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3092\u7121\u8996\u3057\u3066\u7269\u7406\u524a\u9664\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u304c\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3051\u308c\u3069\u3082\u7269\u7406\u7684\u306b\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3057\u305f\u3044\uff08\u3064\u307e\u308aDB\u304b\u3089\u6d88\u3057\u305f\u3044\uff09\u5834\u5408\u306fforceDelete()\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>\\App\\Item::where('id', 1)-&gt;<strong>forceDelete()<\/strong>;<\/pre>\n<p>\u203b\u3053\u306e\u5834\u5408\u3001\u3082\u3061\u308d\u3093\u30c7\u30fc\u30bf\u306e\u5fa9\u6d3b\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306fLaravel\u3067DB\u64cd\u4f5c\u3092\u3059\u308b\u65b9\u6cd5\u3092\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u6539\u3081\u3066\u3053\u3046\u3044\u3046\u57fa\u672c\u7684\u306a\u5185\u5bb9\u3092\u78ba\u8a8d\u3059\u308b\u3068\u3001\u3044\u304b\u306bLaravel\u306e\u5b8c\u6210\u5ea6\u304c\u9ad8\u3044\u304b\u3092\u5b9f\u611f\u3057\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u307f\u306a\u3055\u3093\u3082\u3054\u6d3b\u7528\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-444 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/16.png\" alt=\"\" width=\"370\" height=\"310\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/16.png 370w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/16-300x251.png 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3001\u524d\u56de\u306f\u8a18\u4e8b\u5185\u5bb9\u304c20,000\u6587\u5b57\u3092\u8d8a\u3048\u308b\u9577\u7de8\u8a18\u4e8b\u300cLaravel\u30fb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u5168\u5b9f\u4f8b\u300d\u3092\u304a\u5c4a\u3051\u3057\u307e\u3057\u305f\u304c\u3001\u4eca\u56de\u306f\u305d\u306e\u7d9a\u7de8\u3068\u3057\u3066\u300cDB\u306e\u30c7 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=699\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Laravel\u30fb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c7\u30fc\u30bf\u64cd\u4f5c\uff08\u8ffd\u52a0\uff0f\u5909\u66f4\uff0f\u524a\u9664\uff09\u3059\u308b\u5168\u5b9f\u4f8b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":706,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[60],"class_list":["post-699","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-60"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=699"}],"version-history":[{"count":17,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/699\/revisions"}],"predecessor-version":[{"id":724,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/699\/revisions\/724"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/706"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}