{"id":8128,"date":"2020-10-19T07:00:52","date_gmt":"2020-10-18T22:00:52","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=8128"},"modified":"2020-10-17T04:40:00","modified_gmt":"2020-10-16T19:40:00","slug":"%e3%80%90laravel%e3%80%91%e5%86%99%e7%9c%9f%e3%81%8b%e3%82%89%e8%89%b2%e6%83%85%e5%a0%b1%e3%82%92%e5%8f%96%e5%be%97%e3%81%97%e3%80%81%e6%a4%9c%e7%b4%a2%e3%81%a7%e3%81%8d%e3%82%8b%e3%82%88%e3%81%86","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=8128","title":{"rendered":"\u3010Laravel\u3011\u5199\u771f\u304b\u3089\u8272\u60c5\u5831\u3092\u53d6\u5f97\u3057\u3001\u691c\u7d22\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u3053\u306e\u3068\u3053\u308d\u3042\u307e\u308a\u5916\u51fa\u3092\u3057\u306a\u304f\u306a\u3063\u305f\u3053\u3068\u3082\u3042\u308a\u3001\u30aa\u30f3\u30e9\u30a4\u30f3\u30b7\u30e7\u30c3\u30d7\u3067\u30e2\u30ce\u3092\u8cb7\u3046\u3053\u3068\u304c\u5897\u3048\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u6700\u8fd1\u3067\u306f\u670d\u3084\u9774\u306a\u3093\u304b\u3082\u305f\u307e\u306b\u8cb7\u3046\u3093\u3067\u3059\u304c\u3001\u3053\u308c\u306b\u95a2\u9023\u3057\u3066\u30aa\u30f3\u30e9\u30a4\u30f3\u30b7\u30e7\u30c3\u30d7\u306e\u300c\u3042\u308b\u6a5f\u80fd\u300d\u304c\u4fbf\u5229\u3060\u306a\u3068\u611f\u3058\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u308c\u304c\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb<\/p>\n<p><strong style=\"font-size: 35px;\">\u8272\u3092\u4f7f\u3063\u305f\u691c\u7d22<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u8d64\u3084\u9752\u306a\u3069<strong>\u597d\u304d\u306a\u8272\u3092\u6307\u5b9a\u3057\u3001\u305d\u306e\u8272\u306b\u95a2\u9023\u3059\u308b\u5546\u54c1\u304c\u305a\u3089\u3063\u3068\u8868\u793a\u3055\u308c\u308b\u6a5f\u80fd<\/strong>\u3067\u3059\u306d\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093\u3001\u7cbe\u5ea6\u304c\u91cd\u8981\u306a\u30aa\u30f3\u30e9\u30a4\u30f3\u30b7\u30e7\u30c3\u30d7\u3067\u306f100\uff05\u81ea\u52d5\u3067\u8272\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u3044\u308b\u308f\u3051\u3067\u306a\u306a\u3044\u306e\u3067\u3057\u3087\u3046\u304c\u3001\u540c\u6642\u306b\u300c\u81ea\u52d5\u3067\u3084\u3063\u305f\u3089\u3069\u3053\u307e\u3067\u306e\u7cbe\u5ea6\u3067\u3067\u304d\u308b\u3060\u308d\u3046\uff1f\uff1f\u300d\u3068\u3044\u3046\u7591\u554f\u3082\u6d6e\u304b\u3073\u4e0a\u304c\u3063\u3066\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u753b\u50cf\u305d\u3053\u3067\u2757<\/p>\n<p>\u4eca\u56de\u306f<code>Laravel<\/code>\u3092\u4f7f\u3063\u3066\u753b\u50cf\u304b\u3089\u8272\u3092\u62bd\u51fa\u3057\u3001\u305d\u306e\u8272\u30c7\u30fc\u30bf\u3067\u691c\u7d22\u3059\u308b\u3068\u3044\u3046\u6a5f\u80fd\u3092\u3064\u304f\u3063\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u306e\u5b66\u7fd2\u306b\u5f79\u7acb\u3066\u3070\u5b09\u3057\u3044\u3067\u3059\ud83d\ude0a\u2728<br \/>\n\uff08\u6700\u5f8c\u306b\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u4e00\u5f0f\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3067\u304d\u307e\u3059\u3088\ud83d\udc4d\uff09<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3405 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_18.png\" alt=\"\" width=\"310\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_18.png 310w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_18-291x300.png 291w\" sizes=\"auto, (max-width: 310px) 100vw, 310px\" \/>\u300c\u30dc\u30c8\u30e0\u306f\u57fa\u672c\u3001\u8d64\u3067\u3059\ud83d\udc4d\u300d<\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 8.x<\/p>\n<h1>\u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u3042\u308b\u753b\u50cf\u306e\u4e2d\u306b\u306f\u3069\u3093\u306a\u8272\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u3092\u62bd\u51fa\u3057\u3066\u4fdd\u5b58\u3057\u307e\u3059\u3002<br \/>\n\u3055\u3089\u306b\u3001\u305d\u306e\u4fdd\u5b58\u3057\u305f\u8272\u60c5\u5831\u3092\u4f7f\u3063\u3066\u5546\u54c1\u691c\u7d22\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u5bfe\u8c61\u306e\u8272\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u8d64<\/li>\n<li>\u9752<\/li>\n<li>\u9ec4<\/li>\n<li>\u7dd1<\/li>\n<\/ul>\n<p>\u3067\u306f\u697d\u3057\u304f\u3084\u3063\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u2757<\/p>\n<h1>\u30e2\u30c7\u30eb\uff06DB\u30c6\u30fc\u30d6\u30eb\u3092\u3064\u304f\u308b<\/h1>\n<h2>\u30c6\u30fc\u30d6\u30eb\u69cb\u6210<\/h2>\n<p>\u4eca\u56de\u5fc5\u8981\u306b\u306a\u308b\u30c6\u30fc\u30d6\u30eb\u306f\u4ee5\u4e0b\uff13\u3064\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u5546\u54c1<\/li>\n<li>\u5546\u54c1\u306e\u753b\u50cf<\/li>\n<li>\u5546\u54c1\u753b\u50cf\u306e\u8272<\/li>\n<\/ul>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e\uff13\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u95a2\u4fc2\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8133\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_relationship.png\" alt=\"\" width=\"640\" height=\"430\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_relationship.png 640w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_relationship-300x202.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>\u3064\u307e\u308a\u3001\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li><span style=\"text-decoration: underline;\">\u5546\u54c1<\/span> \u5bfe <span style=\"text-decoration: underline;\">\u5546\u54c1\u753b\u50cf<\/span>\u3000=\u3000<span style=\"text-decoration: underline;\">\uff11<\/span> \u5bfe <span style=\"text-decoration: underline;\">\u591a<\/span>\u3000\u30fb\u30fb\u30fb\u3000\u5546\u54c1\u306e\u753b\u50cf\u306f\u4f55\u500b\u3067\u3082\u4fdd\u5b58\u3067\u304d\u308b<\/li>\n<li><span style=\"text-decoration: underline;\">\u5546\u54c1\u753b\u50cf<\/span> \u5bfe <span style=\"text-decoration: underline;\">\u305d\u306e\u8272<\/span>\u3000=\u3000<span style=\"text-decoration: underline;\">\uff11<\/span> \u5bfe <span style=\"text-decoration: underline;\">\u591a<\/span>\u3000\u30fb\u30fb\u30fb\u3000\u753b\u50cf\u306e\u8272\u306f\u4f55\u500b\u3067\u3082\u4fdd\u5b58\u3067\u304d\u308b<\/li>\n<\/ul>\n<p>\u306a\u304a\u3001\u753b\u50cf\u304c\u8907\u96d1\u306b\u306a\u308b\u306e\u3067\u63cf\u3044\u3066\u3044\u307e\u305b\u3093\u304c\u3001\u8272\u30c6\u30fc\u30d6\u30eb\u306b\u306f\u691c\u7d22\u305f\u3081\u306b\uff08\u89aa\u306e\u89aa\u306b\u306a\u308b\uff09<strong>\u5546\u54c1ID<\/strong>\u3082\u4fdd\u5b58\u3057\u307e\u3059\u306e\u3067\u3001<strong>\u5546\u54c1<\/strong> \u5bfe<strong> \u8272<\/strong>\u3082\u300c<strong>\uff11<\/strong> \u5bfe <strong>\u591a<\/strong>\u300d\u3068\u3057\u3066\u6a5f\u80fd\u3057\u307e\u3059\u3002<\/p>\n<p>\u203b\u3061\u306a\u307f\u306b\u3001\u672c\u6765\u306f\u300c\u753b\u50cf\u300d\u3068\u300c\u8272\u300d\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u3064\u304f\u308a\u3001\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u3067\u3064\u306a\u3050\u65b9\u304c\u30b9\u30de\u30fc\u30c8\u3060\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u4eca\u56de\u306f\u30c6\u30b9\u30c8\u3067\u3059\u306e\u3067\u3001\u30b7\u30f3\u30d7\u30eb\u306a\u69cb\u6210\u3067\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<h2>\u5546\u54c1\u30e2\u30c7\u30eb\uff06\u30c6\u30fc\u30d6\u30eb\u3092\u3064\u304f\u308b<\/h2>\n<p>\u307e\u305a\u306f\u57fa\u672c\u306b\u306a\u308b\u5546\u54c1\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u3067\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:model Item -m<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>database\/migrations\/****_**_**_******_create_items_table.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass CreateItemsTable extends Migration\r\n{\r\n    public function up()\r\n    {\r\n        <strong>Schema::create('items', function (Blueprint $table) {<\/strong>\r\n<strong>            $table-&gt;id();<\/strong>\r\n<strong>            $table-&gt;string('name')-&gt;comment('\u5546\u54c1\u540d');<\/strong>\r\n<strong>            $table-&gt;text('description')-&gt;comment('\u8aac\u660e\u6587');<\/strong>\r\n<strong>            $table-&gt;integer('price')-&gt;comment('\u91d1\u984d');<\/strong>\r\n<strong>            $table-&gt;timestamps();<\/strong>\r\n<strong>        });<\/strong>\r\n    }<\/pre>\n<p>\u307e\u305f\u3001\u4e00\u7dd2\u306b\u4f5c\u6210\u3055\u308c\u305f\u30e2\u30c7\u30eb\u306e\u4e2d\u306b\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u8ffd\u52a0\u3057\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>app\/Models\/Item.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass Item extends Model\r\n{\r\n    use HasFactory;\r\n\r\n    \/\/ Relationship\r\n    <strong>public function item_images() {<\/strong>\r\n\r\n<strong>        return $this-&gt;hasMany('App\\Models\\ItemImage', 'item_id', 'id');<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n<strong>    public function item_image_colors() {<\/strong>\r\n\r\n<strong>        return $this-&gt;hasMany('App\\Models\\ItemImageColor', 'item_id', 'id');<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<h2>\u5546\u54c1\u753b\u50cf\u30e2\u30c7\u30eb\uff06\u30c6\u30fc\u30d6\u30eb\u3092\u3064\u304f\u308b<\/h2>\n<p>\u6b21\u306b\u5546\u54c1\u753b\u50cf\u30e2\u30c7\u30eb\u3067\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:model ItemImage -m\r\n<\/strong><\/pre>\n<p>\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<p><strong>database\/migrations\/****_**_**_******_create_item_images_table.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass CreateItemImagesTable extends Migration\r\n{\r\n    public function up()\r\n    {\r\n        <strong>Schema::create('item_images', function (Blueprint $table) {<\/strong>\r\n<strong>            $table-&gt;id();<\/strong>\r\n<strong>            $table-&gt;unsignedBigInteger('item_id')-&gt;comment('\u5546\u54c1ID');<\/strong>\r\n<strong>            $table-&gt;string('path')-&gt;comment('\u30d5\u30a1\u30a4\u30eb\u30d1\u30b9');<\/strong>\r\n<strong>            $table-&gt;timestamps();<\/strong>\r\n\r\n<strong>            $table-&gt;foreign('item_id')-&gt;references('id')-&gt;on('items');<\/strong>\r\n<strong>        });<\/strong>\r\n    }<\/pre>\n<h2>\u753b\u50cf\u304b\u3089\u62bd\u51fa\u3057\u305f\u8272\u306e\u30e2\u30c7\u30eb\uff06\u30c6\u30fc\u30d6\u30eb\u3092\u3064\u304f\u308b<\/h2>\n<p>\u6700\u5f8c\u306b\u8272\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u30c6\u30fc\u30d6\u30eb\u3067\u3059\u3002<br \/>\n\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:model ItemImageColor -m<\/strong><\/pre>\n<p>\u4e2d\u8eab\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><strong>database\/migrations\/****_**_**_******_create_item_image_colors_table.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass CreateItemImageColorsTable extends Migration\r\n{\r\n    public function up()\r\n    {\r\n        <strong>Schema::create('item_image_colors', function (Blueprint $table) {<\/strong>\r\n<strong>            $table-&gt;id();<\/strong>\r\n<strong>            $table-&gt;unsignedBigInteger('item_id')-&gt;comment('\u5546\u54c1ID');<\/strong>\r\n<strong>            $table-&gt;unsignedBigInteger('item_image_id')-&gt;comment('\u5546\u54c1\u753b\u50cfID');<\/strong>\r\n<strong>            $table-&gt;string('color')-&gt;comment('\u8272'); \/\/ `red`, `blue`, `green` ...<\/strong>\r\n<strong>            $table-&gt;timestamps();<\/strong>\r\n\r\n<strong>            $table-&gt;foreign('item_id')-&gt;references('id')-&gt;on('items');<\/strong>\r\n<strong>            $table-&gt;foreign('item_image_id')-&gt;references('id')-&gt;on('item_images');<\/strong>\r\n<strong>        });<\/strong>\r\n    }<\/pre>\n<h1>\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u3064\u304f\u308b<\/h1>\n<p>\u7d9a\u3044\u3066\u3001\u958b\u767a\u3092\u3057\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u306b\u30c0\u30df\u30fc\u306e\u5546\u54c1\u30c7\u30fc\u30bf\u3092\u3064\u304f\u308b\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f<code>Laravel 8.x<\/code>\u304b\u3089\u65b0\u3057\u304f\u306a\u3063\u305f<code>Factory<\/code>\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:factory ItemFactory --model=Item<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001<code>Factory<\/code>\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<p><strong>database\/factories\/ItemFactory.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass ItemFactory extends Factory\r\n{\r\n    protected $model = Item::class;\r\n    <strong>private $number = 0; \/\/ \ud83d\udc48 \u9023\u756a\u7528\u306e\u5909\u6570<\/strong>\r\n\r\n    public function definition()\r\n    {\r\n        <strong>$this-&gt;number++;<\/strong>\r\n\r\n<strong>        return [<\/strong>\r\n<strong>            'name' =&gt; '\u30c6\u30b9\u30c8\u5546\u54c1\u540d - '. $this-&gt;number,<\/strong>\r\n<strong>            'description' =&gt; \"\u30c6\u30b9\u30c8\u8aac\u660e\u6587\\n\u30c6\u30b9\u30c8\u8aac\u660e\u6587\\n\u30c6\u30b9\u30c8\u8aac\u660e\u6587 - \". $this-&gt;number,<\/strong>\r\n<strong>            'price' =&gt; $this-&gt;faker-&gt;randomDigitNotNull() * 1000<\/strong>\r\n<strong>        ];<\/strong>\r\n    }\r\n}<\/pre>\n<p>\u5909\u66f4\u304c\u5b8c\u4e86\u3057\u305f\u3089\u3001<code>ItemFactory<\/code>\u304c\u6709\u52b9\u306b\u306a\u308b\u3088\u3046\u306b<code>Seeder<\/code>\u30d5\u30a1\u30a4\u30eb\u3078\u767b\u9332\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p><strong>database\/seeders\/DatabaseSeeder.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass DatabaseSeeder extends Seeder\r\n{\r\n    public function run()\r\n    {\r\n        <strong>Item::factory(25)-&gt;create(); \/\/ \ud83d\udc48 \u3053\u3053\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f<\/strong>\r\n    }\r\n}<\/pre>\n<p>\u3067\u306f\u3001\u3053\u306e\u72b6\u614b\u3067\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3057\u307e\u3057\u3087\u3046\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan migrate:fresh --seed<\/strong><\/pre>\n<p>\u5b9f\u884c\u304c\u5b8c\u4e86\u3059\u308b\u3068\u3001\u5b9f\u969b\u306e\u30c6\u30fc\u30d6\u30eb\u306f\u3053\u3046\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8130\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_1.png\" alt=\"\" width=\"648\" height=\"475\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_1.png 648w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_1-300x220.png 300w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/p>\n<h1>\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b<\/h1>\n<p>\u6b21\u306b\u3001\u8272\u62bd\u51fa\u3059\u308b\u305f\u3081\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/thephpleague\/color-extractor\" target=\"_blank\" rel=\"noopener noreferrer\">thephpleague \/ color-extractor<\/a><\/p>\n<p>\u3053\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3046\u3068\u3001\u753b\u50cf\u306e\u4e2d\u3067\u4f7f\u308f\u308c\u3066\u3044\u308b\u8272\u3092\u7c21\u5358\u306b\u62bd\u51fa\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>composer require league\/color-extractor:0.3.*<\/strong><\/pre>\n<h1>\u753b\u50cf\u304c\u767b\u9332\u3055\u308c\u305f\u3089\u8272\u3092\u53d6\u5f97\u3059\u308b<\/h1>\n<p>\u3067\u306f\u3001\u3053\u3053\u304b\u3089\u304c\u672c\u984c\u306e\u753b\u50cf\u306e\u8272\u3092\u53d6\u308a\u51fa\u3059\u90e8\u5206\u306b\u306a\u308a\u307e\u3059\u2757<br \/>\n\u4eca\u56de\u30c6\u30b9\u30c8\u3067\u4f7f\u3046\u753b\u50cf\u306f\u3053\u3061\u3089\u306e\uff14\u679a\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8137\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_1.jpg\" alt=\"\" width=\"320\" height=\"427\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_1.jpg 320w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_1-225x300.jpg 225w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8136\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_2.jpg\" alt=\"\" width=\"320\" height=\"427\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_2.jpg 320w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_2-225x300.jpg 225w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8135\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_3.jpg\" alt=\"\" width=\"320\" height=\"240\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_3.jpg 320w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_3-300x225.jpg 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8134\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_4.jpg\" alt=\"\" width=\"320\" height=\"240\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_4.jpg 320w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/color_extract_4-300x225.jpg 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/p>\n<p>\u203b\u3061\u306a\u307f\u306b\u30d2\u30de\u30ef\u30ea\u306e\u7a2e\u306f\u30ab\u30ca\u30c0\u3067\u304a\u3084\u3064\u3068\u3057\u3066\u58f2\u3089\u308c\u3066\u3044\u308b<code>spitz<\/code>\u3067\u3059\u3002\uff08\u304a\u3044\u3057\u3044\u306e\u306b\u3001\u65e5\u672c\u3067\u58f2\u3063\u3066\u306a\u3044\u30fb\u30fb\u30fb\ud83d\ude2d\uff09<\/p>\n<h2>\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u304f\u308b<\/h2>\n<p>\u4eca\u56de\u306f\u3001DB\u30c6\u30fc\u30d6\u30eb<code>item_images<\/code>\u306b\u753b\u50cf\u60c5\u5831\u304c\u4fdd\u5b58\u3055\u308c\u305f\u3089<strong>\u81ea\u52d5\u7684\u306b\u8272\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/strong><\/p>\n<p>\u3067\u306f\u3001\u305d\u306e\u305f\u3081\u306b<code>ItemImageSaved<\/code>\u3068\u3044\u3046\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u304f\u308a\u307e\u3057\u3087\u3046\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:event ItemImageSaved<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/Events\/ItemImageSaved.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Events;\r\n\r\nuse App\\Models\\ItemImage;\r\nuse App\\Models\\ItemImageColor;\r\nuse Illuminate\\Broadcasting\\InteractsWithSockets;\r\nuse Illuminate\\Foundation\\Events\\Dispatchable;\r\nuse Illuminate\\Queue\\SerializesModels;\r\nuse League\\ColorExtractor\\Color;\r\nuse League\\ColorExtractor\\Palette;\r\n\r\nclass ItemImageSaved\r\n{\r\n    use Dispatchable, InteractsWithSockets, SerializesModels;\r\n\r\n    <strong>const THRESHOLD = 20; \/\/ \u4f55\uff05\u4ee5\u4e0a\u8272\u304c\u3042\u308c\u3070\u4fdd\u5b58\u3059\u308b\u304b\r\n    const BASE_COLORS = [  \/\/ \u57fa\u6e96\u3068\u306a\u308b\u8272\r\n        'red' =&gt; [128, 0, 0],\r\n        'blue' =&gt; [0, 0, 128],\r\n        'yellow' =&gt; [128, 128, 0],\r\n        'green' =&gt; [0, 128, 0],\r\n        'black' =&gt; [0, 0, 0],\r\n        'gray' =&gt; [128, 128, 128],\r\n        'white' =&gt; [255, 255, 255]\r\n    ];\r\n\r\n    public function __construct(ItemImage $item_image)\r\n    {\r\n        $path = $item_image-&gt;path;\r\n        $palette = Palette::fromFilename($path);\r\n        $all_color_count = 0;\r\n        $extracted_color_counts = [\r\n            'red' =&gt; 0,\r\n            'blue' =&gt; 0,\r\n            'yellow' =&gt; 0,\r\n            'green' =&gt; 0,\r\n        ];\r\n\r\n        foreach($palette as $color =&gt; $count) {\r\n\r\n            $extracted_rgb = array_values(\r\n                Color::fromIntToRgb($color)\r\n            );\r\n            $min_distance = 765; \/\/ \u6700\u5927\u8ddd\u96e2\u304b\u3089\u30b9\u30bf\u30fc\u30c8\r\n            $color_key = '';\r\n\r\n            foreach(self::BASE_COLORS as $key =&gt; $rgb) {\r\n\r\n                $color_distance = $this-&gt;getColorDistance($extracted_rgb, $rgb);\r\n\r\n                if($color_distance &lt; $min_distance) {\r\n\r\n                    $min_distance = $color_distance;\r\n                    $color_key = $key;\r\n\r\n                }\r\n\r\n            }\r\n\r\n            if(in_array($color_key, ['red', 'blue', 'yellow', 'green'])) {\r\n\r\n                $extracted_color_counts[$color_key] += $count;\r\n                $all_color_count += $count;\r\n\r\n            }\r\n\r\n        }\r\n\r\n        foreach($extracted_color_counts as $color_key =&gt; $count) {\r\n\r\n            if($count &gt; 0 &amp;&amp; in_array($color_key, ['red', 'blue', 'yellow', 'green'])) {\r\n\r\n                $percentage = $count \/ $all_color_count * 100;\r\n\r\n                if($percentage &gt; self::THRESHOLD) { \/\/ \u3057\u304d\u3044\u5024\u3088\u308a\u5927\u304d\u3051\u308c\u3070\u4fdd\u5b58\u3059\u308b\r\n\r\n                    $item_image_color = new ItemImageColor();\r\n                    $item_image_color-&gt;item_id = $item_image-&gt;item_id;\r\n                    $item_image_color-&gt;item_image_id = $item_image-&gt;id;\r\n                    $item_image_color-&gt;color = $color_key;\r\n                    $item_image_color-&gt;save();\r\n\r\n                }\r\n\r\n            }\r\n\r\n        }\r\n\r\n    }\r\n\r\n    function getColorDistance($color_1, $color_2) { \/\/ \uff12\u3064\u306e\u8272\u304c\u3069\u308c\u3060\u3051\u96e2\u308c\u3066\u3044\u308b\u304b\u3092\u53d6\u5f97\r\n\r\n        return abs($color_1[0] - $color_2[0]) +\r\n            abs($color_1[1] - $color_2[1]) +\r\n            abs($color_1[2] - $color_2[2]);\r\n\r\n    }<\/strong>\r\n\r\n}<\/pre>\n<p>\u3053\u306e\u4e2d\u3067\u3084\u3063\u3066\u3044\u308b\u624b\u9806\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ol>\n<li>\u753b\u50cf\u306e\u4e2d\u304b\u3089\u8272\u30c7\u30fc\u30bf\u3092\u62bd\u51fa<\/li>\n<li>\u62bd\u51fa\u3057\u305f\u8272\u304c\u300c\u3069\u306e\u8272\u306b\uff08\u8ddd\u96e2\u7684\u306b\uff09\u8fd1\u3044\u304b\uff1f\u300d\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3001\u8272\u3092\u5224\u5225<\/li>\n<li>\u5224\u5225\u3057\u305f\u8272\u30c7\u30fc\u30bf\u3092\u96c6\u8a08\u3057\u3066\u3001\u3082\u3057\u8a2d\u5b9a\u3057\u305f\u5024\uff08\u3057\u304d\u3044\u5024\uff09\u3088\u308a\u3082\u5927\u304d\u3051\u308c\u3070\u8272\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u3082\u306e\u3068\u3057\u3066\u30c7\u30fc\u30bf\u4fdd\u5b58<\/li>\n<\/ol>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e\u6d41\u308c\u3092\u5b9f\u88c5\u3059\u308b\u305f\u3081\u306b\u91cd\u8981\u306a\u70b9\u306f\u3001\u300c<strong>\u4eca\u56de\u306f\u4f7f\u308f\u306a\u3044\u9ed2\u3084\u3001\u767d\u306a\u3069\u306e\u30e2\u30ce\u30c8\u30fc\u30f3\u3082\u8272\u3082\u62bd\u51fa\u3059\u308b<\/strong>\u300d\u90e8\u5206\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u4f8b\u3048\u3070\u9ed2\u306b\u8fd1\u3044\u8272\u306f\u9ed2\u3068\u3057\u3066\u51e6\u7406\u3057\u306a\u3044\u3068\u3001\u4e00\u756a\u8fd1\u3044\u5225\u306e\u8272\u3068\u3057\u3066\u5224\u5225\u3057\u3066\u3057\u307e\u3046\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u30b3\u30fc\u30c9\u3092\u898b\u3066\u300c\u304a\u3084\uff1f\u300d\u3068\u601d\u3063\u305f\u4eba\u3082\u3044\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001<code>BASE_COLORS<\/code>\u306e\u4e2d\u8eab\u306b<code>RBG<\/code>\u8272\u306e\u6700\u5927\u5024<code>255<\/code>\u3092\u4f7f\u308f\u305a<code>128<\/code>\u3092\u4f7f\u3063\u3066\u3044\u308b\u90e8\u5206\u304c\u3042\u308a\u307e\u3059\uff08\u3064\u307e\u308a\u3001\u5fae\u5999\u306a\u8d64\u3084\u9752\u3067\u3059\uff09<\/p>\n<p>\u3053\u308c\u306f\u3001\u5199\u771f\u306a\u3069\u3067\u8272\u62bd\u51fa\u3092\u3059\u308b\u5834\u5408\u3001\u81ea\u7136\u754c\u306b\u306f\u300c\u5b8c\u5168\u306a\u8d64\u300d\u3084\u300c\u5b8c\u5168\u306a\u9752\u300d\u306f\u307b\u307c\u5b58\u5728\u3057\u306a\u3044\u3001\u7d50\u679c\u3068\u3057\u3066\u3001\u30c1\u30a7\u30c3\u30af\u3059\u308b\u8272\u3068\u306e\u8ddd\u96e2\u304c\u5927\u304d\u3059\u304e\u3001\u8d64\u3084\u9752\u3068\u3057\u3066\u5224\u5225\u3055\u308c\u306a\u3044\u305f\u3081\u3067\u3059\u3002\uff08\u3053\u3046\u3044\u3046\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u3063\u3066\u96e3\u3057\u3044\u3067\u3059\u306d\u30fb\u30fb\u30fb\ud83d\ude2b\uff09<\/p>\n<p>\u3067\u306f\u3001\u30b3\u30fc\u30c9\u306e\u4e2d\u8eab\u306b\u3064\u3044\u3066\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a<code>__construct()<\/code>\u306e\u4e2d\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u305f\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3063\u3066\u8272\u3092\u62bd\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u5c11\u3057\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u304c\u3001\u3053\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u306f\u300c\u753b\u50cf\u306e\u30c9\u30c3\u30c8\u3054\u3068\u306b\u60c5\u5831\u3092\u53d6\u5f97\u3067\u304d\u308b\u300d<strong>\u306e\u3067\u306f\u306a\u304f<\/strong>\u3001\u5b58\u5728\u3057\u3066\u3044\u308b<strong>\u8272\u304c\u3044\u304f\u3064\u753b\u50cf\u306b\u5165\u3063\u3066\u3044\u308b\u304b<\/strong>\u3068\u3044\u3046\u96c6\u8a08\u6e08\u307f\u30c7\u30fc\u30bf\u3092\u63d0\u4f9b\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u30eb\u30fc\u30d7\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5f62\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>foreach($palette as $color =&gt; $count) {\r\n\r\n    \/\/ $color\u304c\u300c16\u9032\u6570\u306e\u8272\u30b3\u30fc\u30c9\u300d\u3067\u3001$count\u304c\u300c\u4ef6\u6570\u300d\u3067\u3059\r\n\r\n}<\/strong><\/pre>\n<p>\u307e\u305f\u3001\u300c\uff12\u3064\u306e\u8272\u304c\u3069\u308c\u3060\u3051\u8fd1\u3044\u304b\uff1f\u300d\u3092\u8a08\u7b97\u3057\u3066\u3044\u308b\u306e\u304c\u3001<code>getColorDistance()<\/code>\u3067\u3001<code>RBG<\/code>\u306e\uff13\u8272\u306e\u5dee\uff08\u306e\u7d76\u5bfe\u5024\uff09\u3092\u5408\u8a08\u3057\u3066\u3044\u307e\u3059\u3002\uff08\u3064\u307e\u308a\u3001\u307e\u3063\u305f\u304f\u540c\u3058\u8272\u306a\u3089\u7d50\u679c\u306f\u30bc\u30ed\u3067\u3059\u3057\u3001\u8272\u304c\u9055\u3063\u3066\u3044\u308c\u3070\u9055\u3063\u3066\u3044\u308b\u307b\u3069\u3053\u306e\u5024\u306f\u5927\u304d\u304f\u306a\u3063\u3066\u3044\u304d\u307e\u3059\uff09<\/p>\n<p>\u305d\u3057\u3066\u3001\u753b\u50cf\u5185\u306e\u5168\u3066\u306e\u8272\u304c\u3069\u306e\u8272\u306a\u306e\u304b\u3092\u5224\u5225\u3057\u305f\u7d50\u679c\u3001\u5b9a\u6570<code>THRESHOLD<\/code>\u306e\u30d1\u30fc\u30bb\u30f3\u30c8\u3088\u308a\u3082\u591a\u304f\u5b58\u5728\u3057\u3066\u3044\u308c\u3070\u3001<code>item_image_colors<\/code>\u306b\u305d\u306e\u8272\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>\u30a4\u30d9\u30f3\u30c8\u3092\u6709\u52b9\u306b\u3059\u308b<\/h2>\n<p>\u3067\u306f\u3001\u5148\u307b\u3069\u4f5c\u6210\u3057\u305f<code>ItemImageSaved<\/code>\u30a4\u30d9\u30f3\u30c8\u3092\u30e2\u30c7\u30eb\u306b\u30bb\u30c3\u30c8\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/Models\/ItemImage.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\n<strong>use App\\Events\\ItemImageSaved; \/\/ \ud83d\udc48 \u3053\u3053\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f<\/strong>\r\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass ItemImage extends Model\r\n{\r\n    use HasFactory;\r\n\r\n    <strong>\/\/ \ud83d\udc47 \u3053\u3053\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f<\/strong>\r\n<strong>    protected $dispatchesEvents = [<\/strong>\r\n<strong>        'saved' =&gt; ItemImageSaved::class<\/strong>\r\n<strong>    ];<\/strong>\r\n}<\/pre>\n<h1>\u30c6\u30b9\u30c8\u3057\u3066\u307f\u308b<\/h1>\n<p>\u3067\u306f\u3001\u5b9f\u969b\u306b\u30c6\u30b9\u30c8\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u2757<\/p>\n<p>\u307e\u305a\u306f\u753b\u50cf\u3092\u767b\u9332\u3057\u3066\u8272\u3092\u62bd\u51fa\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\u3002\uff08\u30c6\u30b9\u30c8\u306a\u306e\u3067\u30eb\u30fc\u30c8\u306b\u66f8\u3044\u3066\u307e\u3059\u304c\u3001\u672c\u6765\u306f\u30eb\u30fc\u30c8 + \u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3067\u5b9f\u88c5\u3057\u3066\u304f\u3060\u3055\u3044\ud83d\ude0a\uff09<\/p>\n<p><strong>routes\/web.php<\/strong><\/p>\n<pre>\/\/ \u7701\u7565\r\n\r\nRoute::get('color_extract', function(){\r\n\r\n    <strong>for($i = 1 ; $i &lt;= 4 ; $i++) {<\/strong>\r\n\r\n<strong>        $image_path = public_path('images\/color_extract_'. $i .'.jpg');<\/strong>\r\n\r\n<strong>        $item_image = new \\App\\Models\\ItemImage();<\/strong>\r\n<strong>        $item_image-&gt;item_id = $i;<\/strong>\r\n<strong>        $item_image-&gt;path = $image_path;<\/strong>\r\n<strong>        $item_image-&gt;save();<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n});<\/pre>\n<p>ID\uff11\u301c\uff14\u306e\u5546\u54c1\u306b\u5148\u307b\u3069\u3054\u7d39\u4ecb\u3057\u305f\u753b\u50cf\u3092\u3072\u3068\u3064\u305a\u3064\u4fdd\u5b58\uff06\u8272\u62bd\u51fa\u3092\u3057\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u5b9f\u884c\u3057\u305f\u7d50\u679c\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8142\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_2.png\" alt=\"\" width=\"585\" height=\"440\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_2.png 585w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_2-300x226.png 300w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><\/p>\n<p>\u62bd\u51fa\u81ea\u4f53\u306f\u3046\u307e\u304f\u3044\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3067\u306f\u3001\u672c\u5f53\u306b\u6b63\u3057\u3044\u8272\u304c\u62bd\u51fa\u3067\u304d\u3066\u3044\u308b\u304b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u2757<\/p>\n<pre>Route::get('show_items', function(){\r\n\r\n    <strong>$items = \\App\\Models\\Item::with('item_image_colors')\r\n        -&gt;whereHas('item_image_colors') \/\/ \u8272\u30c7\u30fc\u30bf\u304c\u3042\u308b\u3082\u306e\u3060\u3051\r\n        -&gt;get();\r\n\r\n    echo '&lt;div style=\"text-align:center;\"&gt;';\r\n\r\n    foreach ($items as $item) {\r\n\r\n        echo $item-&gt;name .'&lt;br&gt;';\r\n        echo '&lt;img style=\"height:150px;\" src=\"\/images\/color_extract_'. $item-&gt;id .'.jpg\"&gt;&lt;br&gt;';\r\n        echo '\u8272\uff1a '. $item-&gt;item_image_colors-&gt;pluck('color')-&gt;implode(', ');\r\n        echo '&lt;hr&gt;';\r\n\r\n    }\r\n\r\n    echo '&lt;\/div&gt;';<\/strong>\r\n\r\n});<\/pre>\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3057\u305f\u7d50\u679c\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8143\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_3.png\" alt=\"\" width=\"227\" height=\"859\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_3.png 227w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_3-79x300.png 79w\" sizes=\"auto, (max-width: 227px) 100vw, 227px\" \/><\/p>\n<p>\u3069\u3046\u3067\u3057\u3087\u3046\u3002<br \/>\n\u305d\u308c\u306a\u308a\u306b\u3046\u307e\u304f\u8272\u304c\u53d6\u5f97\u3067\u304d\u3066\u3044\u308b\u3093\u3058\u3083\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u3067\u306f\u3001\u6700\u5f8c\u306b\u6307\u5b9a\u3057\u305f\u8272\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\u3002<br \/>\n\u9752\u3067\u6307\u5b9a\u3057\u3066\u53d6\u5f97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>Route::get('search_item_by_color', function(){\r\n\r\n<strong>    $items = \\App\\Models\\Item::with('item_image_colors')<\/strong>\r\n<strong>        -&gt;whereHas('item_image_colors', function($q){ \/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u5148\u3067\u7d5e\u308a\u8fbc\u307f<\/strong>\r\n\r\n<strong>            $q-&gt;where('color', 'blue'); \/\/ \ud83d\udc48 \u9752\u30c7\u30fc\u30bf\u304c\u3042\u308b\u5546\u54c1\u3060\u3051<\/strong>\r\n\r\n<strong>        })<\/strong>\r\n<strong>        -&gt;get();<\/strong>\r\n<strong>    dd($items-&gt;toArray());<\/strong>\r\n\r\n});<\/pre>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u3053\u3046\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8144\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_4.png\" alt=\"\" width=\"329\" height=\"687\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_4.png 329w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/10\/laravel_color_extract_4-144x300.png 144w\" sizes=\"auto, (max-width: 329px) 100vw, 329px\" \/><\/p>\n<p>\u6210\u529f\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<p>\u306a\u304a\u3001\u691c\u7d22\u306b\u5bfe\u5fdc\u3055\u305b\u308b\uff08\u8272\u30c7\u30fc\u30bf\u304c\u9001\u4fe1\u3055\u308c\u305f\u3068\u304d\u3060\u3051\u7d5e\u308a\u8fbc\u3080\uff09\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>when()<\/code>\u3092\u4f7f\u3046\u3068\u4fbf\u5229\u3067\u3059\u3088\ud83d\udc4d<\/p>\n<pre>Route::get('search_item_by_color', function(){\r\n\r\n    <strong>$color = request('color');<\/strong>\r\n\r\n<strong>    $items = \\App\\Models\\Item::with('item_image_colors')<\/strong>\r\n<strong>        -&gt;when($color, function($query, $color){ \/\/ \ud83d\udc48 \u5024\u304c\u3042\u308b\u3068\u304d\u3060\u3051\u5b9f\u884c\u3055\u308c\u307e\u3059<\/strong>\r\n\r\n<strong>            if(in_array($color, ['red', 'blue', 'yellow', 'green'])) {<\/strong>\r\n\r\n<strong>                $query-&gt;whereHas('item_image_colors', function($q) use($color) {<\/strong>\r\n\r\n<strong>                    $q-&gt;where('color', $color);<\/strong>\r\n\r\n<strong>                });<\/strong>\r\n\r\n<strong>            }<\/strong>\r\n\r\n<strong>        })<\/strong>\r\n<strong>        -&gt;get();<\/strong>\r\n<strong>    dd($items-&gt;toArray());<\/strong>\r\n\r\n});<\/pre>\n<p>\u203b <code>when()<\/code>\u306f\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u5834\u5408\u3060\u3051\u5b9f\u884c\u3055\u308c\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<h1>\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b<\/h1>\n<p>\u4eca\u56de\u5b9f\u969b\u306b\u958b\u767a\u3057\u305f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u4e00\u5f0f\u3092\u4ee5\u4e0b\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3067\u304d\u307e\u3059\u3002<\/p>\n<a  data-e-Disable-Page-Transition=\"true\" class=\"download-link\" title=\"\" href=\"https:\/\/blog.capilano-fw.com?download=8148&amp;tmstv=1777527147\" rel=\"nofollow\" id=\"download-link-8148\" data-redirect=\"false\" >\n\t\u3010Laravel\u3011\u5199\u771f\u304b\u3089\u8272\u60c5\u5831\u3092\u53d6\u5f97<\/a>\n\n<p>\u203b\u4eca\u56de\u4f7f\u7528\u3057\u305f\uff14\u679a\u306e\u5199\u771f\u3082\u5165\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n\u203b\u305f\u3060\u3057\u3001\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306a\u3069\u306f\u3054\u81ea\u8eab\u3067\u5b9f\u884c\u3057\u3066\u3044\u305f\u3060\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u4eca\u56de\u306f\u3001<code>Laravel<\/code>\u3092\u4f7f\u3063\u3066\u753b\u50cf\u304b\u3089\u8272\u3092\u62bd\u51fa\uff06\u691c\u7d22\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u4eca\u56de\u958b\u767a\u3057\u3066\u307f\u305f\u611f\u60f3\u3068\u3057\u307e\u3057\u3066\u306f\u3001\u3084\u306f\u308a\u300c\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u304c\u96e3\u3057\u3044\u300d\u306e\u4e00\u8a00\u3067\u3059\u3002<\/p>\n<p>\u8a18\u4e8b\u306e\u4e2d\u3067\u3082\u66f8\u304d\u307e\u3057\u305f\u304c\u3001\u6700\u521d\u306f\u300c<strong>\u9752\u306a\u306e\u306b\u9ed2<\/strong>\u300d\u3068\u5224\u5225\u3055\u308c\u3066\u3057\u307e\u3046\u306a\u3069\u7cbe\u5ea6\u306b\u554f\u984c\u304c\u3042\u3063\u305f\u305f\u3081\u3044\u308d\u3044\u308d\u3068\u5de5\u592b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\u3002\ud83d\ude05<\/p>\n<p>\u307e\u305f\u3001\u3053\u308c\u3082\u91cd\u8981\u306a\u70b9\u3060\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u901a\u5e38\u5199\u771f\u3092\u64ae\u308b\u3068\u304d\u306f\u30e1\u30a4\u30f3\u306e\u7269\u4f53\u3060\u3051\u3067\u306a\u304f\u3001\u305d\u306e\u7269\u4f53\u3092\u7f6e\u3044\u3066\u3044\u308b\u30c6\u30fc\u30d6\u30eb\u3084\u80cc\u666f\u304c\u6620\u308a\u8fbc\u3093\u3067\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u3053\u306e\u6620\u308a\u8fbc\u307f\u306b\u3082\u3082\u3061\u308d\u3093\u8272\u304c\u5165\u3063\u3066\u3044\u308b\u308f\u3051\u3067\u3001\u305d\u308c\u3092\u9664\u53bb\u3059\u308b\u306b\u306f\u6a5f\u68b0\u5b66\u7fd2\u306e<strong>\u300c\u7269\u4f53\u691c\u51fa\u300d\u306a\u3069\u3067\u4e0d\u8981\u306a\u90e8\u5206\u3092\u30ab\u30c3\u30c8\u3057\u3066\u304b\u3089\u8272\u691c\u51fa\u3059\u308b\u306a\u3069\u306e\u5de5\u592b\u3082\u5fc5\u8981<\/strong>\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\uff08\u4eca\u56de\u306f\u624b\u52d5\u3067\u30ab\u30c3\u30c8\u3057\u307e\u3057\u305f\uff09<\/p>\n<p>\u3068\u306f\u3044\u3048\u3001\u4eca\u56de\u306e\u753b\u50cf\u3060\u3051\u3067\u306a\u304f\u5225\u306e\u753b\u50cf\u3067\u3082\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u304c\u3001\u7d50\u679c\u306f\u305d\u308c\u307b\u3069\u5916\u3057\u3066\u306f\u3044\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u3082\u3084\u3063\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\u2757<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3467 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png\" alt=\"\" width=\"216\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9.png 216w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_9-203x300.png 203w\" sizes=\"auto, (max-width: 216px) 100vw, 216px\" \/>\u300c\u7121\u6027\u306bFF\uff15\u304c\u3084\u308a\u305f\u304f\u306a\u3063\u3066\u304d\u307e\u3057\u305f\u30fb\u30fb\u30fb\u300d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u3053\u306e\u3068\u3053\u308d\u3042\u307e\u308a\u5916\u51fa\u3092\u3057\u306a\u304f\u306a\u3063\u305f\u3053\u3068\u3082\u3042\u308a\u3001\u30aa\u30f3\u30e9\u30a4\u30f3\u30b7\u30e7\u30c3\u30d7\u3067\u30e2\u30ce\u3092\u8cb7\u3046\u3053\u3068\u304c\u5897\u3048\u3044\u307e\u3059\u3002 \u305d\u3057\u3066\u3001\u6700\u8fd1\u3067\u306f\u670d\u3084\u9774\u306a\u3093\u304b\u3082\u305f\u307e\u306b\u8cb7\u3046\u3093\u3067\u3059\u304c\u3001\u3053 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=8128\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u3010Laravel\u3011\u5199\u771f\u304b\u3089\u8272\u60c5\u5831\u3092\u53d6\u5f97\u3057\u3001\u691c\u7d22\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":8154,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,97],"tags":[83],"class_list":["post-8128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","category-97","tag-83"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/8128","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=8128"}],"version-history":[{"count":16,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/8128\/revisions"}],"predecessor-version":[{"id":8158,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/8128\/revisions\/8158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/8154"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}