我想使用Laravel更新记录。

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

I want to update record using laravel

问题

我尝试更新记录使用Laravel,我的记录成功更新了,但不是特定ID的记录在我点击更新按钮时更新,我该如何修复?

有人有任何想法?

控制器

public function updateaction(Request $request, $id)
{     
    $category = $request->input('select_category');
    $project_name = $request->input('product_name');                  
    $updaterecord = DB::table('projects')-
        >where('products_id', $id)-
        >update(['project_name' => $project_name, 'category_id' => $category]);                
    if ($updaterecord) {
        return redirect('view_projects');
    }
}

HTML视图

<form action="{{ route('project.update', $project->products_id) }}" method="POST">
    @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="submit" class="btn btn-primary form-control" value="UPDATE" name="Update">  
    </div>
</form>

路由

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

请使用以上修改后的代码。

英文:

I am trying to update record using laravel my record is updating successfully but not record updating of specific id when i click update button all record is updating how can i fix it ?

Does Anyone have any idea ?

controller

             public function updateaction(Request $request,$id)
             {     
             $category=$request-&gt;input(&#39;select_category&#39;);
             $project_name=$request-&gt;input(&#39;product_name&#39;);                  
             $updaterecord=DB::table(&#39;projects&#39;)- 
             &gt;update([&#39;project_name&#39;=&gt;$project_name,&#39;category_id&#39;=&gt;$category]);                
             if($updaterecord)
             {
             return redirect(&#39;view_projects&#39;);
             }
             }

html view

            &lt;form action=&quot;{{route(&#39;project.update&#39;, $project-&gt;products_id)}}&quot; method=&quot;POST&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;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

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

答案1

得分: 1

**控制器**
你缺少 `->where(['id' => $id]);`
```php
public function updateaction(Request $request, $id)
{     
    $category = $request->input('select_category');
    $project_name = $request->input('product_name');                  
    $updaterecord = DB::table('projects')
    ->where(['id' => $id])->update(['project_name' => $project_name, 'category_id' => $category]);
                   
    if ($updaterecord) {
        return redirect('view_projects');
    }
}

在视图中

如果您使用 Resource Route,请添加 @method('put')

<form action="{{ route('project.update', $project->products_id) }}" method="POST">
   @csrf
   @method('put') 

<details>
<summary>英文:</summary>

**controller**
you are missing `-&gt;where([&#39;id&#39;=&gt; $id]);`
```php
public function updateaction(Request $request,$id)
    {     
        $category=$request-&gt;input(&#39;select_category&#39;);
        $project_name=$request-&gt;input(&#39;product_name&#39;);                  
        $updaterecord=DB::table(&#39;projects&#39;)
        -&gt;where([&#39;id&#39;=&gt; $id])-&gt;update([&#39;project_name&#39;=&gt;$project_name,&#39;category_id&#39;=&gt;$category]);
                       
        if($updaterecord){
        return redirect(&#39;view_projects&#39;);
        }
    }

in View

add @method(&#39;put&#39;) if you are using Resource Route

&lt;form action=&quot;{{route(&#39;project.update&#39;, $project-&gt;products_id)}}&quot; method=&quot;POST&quot;&gt;
   @csrf
   @method(&#39;put&#39;) 

答案2

得分: 0

你需要更改特定元素,因此使用该ID查找并更新该特定元素。

$project = Project::find($id);
$project->update(['project_name' => $project_name, 'category_id' => $category]);
英文:

You need to change the specific element so find with that id & update that specific element.

$project=Project::find($id);
$project-&gt;update([&#39;project_name&#39;=&gt;$project_name,&#39;category_id&#39;=&gt;$category]);

huangapple
  • 本文由 发表于 2020年1月6日 19:07:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610992.html
匿名

发表评论

匿名网友

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

确定