{"id":481,"date":"2021-12-21T22:21:00","date_gmt":"2021-12-21T22:21:00","guid":{"rendered":"https:\/\/rishikantsri.in\/blog\/?p=481"},"modified":"2023-10-25T10:11:07","modified_gmt":"2023-10-25T10:11:07","slug":"481-2","status":"publish","type":"post","link":"https:\/\/rishikantsri.in\/blog\/481-2\/","title":{"rendered":"Code Revie : image"},"content":{"rendered":"\n<p>Code, to be reviewed ,is <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.79999542236328px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"if ($request-&gt;file('image')) {\n            $data = CustomerDetails::where('user_id',$id)-&gt;first();\n\n            $file = $request-&gt;file('image');\n            @unlink(public_path('upload\/customer_images\/' . $data-&gt;image));\n            $filename = date('YmdHi') . $file-&gt;getClientOriginalName();\n            $file-&gt;move(public_path('upload\/customer_images'), $filename);\n            $data['image'] = $filename;\n            $data-&gt;save();\n        }\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> ($request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">file<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;image&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            $data <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">CustomerDetails<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">where<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;user_id&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\">$id<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">first<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            $file <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">file<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;image&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            <\/span><span style=\"color: #F97583\">@<\/span><span style=\"color: #B392F0\">unlink<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">public_path<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;upload\/customer_images\/&#39;<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">.<\/span><span style=\"color: #B392F0\"> $data<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">image<\/span><span style=\"color: #BBBBBB\">))<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            $filename <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> date<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;YmdHi&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">.<\/span><span style=\"color: #B392F0\"> $file<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">getClientOriginalName<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            $file<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">move<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">public_path<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;upload\/customer_images&#39;<\/span><span style=\"color: #BBBBBB\">),<\/span><span style=\"color: #B392F0\"> $filename<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            $data[<\/span><span style=\"color: #FFAB70\">&#39;image&#39;<\/span><span style=\"color: #B392F0\">] <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $filename;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">            $data<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">save<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        }<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>The provided Laravel 8 code appears to handle file uploads for a customer&#8217;s image. Let&#8217;s break down the code step by step:<\/p>\n\n\n\n<ol>\n<li><strong>If Condition:<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.9000244140625px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   if ($request-&gt;file('image')) {\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">   <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> ($request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">file<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;image&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This line checks if there is a file named &#8220;image&#8221; in the incoming HTTP request. It&#8217;s typically used to determine if a file has been uploaded through a form.<\/p>\n\n\n\n<ol start=\"2\">\n<li><strong>Query to Retrieve Customer Details:<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.887451171875px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   $data = CustomerDetails::where('user_id', $id)-&gt;first();\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">   $data <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">CustomerDetails<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">where<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;user_id&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $id<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">first<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #B392F0\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This line retrieves customer details from the database. It looks for a record in the &#8220;CustomerDetails&#8221; table where the &#8216;user_id&#8217; column matches the value of the variable <code>$id<\/code>. It then retrieves the first matching record and stores it in the <code>$data<\/code> variable.<\/p>\n\n\n\n<ol start=\"3\">\n<li><strong>Delete Existing Image:<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.9000244140625px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   @unlink(public_path('upload\/customer_images\/' . $data-&gt;image));\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">   <\/span><span style=\"color: #F97583\">@<\/span><span style=\"color: #B392F0\">unlink<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">public_path<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;upload\/customer_images\/&#39;<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">.<\/span><span style=\"color: #B392F0\"> $data<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">image<\/span><span style=\"color: #BBBBBB\">))<\/span><span style=\"color: #B392F0\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This line deletes the existing image associated with the customer. It uses the <code>unlink<\/code> function to delete the file from the server&#8217;s file system. The file&#8217;s path is constructed using the <code>public_path<\/code> helper function and the existing image&#8217;s filename, which is stored in the <code>$data<\/code> variable under the &#8216;image_path&#8217; field.<\/p>\n\n\n\n<ol start=\"4\">\n<li><strong>Generate New Filename:<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.88751220703125px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   $filename = date('YmdHi') . $file-&gt;getClientOriginalName();\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">   $filename <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> date<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;YmdHi&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">.<\/span><span style=\"color: #B392F0\"> $file<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">getClientOriginalName<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #B392F0\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Here, a new filename is generated for the uploaded image. It combines the current date and time in a specific format with the original name of the uploaded file. This is done to ensure that each uploaded image has a unique filename.<\/p>\n\n\n\n<ol start=\"5\">\n<li><strong>Move and Store the Uploaded File:<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.88751220703125px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   $file-&gt;move(public_path('upload\/customer_images'), $filename);\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">   $file<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">move<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">public_path<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;upload\/customer_images&#39;<\/span><span style=\"color: #BBBBBB\">),<\/span><span style=\"color: #B392F0\"> $filename<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This line moves and stores the uploaded file. It uses the <code>move<\/code> method to move the uploaded file to a specific directory on the server. The destination directory is determined using the <code>public_path<\/code> helper function, and the filename is set to the value generated in the previous step.<\/p>\n\n\n\n<ol start=\"6\">\n<li><strong>Update the Database with the New Image Path:<\/strong><\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:9.887496948242188px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   $data['image'] = $filename;\n   $data-&gt;save();\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #B392F0\">   $data[<\/span><span style=\"color: #FFAB70\">&#39;image&#39;<\/span><span style=\"color: #B392F0\">] <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $filename;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">   $data<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">save<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #B392F0\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>After storing the file, this code updates the &#8216;image_path&#8217; field in the customer&#8217;s details stored in the <code>$data<\/code> variable with the new filename. It then saves the updated details back to the database.<\/p>\n\n\n\n<p>To handle file uploads in Laravel using best practices, we can follow these steps:<\/p>\n\n\n\n<ol>\n<li><strong>Validation:<\/strong> Start by validating the uploaded file to ensure it meets your requirements in terms of file type, size, and other constraints. Laravel provides a powerful validation system that you can leverage.<\/li>\n\n\n\n<li><strong>Use Storage Disks:<\/strong> Laravel provides a &#8220;Storage&#8221; facade that allows you to work with various filesystems. Instead of saving files directly to the <code>public_path<\/code>, you can use storage disks to handle file storage. This allows you to easily switch between different storage options, such as local disk, Amazon S3, or others, without changing your code.<\/li>\n\n\n\n<li><strong>Generate Unique Filenames:<\/strong> To ensure that uploaded files have unique filenames, you can use Laravel&#8217;s built-in methods. The <code>store<\/code> method of the <code>UploadedFile<\/code> class can be used to automatically generate unique filenames and store the file in the specified storage disk.<\/li>\n<\/ol>\n\n\n\n<p>Here&#8217;s an example of how you can rewrite your code following these best practices:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#b392f0;--cbp-line-number-width:19.800000190734863px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#1f1f1f\"><span style=\"background:#a37ced;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#1f1f1f\">PHP<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"use Illuminate\\Support\\Facades\\Storage;\n\npublic function uploadImage(Request $request, $id)\n{\n    $request-&gt;validate([\n        'image_path' =&gt; 'required|image|mimes:jpeg,png,jpg,gif|max:2048', \/\/ Adjust the validation rules as needed\n    ]);\n\n    $customer = CustomerDetails::where('user_id', $id)-&gt;first();\n\n    if (!$customer) {\n        \/\/ Handle the case where the customer does not exist\n        return redirect()-&gt;back()-&gt;with('error', 'Customer not found.');\n    }\n\n    if ($request-&gt;file('image')) {\n        $uploadedFile = $request-&gt;file('image');\n        $newFileName = $uploadedFile-&gt;hashName(); \/\/ Generates a unique filename\n\n        \/\/ Store the file on the configured disk (e.g., 'public' disk)\n        $storedPath = $uploadedFile-&gt;store('upload\/customer_images', 'public');\n\n        \/\/ Update the customer's image path in the database\n        $customer-&gt;image = $storedPath;\n        $customer-&gt;save();\n\n        return redirect()-&gt;back()-&gt;with('success', 'Image uploaded successfully.');\n    }\n\n    return redirect()-&gt;back()-&gt;with('error', 'No file was uploaded.');\n}\" style=\"color:#b392f0;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki min-dark\" style=\"background-color: #1f1f1f\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">use<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">Illuminate<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Support<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Facades<\/span><span style=\"color: #BBBBBB\">\\<\/span><span style=\"color: #79B8FF\">Storage<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">public<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">function<\/span><span style=\"color: #B392F0\"> uploadImage(<\/span><span style=\"color: #79B8FF\">Request<\/span><span style=\"color: #B392F0\"> $request<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $id)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    $request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">validate<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #B392F0\">[<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #FFAB70\">&#39;image_path&#39;<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #F97583\">=&gt;<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&#39;required|image|mimes:jpeg,png,jpg,gif|max:2048&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #6B737C\">\/\/ Adjust the validation rules as needed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    ]<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    $customer <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #79B8FF\">CustomerDetails<\/span><span style=\"color: #F97583\">::<\/span><span style=\"color: #B392F0\">where<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;user_id&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> $id<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">first<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> (<\/span><span style=\"color: #F97583\">!<\/span><span style=\"color: #B392F0\">$customer) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #6B737C\">\/\/ Handle the case where the customer does not exist<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> redirect<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">back<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">with<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;error&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&#39;Customer not found.&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #B392F0\"> ($request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">file<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;image&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $uploadedFile <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $request<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">file<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;image&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $newFileName <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $uploadedFile<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">hashName<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #B392F0\">; <\/span><span style=\"color: #6B737C\">\/\/ Generates a unique filename<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #6B737C\">\/\/ Store the file on the configured disk (e.g., &#39;public&#39; disk)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $storedPath <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $uploadedFile<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">store<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;upload\/customer_images&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&#39;public&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #6B737C\">\/\/ Update the customer&#39;s image path in the database<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $customer<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">image <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #B392F0\"> $storedPath;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        $customer<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">save<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> redirect<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">back<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">with<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;success&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&#39;Image uploaded successfully.&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">    <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #B392F0\"> redirect<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">back<\/span><span style=\"color: #BBBBBB\">()<\/span><span style=\"color: #F97583\">-&gt;<\/span><span style=\"color: #B392F0\">with<\/span><span style=\"color: #BBBBBB\">(<\/span><span style=\"color: #FFAB70\">&#39;error&#39;<\/span><span style=\"color: #BBBBBB\">,<\/span><span style=\"color: #B392F0\"> <\/span><span style=\"color: #FFAB70\">&#39;No file was uploaded.&#39;<\/span><span style=\"color: #BBBBBB\">)<\/span><span style=\"color: #B392F0\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #B392F0\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this code:<\/p>\n\n\n\n<ul>\n<li>We start by validating the uploaded file using Laravel&#8217;s validation rules.<\/li>\n\n\n\n<li>We retrieve the customer details and check if the customer exists.<\/li>\n\n\n\n<li>We use the <code>hashName<\/code> method to generate a unique filename for the uploaded file.<\/li>\n\n\n\n<li>We use the <code>store<\/code> method to store the file on the &#8216;public&#8217; disk within the &#8216;upload\/customer_images&#8217; directory.<\/li>\n\n\n\n<li>We update the customer&#8217;s image path in the database and return a success message.<\/li>\n<\/ul>\n\n\n\n<p>Please make sure to customize the validation rules, error handling, and storage disk configuration to match your specific application&#8217;s requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code, to be reviewed ,is The provided Laravel 8 code appears to handle file uploads for a customer&#8217;s image. Let&#8217;s break down the code step by step: This line checks if there is a file named &#8220;image&#8221; in the incoming HTTP request. It&#8217;s typically used to determine if a file has been uploaded through a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,59,11],"tags":[60,3,8,4],"_links":{"self":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/481"}],"collection":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/comments?post=481"}],"version-history":[{"count":3,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/481\/revisions"}],"predecessor-version":[{"id":491,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/481\/revisions\/491"}],"wp:attachment":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/media?parent=481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/categories?post=481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/tags?post=481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}