{"id":11412,"date":"2023-02-27T07:00:39","date_gmt":"2023-02-26T22:00:39","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=11412"},"modified":"2023-01-31T19:12:01","modified_gmt":"2023-01-31T10:12:01","slug":"laravel-react-%e3%81%a7%e6%a4%9c%e7%b4%a2%e3%82%b5%e3%82%b8%e3%82%a7%e3%82%b9%e3%83%88%e6%a9%9f%e8%83%bd%e3%82%92%e3%81%a4%e3%81%8f%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=11412","title":{"rendered":"Laravel + React \u3067\u691c\u7d22\u30b5\u30b8\u30a7\u30b9\u30c8\u6a5f\u80fd\u3092\u3064\u304f\u308b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\uff08\u79c1\u306e\u4e2d\u3067\u306f\u3067\u3059\u304c\uff09\u3053\u306e\u30d6\u30ed\u30b0\u3067\u306f\u300c<strong>\u30e6\u30fc\u30b6\u30d3\u30ea\u30c6\u30a3<\/strong>\u300d\u3084\u300c<strong>\u30db\u30b9\u30d4\u30bf\u30ea\u30c6\u30a3<\/strong>\u300d\u3092\u30c6\u30fc\u30de\u306b\u3057\u305f\u8a18\u4e8b\u3092\u5b9a\u671f\u7684\u306b\u304a\u5c4a\u3051\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3068\u3044\u3046\u306e\u3082\u3001\u5b9f\u969b\u306b\u300c<strong>\u304a\u304a\u3063\u3001\u3053\u308c\u306f\u4f7f\u3044\u3084\u3059\u3044<\/strong>\u300d\u3068\u3044\u3046\u30b5\u30a4\u30c8\u304c\u3042\u308b\u3068\u3001\u305d\u308c\u3060\u3051\u3067\u30ea\u30d4\u30fc\u30c8\u3059\u308b\u7406\u7531\u306b\u30d7\u30e9\u30b9\u5f97\u70b9\u306b\u306a\u3063\u305f\u3053\u3068\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u203b \u3061\u306a\u307f\u306b\u6700\u8fd1\u3060\u3068 <a href=\"https:\/\/www.misoca.jp\/\" target=\"_blank\" rel=\"noopener\">misoca<\/a> \u3084 <a href=\"https:\/\/mail.yahoo.co.jp\/\" target=\"_blank\" rel=\"noopener\">\u30e4\u30d5\u30fc\u30e1\u30fc\u30eb<\/a>\uff08\u30d6\u30e9\u30a6\u30b6\u7248 in \u30e2\u30d0\u30a4\u30eb\uff09\u306f\u300c\u4f7f\u3046\u4eba\u306e\u3053\u3068\u3092\u8003\u3048\u3066\u304f\u308c\u3066\u308b\u306a\u3041\u300d\u3068\u611f\u3058\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u305d\u3046\u8003\u3048\u308b\u3068<code>React<\/code>\u3067\u3082\u3042\u308b\u6a5f\u80fd\u3092\u3064\u304f\u3063\u3066\u307f\u305f\u304f\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u308c\u306f\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb<\/p>\n<p><strong style=\"font-size: 35px;\">\u691c\u7d22\u30b5\u30b8\u30a7\u30b9\u30c8\u6a5f\u80fd<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u300c<strong>\u904e\u53bb\u306b\u691c\u7d22\u3057\u305f\u3053\u3068\u306e\u3042\u308b\u30ad\u30fc\u30ef\u30fc\u30c9<\/strong>\u300d\u3092\u81ea\u52d5\u3067\u8868\u793a\uff06\u9078\u629e\u3067\u304d\u308b\u6a5f\u80fd\u3067\u3059\u306d\u3002\uff08\u3057\u304b\u3082\u6587\u5b57\u3092\u5165\u308c\u308b\u3068\u305d\u306e\u6587\u5b57\u3092\u542b\u3093\u3067\u3044\u308b\u3082\u306e\u3060\u3051\u306b\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3055\u308c\u307e\u3059\uff09<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11418\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_2.png\" alt=\"\" width=\"338\" height=\"283\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_2.png 338w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_2-300x251.png 300w\" sizes=\"auto, (max-width: 338px) 100vw, 338px\" \/><\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u300c<strong>Laravel + React<\/strong>\u300d\u3067\u691c\u7d22\u30b5\u30b8\u30a7\u30b9\u30c8\u6a5f\u80fd\u3092\u3064\u304f\u3063\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u4f55\u304b\u306e\u53c2\u8003\u306b\u306a\u308a\u307e\u3057\u305f\u3089\u5b09\u3057\u3044\u3067\u3059\u3002\ud83d\ude04\u2728<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3566 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png\" alt=\"\" width=\"306\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png 306w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1-287x300.png 287w\" sizes=\"auto, (max-width: 306px) 100vw, 306px\" \/>8000 \u30dd\u30fc\u30c8\u304c\u30c0\u30e1\u306a\u3068\u304d\u3001<br \/>\n4989\uff08\u56db\u82e6\u516b\u82e6\uff09\u3092<br \/>\n\u3064\u304b\u3044\u307e\u3059\ud83d\ude09\ud83d\udc4d<\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 9.x\u3001React\u3001Vite\u3001Inertia.js\u3001TailwindCSS<\/p>\n<h1>\u30e2\u30c7\u30eb\uff06\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u304f\u308b<\/h1>\n<p>\u307e\u305a\u306f\u3001<code>DB<\/code>\u5468\u308a\u306e\u6e96\u5099\u3092\u3057\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<div>\n<pre><strong>php artisan make:model Suggestion -ms<\/strong><\/pre>\n<\/div>\n<p>\u3059\u308b\u3068\u3001\u300c<strong>\u30e2\u30c7\u30eb<\/strong>\u300d\u3060\u3051\u3067\u306a\u304f\u300c<strong>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3<\/strong>\u300d\u300c<strong>Seeder<\/strong>\u300d\uff13\u3064\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u4e00\u6c17\u306b\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u6b21\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<h2>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3<\/h2>\n<p><strong>database\/migrations\/****_**_**_******_create_suggestions_table.php<\/strong><\/p>\n<div>\n<pre>\/\/ \u7701\u7565\r\n\r\npublic function up()\r\n{\r\n    <strong>Schema::create('suggestions', function (Blueprint $table) {<\/strong>\r\n<strong>        $table-&gt;id();<\/strong>\r\n<strong>        $table-&gt;string('key')-&gt;comment('\u30ad\u30fc');<\/strong>\r\n<strong>        $table-&gt;string('word')-&gt;comment('\u691c\u7d22\u30ef\u30fc\u30c9');<\/strong>\r\n<strong>        $table-&gt;timestamps();<\/strong>\r\n\r\n<strong>        $table-&gt;index('key');<\/strong>\r\n<strong>    });<\/strong>\r\n}\r\n\r\n\/\/ \u7701\u7565<\/pre>\n<p>\u691c\u7d22\u30ef\u30fc\u30c9\u306e\u4ef6\u6570\u304c\u591a\u3044\u9806\u306b\u4e26\u3079\u66ff\u3048\u305f\u3044\u306e\u3067\u3001<code>key<\/code>\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u3064\u3051\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>Seeder\uff08\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u4f5c\u6210\uff09<\/h2>\n<\/div>\n<p><strong>database\/seeders\/SuggestionSeeder.php<\/strong><\/p>\n<div>\n<pre>\/\/ \u7701\u7565\r\n\r\npublic function run()\r\n{\r\n    <strong>for($i = 0; $i &lt; 100; $i++) {<\/strong>\r\n\r\n<strong>        $suggestion = new Suggestion();<\/strong>\r\n<strong>        $suggestion-&gt;key = 'food';<\/strong>\r\n<strong>        $suggestion-&gt;word = Arr::random([<\/strong>\r\n<strong>            '\u30e9\u30fc\u30e1\u30f3',<\/strong>\r\n<strong>            '\u30ab\u30ec\u30fc',<\/strong>\r\n<strong>            '\u30d4\u30b6',<\/strong>\r\n<strong>            '\u30d1\u30b9\u30bf',<\/strong>\r\n<strong>            '\u30cf\u30f3\u30d0\u30fc\u30b0',<\/strong>\r\n<strong>        ]);<\/strong>\r\n<strong>        $suggestion-&gt;save();<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<p>\u5185\u5bb9\u3068\u3057\u3066\u306f\u3001\u30e9\u30f3\u30c0\u30e0\u3067\u4e0a\u8a18\u306e\u98df\u3079\u7269\u3092\u5408\u8a08\uff11\uff10\uff10\u500b\u4fdd\u5b58\u3059\u308b\u3082\u306e\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<\/div>\n<p><code>Seeder<\/code>\u306f\u4f5c\u6210\u3057\u305f\u3060\u3051\u3067\u306f\u53cd\u6620\u3055\u308c\u307e\u305b\u3093\u306e\u3067\u3001<code>Laravel<\/code>\u5074\u3078\u767b\u9332\u3057\u307e\u3059\u3002<\/p>\n<p><strong>database\/seeders\/DatabaseSeeder.php<\/strong><\/p>\n<div>\n<pre>&lt;?php\r\n\r\nnamespace Database\\Seeders;\r\n\r\n\/\/ use Illuminate\\Database\\Console\\Seeds\\WithoutModelEvents;\r\nuse Illuminate\\Database\\Seeder;\r\n\r\nclass DatabaseSeeder extends Seeder\r\n{\r\n    \/**\r\n     * Seed the application's database.\r\n     *\r\n     * @return void\r\n     *\/\r\n    public function run()\r\n    {\r\n        \/\/ \u7701\u7565\r\n\r\n        <strong>$this-&gt;call(SuggestionSeeder::class); \/\/ \ud83d\udc48 \u3053\u3053\u3092\u8ffd\u52a0<\/strong>\r\n    }\r\n}<\/pre>\n<\/div>\n<p>\u3067\u306f\u3001\u3053\u306e\u72b6\u614b\u3067<code>DB<\/code>\u3092\u518d\u69cb\u7bc9\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<div>\n<pre><strong>php artisan migrate:fresh --seed<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001\u5b9f\u969b\u306e\u30c6\u30fc\u30d6\u30eb\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11422\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_5.png\" alt=\"\" width=\"539\" height=\"384\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_5.png 539w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_5-300x214.png 300w\" sizes=\"auto, (max-width: 539px) 100vw, 539px\" \/><\/p>\n<\/div>\n<h1>\uff08\u30b5\u30b8\u30a7\u30b9\u30c8\u7528\u306e\uff09\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u3064\u304f\u308b<\/h1>\n<p>\u7d9a\u3044\u3066\u3001\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\u306e\u5019\u88dc\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u3064\u304f\u308a\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<div>\n<pre><strong>php artisan make:controller SuggestionController<\/strong><\/pre>\n<\/div>\n<p>\u3059\u308b\u3068\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u30fb\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/Http\/Controllers\/SuggestionController.php<\/strong><\/p>\n<div>\n<pre><strong>&lt;?php<\/strong>\r\n\r\n<strong>namespace App\\Http\\Controllers;<\/strong>\r\n\r\n<strong>use App\\Models\\Suggestion;<\/strong>\r\n<strong>use Illuminate\\Http\\Request;<\/strong>\r\n\r\n<strong>class SuggestionController extends Controller<\/strong>\r\n<strong>{<\/strong>\r\n<strong>    public function list(Request $request): array<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        \/\/ \u691c\u7d22\u3055\u308c\u305f\u4ef6\u6570\u304c\u591a\u3044\u9806\u306b\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b<\/strong>\r\n<strong>        $suggestions = Suggestion::query()<\/strong>\r\n<strong>            -&gt;selectRaw('word, count(*) as count')<\/strong>\r\n<strong>            -&gt;where('key', $request-&gt;key)<\/strong>\r\n<strong>            -&gt;groupBy('word')<\/strong>\r\n<strong>            -&gt;orderBy('count', 'desc')<\/strong>\r\n<strong>            -&gt;limit(10)<\/strong>\r\n<strong>            -&gt;get();<\/strong>\r\n\r\n<strong>        return $suggestions-&gt;pluck('word')-&gt;toArray();<\/strong>\r\n<strong>    }<\/strong>\r\n<strong>}<\/strong><\/pre>\n<\/div>\n<p>\u3053\u306e\u4e2d\u3067\u306f\u3001<code>key<\/code>\u3067\u7d5e\u308a\u8fbc\u3093\u3060\u30c7\u30fc\u30bf\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u3001\u4ef6\u6570\u304c\u591a\u3044\u9806\u306b\u4e26\u3079\u66ff\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u4eca\u56de\u306f\u30bf\u30a4\u30d7\u30d2\u30f3\u30c8\u3092\u3075\u3093\u3060\u3093\u306b\u4f7f\u3063\u3066\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u3046\u306a\u308b\u3068\u3001\u8fd4\u308a\u5024\u3092<code>array<\/code>\u306b\u3057\u305f\u304f\u306a\u308a\u3001\u305d\u3046\u306a\u308b\u3068\u3053\u308c\u307e\u3067\u66f8\u304b\u306a\u304f\u3066\u3088\u304b\u3063\u305f<code>toArray()<\/code>\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3057\u305f\u3002\uff08\u3046\u30fc\u3093\u3001\u4e16\u306e\u4e2d\u306e\u6d41\u308c\u3068\u306f\u3044\u3048\u30b3\u30fc\u30c9\u304c\u591a\u304f\u306a\u308b\u306e\u306f\u3061\u3087\u3063\u3068\u2026\u3068\u601d\u3046\u4eca\u65e5\u3053\u306e\u9803\u3067\u3059\ud83d\ude02 \u30b7\u30f3\u30d7\u30eb\u306b\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3067\u3044\u3044\u3067\u3057\u3087\u3046\u304b\ud83e\udd14\uff09<\/p>\n<h1>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u3064\u304f\u308b<\/h1>\n<p>\u4eca\u56de\u3001\u30b5\u30b8\u30a7\u30b9\u30c8\u30c7\u30fc\u30bf\u306f\u300c<strong>datalist\u30bf\u30b0<\/strong>\u300d\u3067\u4f5c\u308b\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u90e8\u5206\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><strong>resources\/js\/Components\/SuggestionDataList.jsx<\/strong><\/p>\n<div>\n<pre><strong>import {useEffect, useState} from \"react\";<\/strong>\r\n\r\n<strong>export default function SuggestionDataList(props) {<\/strong>\r\n\r\n<strong>    \/\/ Data<\/strong>\r\n<strong>    const listKey = props.listKey;<\/strong>\r\n<strong>    const [suggestionWords, setSuggestionWords] = useState([]);<\/strong>\r\n\r\n<strong>    useEffect(() =&gt; {<\/strong>\r\n\r\n<strong>        if(listKey) {<\/strong>\r\n\r\n<strong>            const url = route('suggestion.list');<\/strong>\r\n<strong>            axios.post(url, {key: listKey})<\/strong>\r\n<strong>                .then(response =&gt; {<\/strong>\r\n\r\n<strong>                    setSuggestionWords(response.data)<\/strong>\r\n\r\n<strong>                });<\/strong>\r\n\r\n<strong>        }<\/strong>\r\n\r\n<strong>    }, []);<\/strong>\r\n\r\n<strong>    return (<\/strong>\r\n<strong>        &lt;datalist id={listKey}&gt;<\/strong>\r\n<strong>            {suggestionWords.map(word =&gt; (<\/strong>\r\n<strong>                &lt;option key={word} value={word} \/&gt;<\/strong>\r\n<strong>            ))}<\/strong>\r\n<strong>        &lt;\/datalist&gt;<\/strong>\r\n<strong>    );<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p>\u3053\u306e\u4e2d\u3067\u306f\u3001\u8868\u793a\u3055\u308c\u305f\u3089\u3059\u3050\u306b\u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u3092<code>Ajax<\/code>\u3067\u53d6\u5f97\u3057\u3001\u305d\u306e\u30c7\u30fc\u30bf\u3092<code>&lt;datalist&gt;...&lt;\/datalist&gt;<\/code>\u306b\u30bb\u30c3\u30c8\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e\u90e8\u5206\u304c<code>&lt;input type=\"text\" \/&gt;<\/code>\u3068\u5bfe\u5fdc\u3057\u3066\u5019\u88dc\u3092\u8868\u793a\u3057\u3066\u304f\u308c\u308b\u308f\u3051\u3067\u3059\u306d\u3002\u4fbf\u5229\u2757<\/p>\n<\/div>\n<h1>\uff08\u691c\u7d22\u7528\u306e\uff09\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u3064\u304f\u308b<\/h1>\n<p>\u6b21\u306b\u3001\u691c\u7d22\u7528\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3067\u3059\u3002<br \/>\n\u3053\u308c\u306f\u4eca\u56de\u306e\u6a5f\u80fd\u3068\u3057\u3066\u306f\u5fc5\u9808\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u30c6\u30b9\u30c8\u3057\u3084\u3059\u3044\u306e\u3067\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<div>\n<pre><strong>php artisan make:controller SearchController<\/strong><\/pre>\n<\/div>\n<p>\u3059\u308b\u3068\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u30fb\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>app\/Http\/Controllers\/SearchController.php<\/strong><\/p>\n<div>\n<pre><strong>&lt;?php<\/strong>\r\n\r\n<strong>namespace App\\Http\\Controllers;<\/strong>\r\n\r\n<strong>use App\\Models\\Suggestion;<\/strong>\r\n<strong>use Illuminate\\Http\\Request;<\/strong>\r\n<strong>use Inertia\\Inertia;<\/strong>\r\n<strong>use Inertia\\Response;<\/strong>\r\n\r\n<strong>class SearchController extends Controller<\/strong>\r\n<strong>{<\/strong>\r\n<strong>    public function form(): Response<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        return Inertia::render('Search\/Form');<\/strong>\r\n<strong>    }<\/strong>\r\n\r\n<strong>    public function search(Request $request): array<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        \/\/ \u691c\u7d22\u7d50\u679c\u304c\u5b58\u5728\u3057\u3066\u3044\u305f\u5834\u5408\uff08\u4eca\u56de\u306f\u6bce\u56de\u5b9f\u884c\uff09<\/strong>\r\n<strong>        $has_data = true;<\/strong>\r\n\r\n<strong>        if($has_data === true) { \/\/ \ud83d\udc48 \u30c6\u30b9\u30c8\u306a\u306e\u3067\u6bce\u56de\u5b9f\u884c<\/strong>\r\n\r\n<strong>            $key = 'food';<\/strong>\r\n<strong>            $word = $request-&gt;word;<\/strong>\r\n<strong>            Suggestion::add($key, $word); \/\/ \u30b5\u30b8\u30a7\u30b9\u30c8\u3092\u8ffd\u52a0<\/strong>\r\n\r\n<strong>        }<\/strong>\r\n\r\n<strong>        return [];<\/strong>\r\n<strong>    }<\/strong>\r\n<strong>}<\/strong><\/pre>\n<\/div>\n<p>\u3053\u306e\u4e2d\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5185\u5bb9\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>form(): \u691c\u7d22\u30d5\u30a9\u30fc\u30e0<\/li>\n<li>search(): \u691c\u7d22\u7d50\u679c\u306e\u30c7\u30fc\u30bf\u53d6\u5f97\u7528\uff08Ajax\uff09<\/li>\n<\/ul>\n<p>\u306a\u304a\u3001\u3053\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306f\u30c6\u30b9\u30c8\u5411\u3051\u3067\u3059\u306e\u3067\u6bce\u56de\u65b0\u3057\u3044\u30ad\u30fc\u30ef\u30fc\u30c9\u3092\u4fdd\u5b58\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u3082\u3057\u691c\u7d22\u7d50\u679c\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u4fdd\u5b58\u90e8\u5206\u3092\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3088\u3046\u306a\u6d41\u308c\u3092\u60f3\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h1>\u30d3\u30e5\u30fc\u3092\u3064\u304f\u308b<\/h1>\n<p>\u3067\u306f\u3001<code>SearchController<\/code>\u306e<code>form()<\/code>\u3067\u30bb\u30c3\u30c8\u3057\u305f\u30d3\u30e5\u30fc\u306e\u4e2d\u8eab\u3092\u3064\u304f\u3063\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><strong>resources\/js\/Pages\/Search\/Form.jsx<\/strong><\/p>\n<div>\n<pre><strong>import SuggestionDataList from \"@\/Components\/SuggestionDataList\";<\/strong>\r\n<strong>import {useState} from \"react\";<\/strong>\r\n\r\n<strong>export default function Form() {<\/strong>\r\n\r\n<strong>    \/\/ Data<\/strong>\r\n<strong>    const listKey = 'food';<\/strong>\r\n\r\n<strong>    \/\/ Parameters<\/strong>\r\n<strong>    const [searchWord, setSearchWord] = useState('');<\/strong>\r\n\r\n<strong>    \/\/ Submit<\/strong>\r\n<strong>    const handleSubmit = () =&gt; {<\/strong>\r\n\r\n<strong>        const url = route('search.search');<\/strong>\r\n<strong>        const params = { word: searchWord };<\/strong>\r\n\r\n<strong>        axios.post(url, params)<\/strong>\r\n<strong>            .then(response =&gt; {<\/strong>\r\n\r\n<strong>                \/\/ \u3053\u3053\u3067\u691c\u7d22\u7d50\u679c\u3092\u51e6\u7406\u3059\u308b<\/strong>\r\n\r\n<strong>            });<\/strong>\r\n\r\n<strong>    };<\/strong>\r\n\r\n<strong>    return (<\/strong>\r\n<strong>        &lt;div className=\"p-5\"&gt;<\/strong>\r\n<strong>            &lt;input<\/strong>\r\n<strong>                type=\"text\"<\/strong>\r\n<strong>                className=\"mr-2\"<\/strong>\r\n<strong>                placeholder=\"\u691c\u7d22\u30ef\u30fc\u30c9\u3092\u5165\u529b\"<\/strong>\r\n<strong>                list={listKey}<\/strong>\r\n<strong>                value={searchWord}<\/strong>\r\n<strong>                onChange={e =&gt; setSearchWord(e.target.value)} \/&gt;<\/strong>\r\n<strong>            &lt;SuggestionDataList listKey={listKey} \/&gt;<\/strong>\r\n\r\n<strong>            &lt;button className=\"bg-blue-500 text-white px-4 py-2.5 rounded\" onClick={handleSubmit}&gt;\u9001\u4fe1&lt;\/button&gt;<\/strong>\r\n<strong>        &lt;\/div&gt;<\/strong>\r\n<strong>    );<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<\/div>\n<p>\u3053\u306e\u4e2d\u3067\u91cd\u8981\u306a\u306e\u304c\u3001<code>&lt;input&gt;<\/code>\u3068<code>&lt;SuggestionDataList&gt;<\/code>\u304c\u7d10\u4ed8\u3044\u3066\u3044\u308b\u3068\u3044\u3046\u70b9\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001<code>&lt;SuggestionDataList&gt;<\/code>\u306f\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3059\u304c\u3001\u5b9f\u614b\u306f<code>&lt;datalist&gt;<\/code>\u3092\u4f5c\u6210\u3059\u308b\u3082\u306e\u3067\u3059\u306e\u3067\u3001\u7d50\u679c\u3068\u3057\u3066\u901a\u5e38\u306e\u300c<strong>input + datalist<\/strong>\u300d\u306e\u69cb\u6210\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u305d\u306e\u7d10\u4ed8\u3051\u306f<code>listKey<\/code>\u306b\u3088\u3063\u3066\u8b58\u5225\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h1>\u30eb\u30fc\u30c8\u3092\u3064\u304f\u308b<\/h1>\n<p>\u3067\u306f\u3001\u6700\u5f8c\u306b\u30eb\u30fc\u30c8\u3092\u3064\u304f\u308a\u307e\u3059\u3002<br \/>\n\u3053\u308c\u307e\u3067\u3067\u4f5c\u6210\u3057\u305f\uff12\u3064\u306e\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30bb\u30c3\u30c8\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>routes\/web.php<\/strong><\/p>\n<div>\n<pre>\/\/ \u7701\u7565\r\n\r\n<strong>\/\/ Suggestion<\/strong>\r\n<strong>use App\\Http\\Controllers\\SuggestionController;<\/strong>\r\n<strong>use App\\Http\\Controllers\\SearchController;<\/strong>\r\n\r\n<strong>Route::prefix('suggestion')-&gt;controller(SuggestionController::class)-&gt;group(function(){<\/strong>\r\n\r\n<strong>    Route::post('list', 'list')-&gt;name('suggestion.list');<\/strong>\r\n\r\n<strong>});<\/strong>\r\n<strong>Route::prefix('search')-&gt;controller(SearchController::class)-&gt;group(function(){<\/strong>\r\n\r\n<strong>    Route::get('form', 'form')-&gt;name('search.form');<\/strong>\r\n<strong>    Route::post('search', 'search')-&gt;name('search.search');<\/strong>\r\n\r\n<strong>});<\/strong><\/pre>\n<\/div>\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><code>Vite<\/code>\u3092\u8d77\u52d5\u3057\u3066\u3001\u30d6\u30e9\u30a6\u30b6\u3067\u300c<strong>http:\/\/******\/search\/form<\/strong>\u300d\u3078\u30a2\u30af\u30bb\u30b9\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8868\u793a\u3055\u308c\u307e\u3059\u306e\u3067\u3001\u691c\u7d22\u30dc\u30c3\u30af\u30b9\u306b\u30d5\u30a9\u30fc\u30ab\u30b9\u3092\u5f53\u3066\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11419\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_1.png\" alt=\"\" width=\"345\" height=\"86\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_1.png 345w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_1-300x75.png 300w\" sizes=\"auto, (max-width: 345px) 100vw, 345px\" \/><\/p>\n<p>\u3059\u308b\u3068\u3001<code>suggestions<\/code>\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u53d6\u5f97\u3055\u308c\u305f\u300c<strong>food<\/strong>\u300d\u306e\u30ad\u30fc\u30ef\u30fc\u30c9\u304c\u8868\u793a\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11418\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_2.png\" alt=\"\" width=\"338\" height=\"283\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_2.png 338w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_2-300x251.png 300w\" sizes=\"auto, (max-width: 338px) 100vw, 338px\" \/><\/p>\n<p>\u3067\u306f\u3001\u3053\u3053\u306b\u65b0\u3057\u3044\u300c\u98df\u3079\u3082\u306e\u30ef\u30fc\u30c9\u300d\u3068\u3057\u3066\u300c<strong>\u5e72\u3057\u3044\u3082<\/strong>\u300d\u3068\u5165\u529b\u3057\u3001\u9001\u4fe1\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11417\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_3.png\" alt=\"\" width=\"341\" height=\"86\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_3.png 341w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_3-300x76.png 300w\" sizes=\"auto, (max-width: 341px) 100vw, 341px\" \/><\/p>\n<p>\u30d6\u30e9\u30a6\u30b6\u4e0a\u3067\u306f\u4f55\u3082\u5909\u5316\u306f\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u88cf\u3067\u306f\u52d5\u3044\u3066\u3044\u308b\u306f\u305a\u306a\u306e\u3067\u3001\u3053\u306e\u72b6\u614b\u3067\u30ea\u30ed\u30fc\u30c9\u3057\u3001\u518d\u5ea6\u691c\u7d22\u30dc\u30c3\u30af\u30b9\u306b\u30d5\u30a9\u30fc\u30ab\u30b9\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u3069\u3046\u306a\u308b\u3067\u3057\u3087\u3046\u304b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11416\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_4.png\" alt=\"\" width=\"331\" height=\"315\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_4.png 331w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2023\/01\/laravel_react_search_suggestion_4-300x285.png 300w\" sizes=\"auto, (max-width: 331px) 100vw, 331px\" \/><\/p>\n<p>\u306f\u3044\u2757<br \/>\n\u300c<strong>\u5e72\u3057\u3044\u3082<\/strong>\u300d\u304c\u65b0\u3057\u304f\u5019\u88dc\u306b\u8868\u793a\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p>\u6210\u529f\u3067\u3059\ud83d\ude04\u2728<\/p>\n<h1>\u4f01\u696d\u69d8\u3078\u306e\u3054\u63d0\u6848<\/h1>\n<p>\u4eca\u56de\u306e\u3088\u3046\u306b\u3001\u300c<strong>\u30e6\u30fc\u30b6\u30fc\u306e\u64cd\u4f5c\u3057\u3084\u3059\u3055\uff1d\uff08\u30e6\u30fc\u30b6\u30fc\u30d3\u30ea\u30c6\u30a3\uff09<\/strong>\u300d\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u306f\u3001\u30e6\u30fc\u30b6\u30fc\u306e\u6ede\u5728\u6642\u9593\u3084\u518d\u8a2a\u554f\u306e\u591a\u3055\u306b\u3082\u3064\u306a\u304c\u308a\u307e\u3059\u3002<\/p>\n<p>\u3082\u3057\u4eca\u56de\u306e\u3088\u3046\u306a\u300c<strong>\u304b\u3086\u3044\u3068\u3053\u308d\u306b\u624b\u304c\u5c4a\u304f<\/strong>\u300d\u6a5f\u80fd\u3092\u3054\u5e0c\u671b\u3067\u3057\u305f\u3089\u3044\u3064\u3067\u3082\u304a\u554f\u3044\u5408\u308f\u305b\u3088\u308a\u3054\u76f8\u8ac7\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u304a\u5f85\u3061\u3057\u3066\u304a\u308a\u307e\u3059\u3002\ud83d\ude04\u2728<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u300c<strong>Laravel + React<\/strong>\u300d\u3067\u691c\u7d22\u30b5\u30b8\u30a7\u30b9\u30c8\u6a5f\u80fd\u3092\u3064\u304f\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u306e\u6614\u306f<code>datalist<\/code>\u304c\u306a\u304b\u3063\u305f\u306e\u3067\u5165\u529b\u306e\u30b5\u30b8\u30a7\u30b9\u30c8\u6a5f\u80fd\u3092\u3064\u304f\u308b\u306e\u306f<code>select2<\/code>\u306a\u3069\u3092\u4f7f\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\u304c\u3001\u73fe\u5728\u3067\u306f\u3068\u3066\u3082\u697d\u306b\u5b9f\u88c5\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u306d\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\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-9470 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/08\/no_13.png\" alt=\"\" width=\"294\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/08\/no_13.png 294w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/08\/no_13-276x300.png 276w\" sizes=\"auto, (max-width: 294px) 100vw, 294px\" \/>\u300c\u3075\u308b\u3055\u3068\u7d0d\u7a0e\u306e\u8fd4\u793c\u54c1\u3001<br \/>\n\u30a6\u30de\u304b\u3063\u305f\ud83d\ude04\u2728\u300d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\uff08\u79c1\u306e\u4e2d\u3067\u306f\u3067\u3059\u304c\uff09\u3053\u306e\u30d6\u30ed\u30b0\u3067\u306f\u300c\u30e6\u30fc\u30b6\u30d3\u30ea\u30c6\u30a3\u300d\u3084\u300c\u30db\u30b9\u30d4\u30bf\u30ea\u30c6\u30a3\u300d\u3092\u30c6\u30fc\u30de\u306b\u3057\u305f\u8a18\u4e8b\u3092\u5b9a\u671f\u7684\u306b\u304a\u5c4a\u3051\u3057\u3066\u3044\u307e\u3059\u3002 \u3068\u3044\u3046\u306e\u3082\u3001\u5b9f\u969b\u306b\u300c\u304a\u304a\u3063\u3001\u3053 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=11412\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Laravel + React \u3067\u691c\u7d22\u30b5\u30b8\u30a7\u30b9\u30c8\u6a5f\u80fd\u3092\u3064\u304f\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":11413,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[247,3,118,249,248],"tags":[],"class_list":["post-11412","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-inertia-js","category-laravel","category-react-js","category-tailwind-css","category-vite"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/11412","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=11412"}],"version-history":[{"count":6,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/11412\/revisions"}],"predecessor-version":[{"id":11424,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/11412\/revisions\/11424"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/11413"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}