{"id":300915,"date":"2020-03-29T15:00:13","date_gmt":"2020-03-29T15:00:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=300915"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=300915","title":{"rendered":"\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u0440\u0435\u0439\u0442\u044b \u0434\u043b\u044f \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u044b\u0445 \u0440\u0435\u043b\u0435\u0439\u0448\u0435\u043d\u043e\u0432 \u0432 Laravel"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/494658\/\">\n<h4 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h4>\n<p>  <\/p>\n<p>\u041f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u043c \u043f\u0440\u0435\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0442\u0440\u0435\u0439\u0442\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u044b\u043c\u0438 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f\u043c\u0438 \u0432 Laravel. <\/p>\n<p>  <\/p>\n<p><strong>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438:<\/strong><\/p>\n<p>  <\/p>\n<ol>\n<li><a href=\"#start\">\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438<\/a><\/li>\n<li><a href=\"#app\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/a><\/li>\n<li><a href=\"#database\">\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0411\u0414<\/a><\/li>\n<li><a href=\"#entities\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439<\/a><\/li>\n<li><a href=\"#traits\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0440\u0435\u0439\u0442\u0430<\/a><\/li>\n<li><a href=\"#tests\">\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432<\/a><\/li>\n<\/ol>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h4 id=\"anchorstartanchoropisanie-predmetnoy-oblasti\"><a name=\"start\"><\/a>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438<\/h4>\n<p>  <\/p>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0437\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u043a\u0438\u0435 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0438 \u0438 \u043d\u0435\u043a\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u044b \u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0443. \u0421\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0438, \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u044b: \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432, \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u044b \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0438 \u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b. \u041c\u0435\u0436\u0434\u0443 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439 \u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u043c \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 many-to-many (\u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0447\u0442\u043e \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a \u043c\u043e\u0436\u0435\u0442 \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u0445), many-to-many \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u043c\u0438 \u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c\u0438, many-to-many \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438 \u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u043c\u0438. \u0414\u043b\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u044f \u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439 \u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430\u043c\u0438, \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u043c\u0441\u044f \u043d\u0430 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0438 \u043a\u043e\u043c\u0430\u043d\u0434 \u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432 \u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0443.<\/p>\n<p>  <\/p>\n<h4 id=\"anchorappanchorsozdanie-prilozheniya\"><a name=\"app\"><\/a>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/h4>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 Laravel \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"https:\/\/laravel.com\/docs\/7.x#installing-laravel\" rel=\"nofollow\">\u043f\u0430\u043a\u0435\u0442-\u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439<\/a>. \u041f\u043e\u0441\u043b\u0435 \u0435\u0433\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0443\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u0443 \u043a\u043e\u043c\u0430\u043d\u0434\u0443: <\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">laravel new system<\/code><\/pre>\n<p>  <\/p>\n<h4 id=\"anchordatabaseanchorvozmozhnye-struktury-bd\"><a name=\"database\"><\/a>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0411\u0414<\/h4>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0438\u0434\u0442\u0438 \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u043f\u0443\u0442\u0435\u043c, \u0442\u043e \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0442\u0440\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0438 \u0435\u0449\u0451 \u0442\u0440\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u0441\u0432\u044f\u0437\u0435\u0439: \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0438-\u043a\u043e\u043c\u0430\u043d\u0434\u044b, \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0438-\u043f\u0440\u043e\u0435\u043a\u0442\u044b, \u043a\u043e\u043c\u0430\u043d\u0434\u044b-\u043f\u0440\u043e\u0435\u043a\u0442\u044b.<br \/>  \u0415\u0441\u043b\u0438 \u0441\u043d\u0438\u0437\u0438\u0442\u044c \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u043b\u044f \u0441\u0432\u044f\u0437\u0435\u0439 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a-\u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430-\u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u043e\u0434\u043d\u0443, \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u044f \u0442\u0438\u043f \u0441\u0432\u044f\u0437\u0438 \u043f\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044e \u0441 \u0442\u0438\u043f\u043e\u043c (\u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c, 1 \u2014 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a, 2 \u2014 \u043a\u043e\u043c\u0430\u043d\u0434\u0430).<br \/>  \u0418\u0434\u0435\u044f <a href=\"https:\/\/laravel.com\/docs\/7.x\/eloquent-relationships#polymorphic-relationships\" rel=\"nofollow\">\u043c\u043e\u0440\u0444-\u0441\u0432\u044f\u0437\u0435\u0439<\/a> \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u043c\u0435\u043d\u0435\u0435 \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0441 \u0442\u0438\u043f\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0432\u0430 \u2014 \u043e\u0434\u0438\u043d \u0434\u043b\u044f \u0438\u043c\u0435\u043d\u0438 \u043a\u043b\u0430\u0441\u0441\u0430 \u043c\u043e\u0434\u0435\u043b\u0438, \u0432\u0442\u043e\u0440\u043e\u0439 \u0434\u043b\u044f \u0435\u0451 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430. <\/p>\n<p>  <\/p>\n<h4 id=\"anchorentitiesanchorsozdanie-suschnostey\"><a name=\"entities\"><\/a>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439<\/h4>\n<p>  <\/p>\n<p>\u041d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f \u043c\u043e\u0434\u0435\u043b\u0438, \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0438 \u0444\u0430\u0431\u0440\u0438\u043a\u0438 \u0434\u043b\u044f \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432, \u043a\u043e\u043c\u0430\u043d\u0434, \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u0438 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u0439. \u041a\u043e\u043c\u0430\u043d\u0434\u044b \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0432\u0441\u0435\u0433\u043e \u044d\u0442\u043e\u0433\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">php artisan make:model Employee -f \/\/ \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430 php artisan make:model Team -f \/\/ \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u044b php artisan make:model Project -f \/\/ \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 php artisan make:migration CreateEntitiesTables \/\/ \u043e\u0431\u0449\u0430\u044f \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 php artisan make:model Attach -m \/\/ \u043c\u043e\u0434\u0435\u043b\u044c \u0438 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u044f<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0444\u0430\u0439\u043b\u044b \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0432 App\/, \u0444\u0430\u0439\u043b\u044b \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 \u0432 \u043f\u0430\u043f\u043a\u0435 database\/migrations\/ \u0438 \u0444\u0430\u0431\u0440\u0438\u043a\u0438 \u0432 database\/factories\/. <\/p>\n<p>  <\/p>\n<p><strong>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439<\/strong>. \u0412\u043e \u0432\u0441\u0435\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0435\u0439, \u043d\u043e \u043c\u044b \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u043f\u043e \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0443: \u0443 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430, \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u043c\u044f. \u041f\u043e\u0437\u0432\u043e\u043b\u044e \u0441\u0435\u0431\u0435 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 \u0434\u043e \u0434\u0432\u0443\u0445 \u2014 \u0434\u043b\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u043e\u0433\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f. <\/p>\n<p>  <\/p>\n<p><strong>\u041c\u0438\u0433\u0440\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  use Illuminate\\Database\\Migrations\\Migration; use Illuminate\\Database\\Schema\\Blueprint; use Illuminate\\Support\\Facades\\Schema;  class CreateEntitesTables extends Migration {     \/**      * Run the migrations.      *      * @return void      *\/     public function up()     {         Schema::create('employees', function (Blueprint $table) {             $table-&gt;id();             $table-&gt;string('name');             $table-&gt;timestamps();         });          Schema::create('teams', function (Blueprint $table) {             $table-&gt;id();             $table-&gt;string('name');             $table-&gt;timestamps();         });          Schema::create('projects', function (Blueprint $table) {             $table-&gt;id();             $table-&gt;string('name');             $table-&gt;timestamps();         });     }      \/**      * Reverse the migrations.      *      * @return void      *\/     public function down()     {         Schema::dropIfExists('employees');         Schema::dropIfExists('teams');         Schema::dropIfExists('projects');     } }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u0414\u043b\u044f \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u043e\u0433\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  use Illuminate\\Database\\Migrations\\Migration; use Illuminate\\Database\\Schema\\Blueprint; use Illuminate\\Support\\Facades\\Schema;  class CreateAttachesTable extends Migration {     \/**      * Run the migrations.      *      * @return void      *\/     public function up()     {         Schema::create('attachments', function (Blueprint $table) {             $table-&gt;id();             $table-&gt;morphs('attachable');             $table-&gt;unsignedInteger('project_id');             $table-&gt;timestamps();              $table-&gt;foreign('project_id')-&gt;references('id')-&gt;on('projects')                 -&gt;onDelete('cascade');         });     }      \/**      * Reverse the migrations.      *      * @return void      *\/     public function down()     {         Schema::dropIfExists('attachments');     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u043b\u0435\u0439 \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u043e\u0433\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e morphs().<\/p>\n<p>  <\/p>\n<p><strong>\u0422\u0435\u043f\u0435\u0440\u044c \u043a \u043c\u043e\u0434\u0435\u043b\u044f\u043c<\/strong>.<br \/>  \u041c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430: <\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  namespace App;  use Illuminate\\Database\\Eloquent\\Model;  class Employee extends Model {     protected $fillable = ['name']; }<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  namespace App;  use Illuminate\\Database\\Eloquent\\Model;  class Team extends Model {     protected $fillable = ['name']; }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u041c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  namespace App;  use Illuminate\\Database\\Eloquent\\Model; use Illuminate\\Database\\Eloquent\\Relations\\HasMany; use Illuminate\\Database\\Eloquent\\Relations\\MorphToMany;  class Project extends Model {     protected $fillable = ['name'];      \/**      * Relation for project attachments      * @return HasMany      *\/     public function attachments()     {         return $this-&gt;hasMany(Attach::class);     }      \/**      * Relation for project employees      * @return MorphToMany      *\/     public function employees()     {         return $this-&gt;morphedByMany(Employee::class, 'attachable', 'attachments');     }      \/**      * Relation for project teams      * @return MorphToMany      *\/     public function teams()     {         return $this-&gt;morphedByMany(Team::class, 'attachable', 'attachments');     }     }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u041f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u0435<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  namespace App;  use Illuminate\\Database\\Eloquent\\Model;  class Attach extends Model {     protected $table = 'attachments';     protected $fillable = ['attachable_id', 'attachable_type', 'project_id']; }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u0424\u0430\u0431\u0440\u0438\u043a\u0438<\/strong> \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u044b \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  \/** @var \\Illuminate\\Database\\Eloquent\\Factory $factory *\/  use Faker\\Generator as Faker;  $factory-&gt;define(\/* (\u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\/\u043a\u043e\u043c\u0430\u043d\u0434\u0430\/\u043f\u0440\u043e\u0435\u043a\u0442) *\/, function (Faker $faker) {     return [         'name' =&gt; $faker-&gt;colorName     ]; });<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0433\u043e\u0442\u043e\u0432\u044b, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u0442\u0440\u0435\u0439\u0442\u0443. <\/p>\n<p>  <\/p>\n<h4 id=\"anchortraitsanchorispolzovanie-treyta\"><a name=\"traits\"><\/a>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0440\u0435\u0439\u0442\u0430<\/h4>\n<p>  <\/p>\n<p>\u041f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u044b\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u0432 Laravel \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u0433\u043b\u0430\u0432\u043d\u044b\u0445 \u0438 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044f\u0435\u043c\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u2014 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0438\u043f \u0441\u0432\u044f\u0437\u0438 morphedByMany(), \u0430 \u0432 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u0445 \u2014 morphToMany(). \u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044f\u0435\u043c\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0440\u0435\u043b\u0435\u0439\u0448\u0435\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432 \u0442\u0440\u0435\u0439\u0442 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430 \u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b.<br \/>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u0443\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044e app\/Traits \u0438 \u0442\u0440\u0435\u0439\u0442 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0435\u043b\u0435\u0439\u0448\u0435\u043d\u0430: Attachable.php<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\Traits;  use App\\Project; use Illuminate\\Database\\Eloquent\\Relations\\MorphToMany;  trait Attachable {     \/**      * Relation for entity attachments      * @return MorphToMany      *\/     public function attachments()     {         return $this-&gt;morphToMany(Project::class, 'attachable', 'attachments');     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0442\u0440\u0435\u0439\u0442 \u0432 \u043c\u043e\u0434\u0435\u043b\u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u0430 \u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0447\u0435\u0440\u0435\u0437 use.<\/p>\n<p>  <\/p>\n<pre><code class=\"php\">...     use Attachable; ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0435 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0435\u0441\u0442\u043e\u0432.<\/p>\n<p>  <\/p>\n<h4 id=\"anchortestsanchornapisanie-testov\"><a name=\"tests\"><\/a>\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432<\/h4>\n<p>  <\/p>\n<p><a href=\"https:\/\/laravel.com\/docs\/7.x\/testing\" rel=\"nofollow\">\u041f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0443<\/a>, Laravel \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 PHPUnit \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0435\u0441\u0442\u044b \u0434\u043b\u044f \u0440\u0435\u043b\u0435\u0439\u0448\u0435\u043d\u0430: <\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">php artisan make:test AttachableTest<\/code><\/pre>\n<p>  <\/p>\n<p>\u0424\u0430\u0439\u043b \u0442\u0435\u0441\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 tests\/Feature\/. \u0414\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0411\u0414 \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0442\u0435\u0441\u0442\u043e\u0432 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0435\u0439\u0442 RefreshDatabase. <\/p>\n<p>  <\/p>\n<p><strong>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u043c\u043e\u0440\u0444\u0430 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0438 \u0442\u0440\u0435\u0439\u0442\u0430 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438 \u0441\u043e\u0442\u0440\u0443\u0434\u043d\u0438\u043a\u043e\u0432<\/strong><\/p>\n<p>  <\/p>\n<pre><code class=\"php\">&lt;?php  namespace Tests\\Feature;  use App\\Team; use App\\Employee; use App\\Project; use Illuminate\\Foundation\\Testing\\RefreshDatabase; use Tests\\TestCase;  class OrderTest extends TestCase {     use RefreshDatabase;      \/** @test *\/     public function polymorphic_relations_scheme(): void     {         \/\/ Given project         $project = factory(Project::class)-&gt;create();         \/\/ Given team         $team = factory(Team::class)-&gt;create();         \/\/ Given employee         $employee = factory(Employee::class)-&gt;create();          \/\/ When we add team and employee to project         $project-&gt;teams()-&gt;save($team);         $project-&gt;employees()-&gt;save($employee);          \/\/ Then project should have two attachments         $this-&gt;assertCount(2, $project-&gt;attachments);         $this-&gt;assertCount(1, $project-&gt;teams);         $this-&gt;assertCount(1, $project-&gt;employees);         $this-&gt;assertEquals($team-&gt;id, $project-&gt;teams-&gt;first()-&gt;id);         $this-&gt;assertEquals($employee-&gt;id, $project-&gt;employees-&gt;first()-&gt;id);         \/\/ Team and employee should have attachment to project         $this-&gt;assertCount(1, $team-&gt;attachments);         $this-&gt;assertCount(1, $employee-&gt;attachments);         $this-&gt;assertEquals($project-&gt;id, $team-&gt;attachments-&gt;first()-&gt;id);         $this-&gt;assertEquals($project-&gt;id, $employee-&gt;attachments-&gt;first()-&gt;id);     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u0441\u0442 \u043f\u0440\u043e\u0448\u0435\u043b! <\/p>\n<p>  <\/p>\n<p>\u0422\u0440\u0435\u0439\u0442\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043d\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u044b\u0445 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0435\u0439, \u0442\u0430\u043a\u0436\u0435 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u043f\u043e\u043b\u044f \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0430\u0432\u0442\u043e\u0440 \u0437\u0430\u043f\u0438\u0441\u0438) \u2014 \u0442\u0443\u0442 \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0440\u0435\u0439\u0442 \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0440\u0435\u043b\u0435\u0439\u0448\u0435\u043d\u0430.<\/p>\n<p>  <\/p>\n<p>\u0411\u0443\u0434\u0443 \u0440\u0430\u0434 \u0441\u043b\u044b\u0448\u0430\u0442\u044c \u0432\u0430\u0448\u0438 \u043a\u0435\u0439\u0441\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0442\u0440\u0435\u0439\u0442\u043e\u0432 \u0432 Laravel \u0438 PHP.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/494658\/\"> https:\/\/habr.com\/ru\/post\/494658\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/494658\/\">\n<h4 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h4>\n<p>  <\/p>\n<p>\u041f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u043c \u043f\u0440\u0435\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0442\u0440\u0435\u0439\u0442\u043e\u0432 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u043d\u044b\u043c\u0438 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f\u043c\u0438 \u0432 Laravel. <\/p>\n<p>  <\/p>\n<p><strong>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438:<\/strong><\/p>\n<p>  <\/p>\n<ol>\n<li><a href=\"#start\">\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438<\/a><\/li>\n<li><a href=\"#app\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/a><\/li>\n<li><a href=\"#database\">\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0411\u0414<\/a><\/li>\n<li><a href=\"#entities\">\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439<\/a><\/li>\n<li><a href=\"#traits\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0440\u0435\u0439\u0442\u0430<\/a><\/li>\n<li><a href=\"#tests\">\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u043e\u0432<\/a><\/li>\n<\/ol>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-300915","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/300915","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=300915"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/300915\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300915"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300915"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300915"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}