如何使用Laravel更新图像?

huangapple go评论122阅读模式
英文:

How to update an image using Laravel?

问题

Here is the translated content without the code:

我想使用Laravel更新数据库中的图像,但不幸的是,图像没有更新。我该如何解决这个问题?

Controller

public function updateaction(Request $request, $id)
{
    $category = $request->input('select_category');
    $project_name = $request->input('product_name');

    $image = $request->file('select_file');
    $new_name = mt_rand().'.'.$image->getClientOriginalExtension();
    $image->move(public_path('projects'), $new_name);
    $updaterecord = DB::table('projects')->where(['products_id' => $id])->update([
        'project_name' => $project_name, 'category_id' => $category, 'image' => $image
    ]);

    if ($updaterecord) {
        return redirect('view_project');
    }

    return back();
}

View

<form action="{{route('project.update', $project->products_id)}}" method="POST"
      enctype="multipart/form-data">
    @csrf
    <div class="group-form">
        <select class="form-control" name="select_category" required>
            <option value="">Select Category</option>
            @foreach($categories as $category)
                <option value="{{$category->id}}" @if($project->category_id == $category->id) selected @endif>
                {{$category->category_name}}</option>
            @endforeach
        </select>
    </div>
    <br>
    <div class="group-form">
        <input type="text" class="form-control" value="{{$project->project_name}}"
               placeholder="Update Product Name" name="product_name" required>
    </div>
    <br>
    <div class="group-form">
        <input type="file" class="form-control" value="{{asset('/public/projects/'.$projects->image)}}"
               name="select_file">
    </div>
    <br>
    <div class="group-form">
        <input type="submit" class="btn btn-primary form-control" value="UPDATE" name="Update">
    </div>
</form>

Route

Route::post('project_update/{id}/update', 'AdminController@updateaction')
    ->name('project.update');
英文:

I want to update an image in my database using Laravel but unfortunately, the image is not updating. How can I resolve this issue?

Controller

<!-- language: php -->

public function updateaction(Request $request, $id)
{
$category = $request-&gt;input(&#39;select_category&#39;);
$project_name = $request-&gt;input(&#39;product_name&#39;);
$image = $request-&gt;file(&#39;select_file&#39;);
$new_name = mt_rand().&#39;.&#39;.$image-&gt;getClientOriginalExtension();
$image-&gt;move(public_path(&#39;projects&#39;), $new_name);
$updaterecord = DB::table(&#39;projects&#39;)-&gt;where([&#39;products_id&#39; =&gt; $id])-&gt;update([
&#39;project_name&#39; =&gt; $project_name, &#39;category_id&#39; =&gt; $category, &#39;image&#39; =&gt; $image
]);
if ($updaterecord) {
return redirect(&#39;view_project&#39;);
}
return back();
}

View

<!-- language: php -->

&lt;form action=&quot;{{route(&#39;project.update&#39;, $project-&gt;products_id)}}&quot; method=&quot;POST&quot;
enctype=&quot;multipart/form-data&quot;&gt;
@csrf
&lt;div class=&quot;group-form&quot;&gt;
&lt;select class=&quot;form-control&quot; name=&quot;select_category&quot; required&gt;
&lt;option value=&quot;&quot;&gt;Select Category&lt;/option&gt;
@foreach($categories as $category)
&lt;option value=&quot;{{$category-&gt;id}}&quot; @if($project-&gt;category_id) == $category-&gt;id)  @endif&gt;
{{$category-&gt;category_name}}&lt;/option&gt;
@endforeach
&lt;/select&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div class=&quot;group-form&quot;&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; value=&quot;{{$project-&gt;project_name}}&quot;
placeholder=&quot;Update Product Name&quot; name=&quot;product_name&quot; required&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div class=&quot;group-form&quot;&gt;
&lt;input type=&quot;file&quot; class=&quot;form-control&quot; value=&quot;{{asset(&#39;/public/projects/&#39;.$projects-&gt;image)}}&quot;
name=&quot;select_file&quot;&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;div class=&quot;group-form&quot;&gt;
&lt;input type=&quot;submit&quot; class=&quot; btn btn-primary form-control&quot; value=&quot;UPDATE&quot; name=&quot;Update&quot;&gt;
&lt;/div&gt;
&lt;/form&gt;

Route

<!-- language: php -->

Route::post(&#39;project_update/{id}/update&#39;, &#39;AdminController@updateaction&#39;)
-&gt;name(&#39;project.update&#39;);

答案1

得分: 2

尝试将 UploadedFile 对象保存到数据库。我认为你需要保存一个字符串作为文件名,例如,$new_name,而不是 $image。

英文:

You try to save UploadedFile object to database. I think, you need to save a string with file name, for example, $new_name instead of $image.

答案2

得分: 1

你的模型没有更新 'image' 属性,因为你在其中分配了一个对象 ($image) 而不是字符串 ($new_name)。

尝试在 UPDATE() 方法内用 $new_name 替换 $image 变量。

请参考以下代码:

public function updateaction(Request $request, $id)
{     
    $category = $request->input('select_category');
    $project_name = $request->input('product_name'); 

    $image = $request->file('select_file');
    $new_name = rand() . '.' . $image->getClientOriginalExtension();
    $image->move(public_path('projects'), $new_name);            
    $updaterecord = DB::table('projects')->where(['products_id' => $id])
        ->update(['project_name' => $project_name, 'category_id' => $category, 'image' => $new_name]);

    if ($updaterecord) {
        return redirect('view_project');
    }
}
英文:

YOUR MODEL IS NOT UPDATING THE 'image' ATTRIBUTE BECAUSE YOU ASSIGN TO IT AN OBJECT ($image) INSTEAD OF A STRING ($new_name).

TRY TO REPLACE $image VARIABLE INSIDE THE UPDATE() METHOD WITH $new_name.

SEE :

public function updateaction(Request $request,$id)
{     
$category=$request-&gt;input(&#39;select_category&#39;);
$project_name=$request-&gt;input(&#39;product_name&#39;); 
$image = $request-&gt;file(&#39;select_file&#39;);
$new_name = rand() . &#39;.&#39; . $image-&gt;getClientOriginalExtension();
$image-&gt;move(public_path(&#39;projects&#39;), $new_name);            
$updaterecord=DB::table(&#39;projects&#39;)-&gt;where([&#39;products_id&#39;=&gt; $id])
-&gt;update([&#39;project_name&#39;=&gt;$project_name,&#39;category_id&#39;=&gt;$category,&#39;image&#39;=&gt;$new_name]);
if($updaterecord){
return redirect(&#39;view_project&#39;);
}
} 

huangapple
  • 本文由 发表于 2020年1月7日 00:48:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定