{"id":13,"date":"2024-09-28T19:58:55","date_gmt":"2024-09-28T12:58:55","guid":{"rendered":"https:\/\/megadump.ru\/?p=13"},"modified":"2024-12-22T15:45:23","modified_gmt":"2024-12-22T08:45:23","slug":"best-practies-laravel","status":"publish","type":"post","link":"https:\/\/phpha.ru\/?p=13","title":{"rendered":"5 \u0441\u043e\u0432\u0435\u0442\u043e\u0432 \u043f\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u043a\u043e\u0434\u0430 Laravel"},"content":{"rendered":"\n<p>\u041c\u044b \u0441\u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438 5 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u043e\u0432\u0435\u0442\u043e\u0432 \u043f\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u043a\u043e\u0434\u0430 \u0432 Laravel, \u0441\u043b\u0435\u0434\u0443\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u0438 \u0435\u0433\u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e. \u0414\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0432\u0435\u0442\u044b \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u043f\u043e\u0434 \u043b\u044e\u0431\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e Laravel.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">1. \u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0435\u0434\u0438\u043d\u043e\u0439 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438<\/h2>\n\n\n\n<p>\u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0435\u0434\u0438\u043d\u043e\u0439 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0438\u043b\u0438 <strong>Single responsibility principle<\/strong>, \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0443 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c. \u041d\u0435 \u043d\u0443\u0436\u043d\u043e \u0432 \u043e\u0434\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u0432\u0435\u0449\u0435\u0439, \u0441\u0442\u0430\u0440\u0430\u0439\u0442\u0435\u0441\u044c \u0447\u0442\u043e\u0431\u044b \u043e\u0434\u0438\u043d \u043a\u043b\u0430\u0441\u0441 \u0434\u0435\u043b\u0430\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e \u0434\u0435\u043b\u043e.<\/p>\n\n\n\n<p><strong>\u041f\u043b\u043e\u0445\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function update(Request $request): string\n{\n    $validated = $request->validate([\n        'title' => 'required|max:255',\n        'events' => 'required|array:date,type'\n    ]);\n\n    foreach ($request->events as $event) {\n        $date = $this->carbon->parse($event['date'])->toString();\n\n        $this->logger->log('Update event ' . $date . ' :: ' . $);\n    }\n\n    $this->event->updateGeneralEvent($request->validated());\n\n    return back();\n}<\/code><\/pre>\n\n\n\n<p><strong>\u0425\u043e\u0440\u043e\u0448\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function update(UpdateRequest $request): string\n{\n    $this->logService->logEvents($request->events);\n\n    $this->event->updateGeneralEvent($request->validated());\n\n    return back();\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u041c\u0435\u0442\u043e\u0434\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u043e\u0434\u043d\u043e<a href=\"https:\/\/github.com\/alexeymezenin\/laravel-best-practices\/blob\/master\/russian.md#%D0%BC%D0%B5%D1%82%D0%BE%D0%B4%D1%8B-%D0%B4%D0%BE%D0%BB%D0%B6%D0%BD%D1%8B-%D0%B4%D0%B5%D0%BB%D0%B0%D1%82%D1%8C-%D1%87%D1%82%D0%BE-%D1%82%D0%BE-%D0%BE%D0%B4%D0%BD%D0%BE\"><\/a><\/h2>\n\n\n\n<p>\u0421\u0442\u0430\u0440\u0430\u0439\u0442\u0435\u0441\u044c \u0447\u0442\u043e\u0431\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0438\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u043a\u043b\u0430\u0441\u0441\u0430 \u0434\u0435\u043b\u0430\u043b \u0447\u0442\u043e-\u0442\u043e \u043e\u0434\u043d\u043e \u0438 \u0434\u0435\u043b\u0430\u043b \u044d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u043e.<\/p>\n\n\n\n<p><strong>\u041f\u043b\u043e\u0445\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function getFullNameAttribute(): string\n{\n    if (auth()->user() &amp;&amp; auth()->user()->hasRole('client') &amp;&amp; auth()->user()->isVerified()) {\n        return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;\n    } else {\n        return $this->first_name[0] . '. ' . $this->last_name;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u0425\u043e\u0440\u043e\u0448\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function getFullNameAttribute(): string\n{\n    return $this->isVerifiedClient() ? $this->getFullNameLong() : $this->getFullNameShort();\n}\n\npublic function isVerifiedClient(): bool\n{\n    return auth()->user() &amp;&amp; auth()->user()->hasRole('client') &amp;&amp; auth()->user()->isVerified();\n}\n\npublic function getFullNameLong(): string\n{\n    return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;\n}\n\npublic function getFullNameShort(): string\n{\n    return $this->first_name[0] . '. ' . $this->last_name;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u041f\u0438\u0448\u0438\u0442\u0435 \u0442\u043e\u043d\u043a\u0438\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u0438 \u0442\u043e\u043b\u0441\u0442\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438<\/h2>\n\n\n\n<p>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0438 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043a\u0430\u043a\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u043e\u043d \u0440\u0435\u0448\u0430\u0435\u0442. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e, \u0442\u043e \u0432\u044b\u043d\u043e\u0441\u0438\u0442\u0435 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432 \u043c\u043e\u0434\u0435\u043b\u0438 (\u0435\u0441\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445). <\/p>\n\n\n\n<p><strong>\u041f\u043b\u043e\u0445\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function index()\n{\n    $clients = Client::verified()\n        ->with(['orders' => function ($q) {\n            $q->where('created_at', '>', Carbon::today()->subWeek());\n        }])\n        ->get();\n\n    return view('index', ['clients' => $clients]);\n}<\/code><\/pre>\n\n\n\n<p><strong>\u0425\u043e\u0440\u043e\u0448\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function index()\n{\n    return view('index', ['clients' => $this->client->getWithNewOrders()]);\n}\n\nclass Client extends Model\n{\n    public function getWithNewOrders(): Collection\n    {\n        return $this->verified()\n            ->with(['orders' => function ($q) {\n                $q->where('created_at', '>', Carbon::today()->subWeek());\n            }])\n            ->get();\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f<a href=\"https:\/\/github.com\/alexeymezenin\/laravel-best-practices\/blob\/master\/russian.md#%D0%B2%D0%B0%D0%BB%D0%B8%D0%B4%D0%B0%D1%86%D0%B8%D1%8F\"><\/a><\/h2>\n\n\n\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043a \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0443 \u0441\u0432\u043e\u0438 Request \u043a\u043b\u0430\u0441\u0441\u044b, \u0441\u043b\u0435\u0434\u0443\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0430\u043c \u0442\u043e\u043d\u043a\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0438 SRP (single responsibilite principle &#8212; \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u0435\u0434\u0438\u043d\u043e\u0439 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438), \u0432\u044b\u043d\u043e\u0441\u0438\u0442\u0435 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e \u0438\u0437 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0432 Request \u043a\u043b\u0430\u0441\u0441\u044b.<\/p>\n\n\n\n<p><strong>\u041f\u043b\u043e\u0445\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function store(Request $request)\n{\n    $request->validate([\n        'title' => 'required|unique:posts|max:255',\n        'body' => 'required',\n        'publish_at' => 'nullable|date',\n    ]);\n\n    \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p><strong>\u0425\u043e\u0440\u043e\u0448\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function store(PostRequest $request)\n{\n    \/\/ ...\n}\n\nclass PostRequest extends Request\n{\n    public function rules(): array\n    {\n        return [\n            'title' => 'required|unique:posts|max:255',\n            'body' => 'required',\n            'publish_at' => 'nullable|date',\n        ];\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u0425\u0440\u0430\u043d\u0438\u0442\u0435 \u0432\u0441\u044e \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445<a href=\"https:\/\/github.com\/alexeymezenin\/laravel-best-practices\/blob\/master\/russian.md#%D0%B1%D0%B8%D0%B7%D0%BD%D0%B5%D1%81-%D0%BB%D0%BE%D0%B3%D0%B8%D0%BA%D0%B0-%D0%B2-%D1%81%D0%B5%D1%80%D0%B2%D0%B8%D1%81-%D0%BA%D0%BB%D0%B0%D1%81%D1%81%D0%B0%D1%85\"><\/a><\/h2>\n\n\n\n<p>\u0417\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0432\u043e\u0438 \u043f\u0440\u044f\u043c\u044b\u0435 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u043d\u043e\u0441\u0438\u0442\u0435 \u0432\u0441\u044e \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0443 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b (\u0438\u0445 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u0441\u0435\u0440\u0432\u0438\u0441\u044b).<\/p>\n\n\n\n<p><strong>\u041f\u043b\u043e\u0445\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function store(Request $request)\n{\n    if ($request->hasFile('image')) {\n        $request->file('image')->move(public_path('images') . 'temp');\n    }\n    \n    \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p><strong>\u0425\u043e\u0440\u043e\u0448\u043e:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">public function store(Request $request)\n{\n    $this->articleService->handleUploadedImage($request->file('image'));\n\n    \/\/ ...\n}\n\nclass ArticleService\n{\n    public function handleUploadedImage($image): void\n    {\n        if (!is_null($image)) {\n            $image->move(public_path('images') . 'temp');\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0435 5 \u0441\u043e\u0432\u0435\u0442\u043e\u0432 \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u0438 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u0434\u0430. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0435\u0449\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0435\u043c \u0434\u0440\u0443\u0433\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0432\u0435\u0449\u0438 \u043f\u043e Laravel, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0434\u0435\u043b\u0430\u044e\u0442 \u0432\u0430\u0448\u0443 \u0434\u0435\u044f\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043b\u0443\u0447\u0448\u0435 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0435\u0435.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041c\u044b \u0441\u043e\u0441\u0442\u0430\u0432\u0438\u043b\u0438 5 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u043e\u0432\u0435\u0442\u043e\u0432 \u043f\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u043a\u043e\u0434\u0430 \u0432 Laravel, \u0441\u043b\u0435\u0434\u0443\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043c\u043e\u0436\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u0438 \u0435\u0433\u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e. \u0414\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0432\u0435\u0442\u044b \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u043f\u043e\u0434 \u043b\u044e\u0431\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e Laravel.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[5,3,6,4],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-best-practies","tag-laravel","tag-php","tag-srp"],"_links":{"self":[{"href":"https:\/\/phpha.ru\/index.php?rest_route=\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpha.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpha.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpha.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpha.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":10,"href":"https:\/\/phpha.ru\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":74,"href":"https:\/\/phpha.ru\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions\/74"}],"wp:attachment":[{"href":"https:\/\/phpha.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpha.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpha.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}