{"id":9438,"date":"2021-08-30T07:00:41","date_gmt":"2021-08-29T22:00:41","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=9438"},"modified":"2021-07-29T20:35:48","modified_gmt":"2021-07-29T11:35:48","slug":"%e3%80%90laravel%e3%80%9124%e6%99%82%e9%96%93%e3%81%a7%e5%89%8a%e9%99%a4%e3%81%95%e3%82%8c%e3%82%8b%e3%80%8c%e3%82%b9%e3%83%88%e3%83%bc%e3%83%aa%e3%83%bc%e3%80%8d%e6%a9%9f%e8%83%bd%e3%82%92%e3%81%a4","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=9438","title":{"rendered":"\u3010Laravel\u301124\u6642\u9593\u3067\u524a\u9664\u3055\u308c\u308b\u300c\u30b9\u30c8\u30fc\u30ea\u30fc\u300d\u6a5f\u80fd\u3092\u3064\u304f\u308b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u79c1\u304c2012\u5e74\u306b\u30ab\u30ca\u30c0\u7559\u5b66\u3057\u305f\u3068\u304d\u306b\u521d\u3081\u3066\u4f7f\u3044\u3001\u4eca\u3067\u3082\u306a\u304a\u5229\u7528\u3057\u3066\u3044\u308b\u3082\u306e\u3068\u3044\u3048\u3070\u3001\u300c<strong>LINE<\/strong>\u300d\u3067\u3059\u3002<\/p>\n<p>\u5f53\u6642\u306f\u300c<strong>\u4fbf\u5229\u3060\u3051\u3069\u3001\u307f\u3093\u306a\u304c\u4f7f\u308f\u306a\u304d\u3083\u610f\u5473\u306a\u3044\u3051\u3069\u306d\uff01<\/strong>\u300d\u306a\u3093\u3066\u8a00\u3063\u3066\u307e\u3057\u305f\u304c\u3001\u3059\u3050\u30b7\u30a7\u30a2\u3092\u62e1\u5927\u3057\u4eca\u3067\u306f\u30a4\u30f3\u30d5\u30e9\u306e\u3088\u3046\u306a\u5b58\u5728\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u3055\u3066\u3001\u305d\u3093\u306aLINE\u3067\u3059\u304c\u300c<strong>\u30b9\u30c8\u30fc\u30ea\u30fc<\/strong>\u300d\u3068\u3044\u3046\u6a5f\u80fd\u304c\u3042\u308b\u306e\u3092\u3054\u5b58\u77e5\u3067\u3057\u3087\u3046\u304b\u3002\u7c21\u5358\u306b\u8a00\u3046\u3068\u3001<\/p>\n<p><strong style=\"font-size: 35px;\">24\u6642\u9593\u3067\u524a\u9664\u3055\u308c\u308b\u6295\u7a3f<\/strong><\/p>\n<p>\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u4eca\u65e5\u30ad\u30ec\u30a4\u306a\u666f\u8272\u3092\u5199\u771f\u3067\u6295\u7a3f\u3057\u305f\u3068\u3057\u3066\u3082\u6b21\u306e\u65e5\u306b\u306f\u6d88\u3048\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u300c<strong>\u3086\u308b\u30fc\u304f\u6295\u7a3f\u3057\u305f\u3044<\/strong>\u300d\u3068\u3044\u3046\u4eba\u306b\u306f\u5411\u3044\u3066\u3044\u308b\u6a5f\u80fd\u3068\u3044\u3063\u3066\u3044\u3044\u3093\u3058\u3083\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u4eca\u56de\u306f\u3053\u306e\u300c<strong>\u30b9\u30c8\u30fc\u30ea\u30fc<\/strong>\u300d\u6a5f\u80fd\u3092<code>Laravel<\/code>\u3067\u4f5c\u3063\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u305c\u3072\u4f55\u304b\u306e\u53c2\u8003\u306b\u306a\u308a\u307e\u3057\u305f\u3089\u5b09\u3057\u3044\u3067\u3059\u3002\ud83d\ude0a\u2728<\/p>\n<p>\u203b \u306a\u304a\u3001\u4eca\u56de\u306f\u65b0\u3057\u3044\u8a66\u307f\u3068\u3057\u3066\u3001\u7c21\u5358\u306a\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3082\u5408\u308f\u305b\u3066\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9439 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/07\/no_30.png\" alt=\"\" width=\"224\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/07\/no_30.png 224w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/07\/no_30-210x300.png 210w\" sizes=\"auto, (max-width: 224px) 100vw, 224px\" \/>\u300c\u79c1\u306f\u3044\u3064\u3067\u3082\u898b\u3089\u308c\u308b<br \/>\n\u30bf\u30a4\u30e0\u30e9\u30a4\u30f3\u6d3e\u3067\u3059\u300d<\/p>\n<h1>\u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u4eca\u56de\u30b9\u30c8\u30fc\u30ea\u30fc\u6a5f\u80fd\u3092\u3064\u304f\u308b\u308f\u3051\u3067\u3059\u304c\u3001\u591a\u6a5f\u80fd\u306a<code>LINE<\/code>\u3068\u540c\u3058\u306b\u3059\u308b\u3068\u30b3\u30fc\u30c9\u304c\u3042\u307e\u308a\u306b\u3082\u9577\u304f\u306a\u308a\u3059\u304e\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u4eca\u56de\u306f\u4ee5\u4e0b\u306e\u6a5f\u80fd\u306b\u7d5e\u3063\u3066\u958b\u767a\u3092\u9032\u3081\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u6295\u7a3f\u3057\u3066\u304b\u3089 24\u6642\u9593\u3067\u81ea\u52d5\u3067\u30c7\u30fc\u30bf\u524a\u9664\uff08\u7269\u7406\u524a\u9664\u306f\u3057\u306a\u3044\uff09<\/li>\n<li>\u8ab0\u304c\u95b2\u89a7\u3057\u305f\u304b\u304c\u308f\u304b\u308b\u300c\u8db3\u8de1\u6a5f\u80fd\u300d\u306f\u3064\u3051\u308b<\/li>\n<\/ul>\n<p>\u306a\u304a\u3001\u300c<strong>\u53cb\u9054\u3060\u3051\u516c\u958b<\/strong>\u300d\u306a\u3069\u306f\u4eca\u56de\u5b9f\u88c5\u3057\u307e\u305b\u3093\u304c\u3001\u4ee5\u4e0b\u306e\u8a18\u4e8b\u3092\u5fdc\u7528\u3059\u308c\u3070\u5b9f\u88c5\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p><strong>\ud83d\udcdd \u53c2\u8003\u30da\u30fc\u30b8\uff1a<\/strong>\u00a0<a href=\"https:\/\/blog.capilano-fw.com\/?p=9322\">Laravel \u3067\u300c\u597d\u304d\u306a\u82b8\u4eba\u304c\u4f3c\u3066\u308b\u300d\u30de\u30c3\u30c1\u30f3\u30b0\u30b7\u30b9\u30c6\u30e0\u3092\u3064\u304f\u3063\u3066\u307f\u308b<\/a><\/p>\n<h1>\u524d\u63d0\u3068\u3057\u3066<\/h1>\n<p>\u4eca\u56de\u306f\u8db3\u8de1\u6a5f\u80fd\u3092\u3064\u304f\u308b\u306e\u3067\u3001\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u3066\u3044\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u3082\u3057\u307e\u3060\u306e\u65b9\u306f\u4ee5\u4e0b\u3092\u53c2\u8003\u306b\u3057\u3066\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3092\u6e08\u307e\u305b\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>\ud83d\udcdd \u53c2\u8003\u30da\u30fc\u30b8\uff1a<\/strong>\u00a0<a href=\"https:\/\/blog.capilano-fw.com\/?p=8301\">Laravel Breeze\u3067\u300c\u30b7\u30f3\u30d7\u30eb\u306a\u300d\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/a><\/p>\n<h1>\u30b9\u30c8\u30fc\u30ea\u30fc\u304c\u7ba1\u7406\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b<\/h1>\n<h2>\u30b9\u30c8\u30fc\u30ea\u30fc\u7528\u306e\u30e2\u30c7\u30eb\uff06\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u304f\u308b<\/h2>\n<p>\u3067\u306f\u3001\u307e\u305a\u306f24\u6642\u9593\u3067\u524a\u9664\u3055\u308c\u308b\u300c<strong>\u30b9\u30c8\u30fc\u30ea\u30fc<\/strong>\u300d\u3092\u7ba1\u7406\u3059\u308bDB\uff06\u30e2\u30c7\u30eb\u3092\u3064\u304f\u3063\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\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 Story -m<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3068\u30e2\u30c7\u30eb\u304c\u4e00\u6c17\u306b\uff12\u3064\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u4e2d\u8eab\u3092\u6b21\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>database\/migrations\/****_**_**_******_create_stories_table.php<\/strong><\/p>\n<pre>\/\/ \u7701\u7565\r\n\r\npublic function up()\r\n{\r\n<strong>    Schema::create('stories', function (Blueprint $table) {<\/strong>\r\n<strong>        $table-&gt;id();<\/strong>\r\n<strong>        $table-&gt;unsignedBigInteger('user_id')-&gt;comment('\u30e6\u30fc\u30b6\u30fcID');<\/strong>\r\n<strong>        $table-&gt;text('content')-&gt;comment('\u5185\u5bb9');<\/strong>\r\n<strong>        $table-&gt;dateTime('expired_at')-&gt;comment('\u6709\u52b9\u671f\u9650');<\/strong>\r\n<strong>        $table-&gt;timestamps();<\/strong>\r\n<strong>    });<\/strong>\r\n}\r\n\r\n\/\/ \u7701\u7565<\/pre>\n<p>\u305d\u3057\u3066\u30e2\u30c7\u30eb\u3067\u3059\u3002<\/p>\n<p><strong>app\/Models\/Story.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\n<strong>use App\\Events\\StoryCreating;<\/strong>\r\nuse Illuminate\\Database\\Eloquent\\Builder;\r\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass Story extends Model\r\n{\r\n    use HasFactory;\r\n\r\n    <strong>protected $dispatchesEvents = [<\/strong>\r\n<strong>        'creating' =&gt; StoryCreating::class<\/strong>\r\n<strong>    ];<\/strong>\r\n<strong>    protected $casts = [<\/strong>\r\n<strong>        'expired_at' =&gt; 'datetime'<\/strong>\r\n<strong>    ];<\/strong>\r\n<strong>    protected $appends = [<\/strong>\r\n<strong>        'is_expired'<\/strong>\r\n<strong>    ];<\/strong>\r\n\r\n<strong>    \/\/ Global scope<\/strong>\r\n<strong>    protected static function booted()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        static::addGlobalScope('expiration', function (Builder $builder) {<\/strong>\r\n\r\n<strong>            $builder-&gt;where('expired_at', '&gt;', now());<\/strong>\r\n\r\n<strong>        });<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<h2>\u767b\u9332\u6642\u306e\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u304f\u308b<\/h2>\n<p>\u767b\u9332\u3059\u308b\u3054\u3068\u306b\u6709\u52b9\u671f\u9650\uff08<code>expired_at<\/code>\uff09\u3092\u30bb\u30c3\u30c8\u3057\u3066\u3082\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u3081\u3093\u3069\u3046\u306a\u306e\u3067\u81ea\u52d5\u3067\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u300c<strong>creating \u30a4\u30d9\u30f3\u30c8<\/strong>\u300d\u3092\u4f5c\u3063\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\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 StoryCreating<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u30a4\u30d9\u30f3\u30c8\u30fb\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u3001\u4e2d\u8eab\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/Events\/StoryCreating.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Events;\r\n\r\n<strong>use App\\Models\\Story;<\/strong>\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass StoryCreating\r\n{\r\n    \/\/ \u7701\u7565    \r\n\r\n<strong>    public function __construct(Story $story)<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        $story-&gt;expired_at = now()-&gt;addDay();<\/strong>\r\n<strong>    }<\/strong>\r\n\r\n\/\/ \u7701\u7565<\/pre>\n<p>\u306a\u304a\u3001\u3053\u306e\u30a4\u30d9\u30f3\u30c8\u306f\u3059\u3067\u306b<code>Story<\/code>\u30e2\u30c7\u30eb\u306b\u767b\u9332\u3057\u3066\u3044\u307e\u3059\u3002\uff08<code>$dispatchesEvents<\/code>\u306e\u90e8\u5206\u3067\u3059\uff09<\/p>\n<p>\u3053\u308c\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bexpired_at\u306e\u30bb\u30c3\u30c8\u3092\u7701\u7565\u3057\u3066\u3082\u304d\u3061\u3093\u3068\uff11\u65e5\u5f8c\u306e\u65e5\u6642\u304c\u683c\u7d0d\u3055\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<pre><strong>$story = new Storage();<\/strong>\r\n<strong>$story-&gt;user_id = 1;<\/strong>\r\n<strong>$story-&gt;content = '\u30c6\u30b9\u30c8\u306e\u30b9\u30c8\u30fc\u30ea\u30fc\u3067\u3059\uff01';<\/strong>\r\n\r\n<strong>\/\/ $story-&gt;expired_at \u306f\u7701\u7565\u3057\u3066OK\uff01\u3000<\/strong>\r\n\r\n<strong>$story-&gt;save();<\/strong><\/pre>\n<h2>\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3092\u3064\u304f\u308b<\/h2>\n<p>\u3067\u306f\u3001\u30b9\u30c8\u30fc\u30ea\u30fc\u306e\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3092\u4f5c\u3063\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:test StoryTest --unit<\/strong><\/pre>\n<p>\u3059\u308b\u3068\u3001\u30c6\u30b9\u30c8\u7528\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u4e2d\u8eab\u3092\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p><strong>tests\/Unit\/StoryTest.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace Tests\\Unit;\r\n\r\n<strong>use App\\Models\\Story;<\/strong>\r\n<strong>use Carbon\\Carbon;<\/strong>\r\n<strong>use Tests\\TestCase; \/\/ \ud83d\udc48 \u3053\u3053\u6ce8\u610f\u3067\u3059<\/strong>\r\n\r\nclass StoryTest extends TestCase\r\n{\r\n    <strong>\/\/ \u30c7\u30fc\u30bf\u767b\u9332\u3067\u304d\u308b\u304b\u30c1\u30a7\u30c3\u30af<\/strong>\r\n<strong>    public function test_create()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        $story = new Story();<\/strong>\r\n<strong>        $story-&gt;user_id = 1;<\/strong>\r\n<strong>        $story-&gt;content = '\u30c6\u30b9\u30c8\u306e\u30b9\u30c8\u30fc\u30ea\u30fc\u3067\u3059\uff01';<\/strong>\r\n<strong>        $result = $story-&gt;save();<\/strong>\r\n\r\n<strong>        $this-&gt;assertTrue($result);<\/strong>\r\n<strong>    }<\/strong>\r\n\r\n<strong>    \/\/ expired_at \u304c\u578b\u5909\u63db\u3055\u308c\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af<\/strong>\r\n<strong>    public function test_expired_at_is_carbon_instance()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        $story = Story::latest()-&gt;first();<\/strong>\r\n<strong>        $this-&gt;assertTrue($story-&gt;expired_at instanceof Carbon);<\/strong>\r\n<strong>    }<\/strong>\r\n\r\n<strong>    \/\/ \u6709\u52b9\u671f\u9650\u304c\u5207\u308c\u305f\u3089\u524a\u9664\u3055\u308c\u308b\u304b\uff08\u53d6\u5f97\u3067\u304d\u306a\u3044\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u304b\uff09<\/strong>\r\n<strong>    public function test_is_null_after_expired()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        Carbon::setTestNow(now()-&gt;addDay()); \/\/ \u4eca\u304c\u300c\uff11\u65e5\u5f8c\u300d\u3067\u3042\u308b\u3088\u3046\u306b\u5b9f\u884c\u3055\u305b\u308b<\/strong>\r\n\r\n<strong>        $story = Story::latest()-&gt;first();<\/strong>\r\n<strong>        $this-&gt;assertTrue(is_null($story));<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<p>\u3053\u308c\u3067<code>php artisan test --testsuit=Unit<\/code>\u3092\u5b9f\u884c\u3057\u3066\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u3046\u307e\u304f\u3044\u304f\u3068\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-9444\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/07\/laravel_story_1.png\" alt=\"\" width=\"336\" height=\"143\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/07\/laravel_story_1.png 336w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/07\/laravel_story_1-300x128.png 300w\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" \/><\/p>\n<p>\u6210\u529f\u3067\u3059\u306d\ud83d\ude0a\u2728<\/p>\n<h1>\u8db3\u8de1\u6a5f\u80fd\u3092\u3064\u304f\u308b<\/h1>\n<p>\u3067\u306f\u3001\u7d9a\u3044\u3066\u300c<strong>\u8ab0\u304c\u30b9\u30c8\u30fc\u30ea\u30fc\u3092\u898b\u305f\u304b<\/strong>\u300d\u304c\u5206\u304b\u308b\u300c<strong>\u8db3\u8de1<\/strong>\u300d\u6a5f\u80fd\u3092\u3064\u304f\u3063\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30e2\u30c7\u30eb\uff06\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u304f\u308b<\/h2>\n<p>\u307e\u305a\u306fDB\u5468\u308a\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 StoryAccess -m<\/strong><\/pre>\n<p>\u30e2\u30c7\u30eb\u3068\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\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>database\/migrations\/****_**_**_******_create_story_accesses_table.php<\/strong><\/p>\n<pre>\/\/ \u7701\u7565\r\n\r\npublic function up()\r\n{\r\n<strong>    Schema::create('story_accesses', function (Blueprint $table) {<\/strong>\r\n<strong>        $table-&gt;id();<\/strong>\r\n<strong>        $table-&gt;unsignedBigInteger('user_id')-&gt;comment('\u30e6\u30fc\u30b6\u30fcID');<\/strong>\r\n<strong>        $table-&gt;unsignedBigInteger('story_id')-&gt;comment('\u30b9\u30c8\u30fc\u30ea\u30fcID');<\/strong>\r\n<strong>        $table-&gt;timestamps();<\/strong>\r\n\r\n<strong>        $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users')-&gt;onDelete('cascade');<\/strong>\r\n<strong>        $table-&gt;foreign('story_id')-&gt;references('id')-&gt;on('stories')-&gt;onDelete('cascade');<\/strong>\r\n<strong>        $table-&gt;unique(['user_id', 'story_id']);<\/strong>\r\n<strong>    });<\/strong>\r\n}\r\n\r\n\/\/ \u7701\u7565<\/pre>\n<p>\u305d\u3057\u3066\u3001\u30e2\u30c7\u30eb\u3067\u3059\u3002<\/p>\n<p><strong>app\/Models\/StoryAccess.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass StoryAccess extends Model\r\n{\r\n    use HasFactory;\r\n\r\n    <strong>protected $guarded = ['id']; \/\/ \ud83d\udc48 mass assignment \u5bfe\u7b56\u3067\u8ffd\u52a0\u3057\u307e\u3057\u305f<\/strong>\r\n}<\/pre>\n<h2>\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u304f\u308b<\/h2>\n<p><code>Story<\/code>\u30e2\u30c7\u30eb\u306b\u300c<strong>\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3055\u308c\u305f\u3089<\/strong>\u300d\u5b9f\u884c\u3055\u308c\u308b\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u304f\u308a\u307e\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:event StoryRetrieved<\/strong><\/pre>\n<p>\u30a4\u30d9\u30f3\u30c8\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b\u306e\u3067\u4e2d\u8eab\u3092\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/Events\/StoryRetrieved.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Events;\r\n\r\n<strong>use App\\Models\\Story;<\/strong>\r\n<strong>use App\\Models\\StoryAccess;<\/strong>\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass StoryRetrieved\r\n{\r\n    \/\/ \u7701\u7565\r\n\r\n    <strong>public function __construct(Story $story)<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        if(auth()-&gt;check()) {<\/strong>\r\n\r\n<strong>            $user_id = auth()-&gt;id();<\/strong>\r\n\r\n<strong>            if($user_id !== $story-&gt;user_id) { \/\/ \ud83d\udc48 \u81ea\u5206\u4ee5\u5916\u306a\u3089\u8db3\u8de1\u4fdd\u5b58<\/strong>\r\n\r\n<strong>                $story_id = $story-&gt;id;<\/strong>\r\n<strong>                $story_access = StoryAccess::firstOrNew([<\/strong>\r\n<strong>                    'user_id' =&gt; $user_id,<\/strong>\r\n<strong>                    'story_id' =&gt; $story_id<\/strong>\r\n<strong>                ]);<\/strong>\r\n<strong>                $story_access-&gt;save();<\/strong>\r\n\r\n<strong>            }<\/strong>\r\n\r\n<strong>        }<\/strong>\r\n<strong>    }<\/strong>\r\n\r\n\/\/ \u7701\u7565<\/pre>\n<p>\u305d\u3057\u3066\u3001<code>Story<\/code>\u30e2\u30c7\u30eb\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30bb\u30c3\u30c8\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>app\/Models\/Story.php<\/strong><\/p>\n<pre>\/\/ \u7701\u7565\r\n\r\nclass Story extends Model\r\n{\r\n    use HasFactory;\r\n\r\n    protected $dispatchesEvents = [\r\n        <strong>'retrieved' =&gt; StoryRetrieved::class, \/\/ \ud83d\udc48 \u3053\u3053\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f<\/strong>\r\n        'creating' =&gt; StoryCreating::class\r\n    ];<\/pre>\n<h2>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u304f\u308b<\/h2>\n<p>\u307e\u305f\u3001<code>Story<\/code>\u304b\u3089<code>StoryAccess<\/code>\u3092\u9023\u7d50\u3059\u308b\u300c<strong>\uff11\uff1a\u591a\uff08hasMany\uff09<\/strong>\u300d\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u304f\u3063\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><strong>app\/Models\/Story.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass Story extends Model\r\n{\r\n    \/\/ \u7701\u7565\r\n    \r\n<strong>    \/\/ Relationship<\/strong>\r\n<strong>    public function accesses()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        return $this-&gt;hasMany(StoryAccess::class, 'story_id', 'id');<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<h2>\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3092\u3064\u304f\u308b<\/h2>\n<p>\u3067\u306f\u3001\u300c<strong>\u8db3\u8de1<\/strong>\u300d\u6a5f\u80fd\u306b\u3082\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n\u5148\u307b\u3069\u306e<code>StoryTest<\/code>\u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<p><strong>tests\/Unit\/StoryTest.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace Tests\\Unit;\r\n\r\nuse App\\Models\\Story;\r\nuse Carbon\\Carbon;\r\nuse Tests\\TestCase;\r\n\r\nclass StoryTest extends TestCase\r\n{\r\n<strong>    protected function setUp(): void<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        parent::setUp();<\/strong>\r\n\r\n<strong>        auth()-&gt;loginUsingId(2); \/\/ \ud83d\udc48 ID:2 \u306e\u30e6\u30fc\u30b6\u30fc\u3067\u30ed\u30b0\u30a4\u30f3<\/strong>\r\n<strong>    }<\/strong>\r\n\r\n    \/\/ \u7701\u7565\r\n\r\n<strong>    \/\/ Story \u306b\u30a2\u30af\u30bb\u30b9\u3057\u305f\u60c5\u5831\u304c\u4fdd\u5b58\u3055\u308c\u308b\u304b\u3069\u3046\u304b<\/strong>\r\n<strong>    public function test_can_save_history()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        $story = Story::with('accesses')-&gt;latest()-&gt;first();<\/strong>\r\n<strong>        $this-&gt;assertNotEmpty($story-&gt;accesses);<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<p>\u306a\u304a\u3001\u3053\u3053\u3067\u91cd\u8981\u306a\u306e\u306f<code>setup()<\/code>\u306e\u90e8\u5206\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u4e2d\u3067\u306f\u3001\u30e6\u30fc\u30b6\u30fcID\u304c<code>2<\/code>\u3067\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u308c\u306f<code>setup()<\/code>\u306e\u4e2d\u306b\u66f8\u304b\u306a\u3044\u3068<strong>\u30c6\u30b9\u30c8\u4e2d\u306b\u30ed\u30b0\u30a4\u30f3\u304c\u6709\u52b9\u306b\u306a\u308a\u307e\u305b\u3093\u3002<\/strong><\/p>\n<p>\u307e\u305f\u3001\u306a\u305cID<code>2<\/code>\u3092\u4f7f\u3063\u3066\u3044\u308b\u304b\u3068\u3044\u3046\u3068\u3001\u300c<strong>\u81ea\u5206\u4ee5\u5916\u306e\u30a2\u30af\u30bb\u30b9\u306f\u5c65\u6b74\uff08\u8db3\u8de1\uff09\u3092\u6b8b\u3059<\/strong>\u300d\u3068\u3044\u3046\u4ed5\u69d8\u306b\u3057\u3066\u3044\u308b\u305f\u3081\u3001<code>1<\/code>\u3060\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001<code>php artisan test --testsuit=Unit<\/code> \u3092\u5b9f\u884c\u3057\u3066\u30c6\u30b9\u30c8\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-9446\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/07\/laravel_story_2.png\" alt=\"\" width=\"351\" height=\"167\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/07\/laravel_story_2.png 351w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2021\/07\/laravel_story_2-300x143.png 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/p>\n<p>\u3053\u308c\u3082OK\u3067\u3059\u306d\ud83d\ude0a\u2728<\/p>\n<h1>\u30c6\u30b9\u30c8\u3057\u3066\u307f\u308b<\/h1>\n<p>\u4eca\u56de\u306f\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3067\u5b9f\u884c\u3057\u305f\u306e\u3067\u30c6\u30b9\u30c8\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u4f7f\u3044\u65b9\u3068\u3057\u3066\u306f<code>Story<\/code>\u30e2\u30c7\u30eb\u306b\u30c7\u30fc\u30bf\u8ffd\u52a0\u3059\u308b\u3001\u305d\u3057\u3066\u3001\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3044\u3046\u30b3\u30fc\u30c9\u304c\u3042\u308c\u3070\u30b9\u30c8\u30fc\u30ea\u30fc\u6a5f\u80fd\u306f\u6709\u52b9\u306b\u306a\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h1>\u4f01\u696d\u69d8\u3078\u306e\u3054\u63d0\u6848<\/h1>\n<p>\u4eca\u56de\u306e\u30b9\u30c8\u30fc\u30ea\u30fc\u6a5f\u80fd\u3092\u4f7f\u3048\u3070\u3001\u300c<strong>\u671f\u9593\u9650\u5b9a\u30b3\u30f3\u30c6\u30f3\u30c4<\/strong>\u300d\u3092\u767b\u9332\u30e6\u30fc\u30b6\u30fc\u306b\u3064\u304f\u3063\u3066\u3082\u3046\u3053\u3068\u304c\u3067\u304d\u308b\u305f\u3081\u3001\u3088\u308a\u591a\u304f\u306e\u30e6\u30fc\u30b6\u30fc\u304c\u5b9a\u671f\u7684\u306b\u8a2a\u554f\u3059\u308b\u30a4\u30f3\u30bb\u30f3\u30c6\u30a3\u30d6\u306b\u3064\u306a\u304c\u308a\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u6295\u7a3f\u3059\u308b\u5074\u304b\u3089\u3057\u3066\u3082\u300c<strong>24\u6642\u9593\u3060\u304b\u3089\u6295\u7a3f\u3057\u3066\u3082\u3044\u3044\u304b\u306a<\/strong>\u300d\u3068\u610f\u6b32\u304c\u591a\u304f\u306a\u308b\u306e\u3082\u4e8b\u5b9f\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u4eca\u56de\u306e\u3088\u3046\u306a\u30b9\u30c8\u30fc\u30ea\u30fc\u6a5f\u80fd\u3092\u3054\u5e0c\u671b\u306e\u5834\u5408\u306f\u304a\u554f\u3044\u5408\u308f\u305b\u304b\u3089\u3054\u9023\u7d61\u304f\u3060\u3055\u3044\u3002\u3069\u3046\u305e\u3088\u308d\u3057\u304f\u304a\u9858\u3044\u3044\u305f\u3057\u307e\u3059\u3002m(_ _)m<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u4eca\u56de\u306f\u300c<strong>\u30b9\u30c8\u30fc\u30ea\u30fc<\/strong>\u300d\u6a5f\u80fd\u3092<code>Laravel<\/code>\u3067\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002\u671f\u9593\u9650\u5b9a\u306e\u30c7\u30fc\u30bf\u3092\u3064\u304f\u308b\u306e\u306f\u305d\u308c\u307b\u3069\u96e3\u3057\u3044\u3082\u306e\u3067\u306f\u306a\u304b\u3063\u305f\u3093\u3058\u3083\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002\uff08<code>Laravel<\/code>\u30b5\u30de\u30b5\u30de\u3067\u3059\ud83d\udc4d\uff09<\/p>\n<p>\u307e\u305f\u3001\u4eca\u56de\u306f\u3044\u3064\u3082\u3068\u9055\u3063\u3066\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3092\u8a18\u4e8b\u306b\u5165\u308c\u3066\u307f\u307e\u3057\u305f\u304c\u3001\u3044\u304b\u304c\u3060\u3063\u305f\u3067\u3057\u3087\u3046\u304b\u3002\uff08\u30c6\u30b9\u30c8\u3092\u901a\u904e\u3057\u305f\u3068\u304d\u306e\u898b\u305f\u76ee\u3001\u304b\u3063\u3053\u3044\u3044\u3067\u3059\u306d\ud83d\udc4d\uff09<\/p>\n<p>\u3053\u3046\u3044\u3063\u305f\u30c6\u30b9\u30c8\u304c\u6a19\u6e96\u642d\u8f09\u3055\u308c\u3066\u3044\u308b\u306e\u3082<code>Laravel<\/code>\u306e\u4eba\u6c17\u306e\u3072\u3068\u3064\u306a\u3093\u3060\u308d\u3046\u306a\u3068\u5b9f\u611f\u3059\u308b\u4eca\u65e5\u3053\u306e\u9803\u3067\u3059\u3002<\/p>\n<p>\u305c\u3072\u307f\u306a\u3055\u3093\u3082\u8a66\u3057\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-2173\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/11\/no_24.png\" alt=\"\" width=\"300\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/11\/no_24.png 300w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/11\/no_24-281x300.png 281w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><br \/>\n\u300cLaravel 9 \u306f 2022\u5e741\u6708<br \/>\n\u30ea\u30ea\u30fc\u30b9\u306b\u5909\u66f4\u306b\u306a\u308a\u307e\u3057\u305f\u300d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u79c1\u304c2012\u5e74\u306b\u30ab\u30ca\u30c0\u7559\u5b66\u3057\u305f\u3068\u304d\u306b\u521d\u3081\u3066\u4f7f\u3044\u3001\u4eca\u3067\u3082\u306a\u304a\u5229\u7528\u3057\u3066\u3044\u308b\u3082\u306e\u3068\u3044\u3048\u3070\u3001\u300cLINE\u300d\u3067\u3059\u3002 \u5f53\u6642\u306f\u300c\u4fbf\u5229\u3060\u3051\u3069\u3001\u307f\u3093\u306a\u304c\u4f7f\u308f\u306a\u304d\u3083\u610f\u5473\u306a\u3044 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=9438\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u3010Laravel\u301124\u6642\u9593\u3067\u524a\u9664\u3055\u308c\u308b\u300c\u30b9\u30c8\u30fc\u30ea\u30fc\u300d\u6a5f\u80fd\u3092\u3064\u304f\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":9449,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-9438","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/9438","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=9438"}],"version-history":[{"count":5,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/9438\/revisions"}],"predecessor-version":[{"id":9448,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/9438\/revisions\/9448"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/9449"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}