Laravel 10 无法更新数据

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

Laravel 10 can't update data

问题

我无法运行代码,但可以为您提供代码的翻译和可能的解决方案。

这是您提供的代码的翻译:

// 控制器
public function update(Request $request, string $id): RedirectResponse
{
    //
    $input = $request->validate([
        'id' => 'required',
        'name' => 'required',
        'description' => 'required',
    ]);

    $accounting_group = AccountingGroup::findOrFail($id);
    $accounting_group->update($input);

    return redirect()->route('accounting_group.index')->with(['success' => '数据已保存']);
}
<!-- 路由 -->
Route::resource('accounting_group', AccountingGroupController::class)->except(['show']);
<!-- 模型 -->
class AccountingGroup extends Model
{
    use HasFactory;
    protected $table = 'accounting_groups';
    protected $fillable = [
        'id',
        'name',
        'description',
    ];

    protected $hidden = [];

    public function transactionaccount(): HasMany
    {
        return $this->hasMany(TransactionAccount::class);
    }
}
<form method="post" class="mx-2 p-4" action="{{ route('accounting_group.update', $accounting_group->id) }}">
    @csrf
    @method('put')
    <div class="form-group">
        <label for="id">ID Akun</label>
        <input type="text" class="form-control" id="id" placeholder="ID accounting..." value="{{ old('id', $accounting_group->id) }}">
    </div>
    <!-- ...其他表单字段... -->
    <div class="d-flex justify-content-end">
        <button type="submit" class="btn btn-primary">保存</button>
    </div>
</form>

根据您提供的代码,您遇到了两个问题:

  1. 您没有看到Laravel中的错误显示。
  2. 数据在MySQL中没有更新。

对于问题1,确保在Laravel的配置文件中启用了错误显示,通常在 .env 文件中将 APP_DEBUG 设置为 true

对于问题2,您的表单中使用了 @method('put') 来指定使用PUT请求进行更新。请确保您的路由支持PUT请求,并且在路由定义中使用了update操作。另外,也要确保您的数据库连接正确,并且在更新之后没有其他操作导致数据被还原或覆盖。

如果问题仍然存在,您可能需要查看Laravel的日志文件以获取更多详细信息,以便进一步调试。

英文:

i can't update table data in laravel 10 with controller resource

my code:

// controller
    public function update(Request $request, string $id):RedirectResponse
    {
        //
        $input = $request-&gt;validate([
            &#39;id&#39;=&gt;&#39;required&#39;,
            &#39;name&#39;=&gt;&#39;required&#39;,
            &#39;description&#39;=&gt;&#39;required&#39;,
        ]);

        $accounting_group = AccountingGroup::findOrFail($id);
        $accounting_group-&gt;update($input);

        return redirect()-&gt;route(&#39;accounting_group.index&#39;)-&gt;with([&#39;success&#39; =&gt; &#39;Data telah disimpan&#39;]);
    }
&lt;!-- route --&gt;
Route::resource(&#39;accounting_group&#39;, AccountingGroupController::class)-&gt;except([&#39;show&#39;]);
&lt;!-- model --&gt;
class AccountingGroup extends Model
{
    use HasFactory;
    protected $table = &#39;accounting_groups&#39;;
    protected $fillable = [
        &#39;id&#39;,
        &#39;name&#39;,
        &#39;description&#39;,
    ];

    protected $hidden = [];

    public function transactionaccount(): HasMany
    {
        return $this-&gt;hasMany(TransactionAccount::class);
    }
}
&lt;form method=&quot;post&quot; class=&quot;mx-2 p-4&quot; action=&quot;{{ route(&#39;accounting_group.update&#39;, $accounting_group-&gt;id) }}&quot;&gt;
               @csrf
               @method(&#39;put&#39;)
          &lt;div class=&quot;form-group&quot;&gt;
               &lt;label for=&quot;id&quot;&gt;ID Akun&lt;/label&gt;
               &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;id&quot; placeholder=&quot;ID accounting...&quot; value=&quot;{{ old(&#39;id&#39;, $accounting_group-&gt;id) }}&quot;&gt;
          &lt;/div&gt;
....
          &lt;div class=&quot;d-flex justify-content-end&quot;&gt;
               &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Simpan&lt;/button&gt;
          &lt;/div&gt;
          &lt;/form&gt;

i didn't get eror display in laravel, i got refresh page where i clicked sumbit button and in mysql the data get no update

答案1

得分: 1

为了允许浏览器发送输入,您需要在您的输入字段中添加name属性。

所以您只需要将name=&quot;id&quot;添加到您的代码中:

<input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;id&quot; name=&quot;id&quot; placeholder=&quot;ID accounting...&quot; value=&quot;{{ old(&#39;id&#39;, $accounting_group-&gt;id) }}&quot;>

并且不要忘记为其他字段添加name属性,如name=&quot;name&quot;name=&quot;description&quot;

英文:

To allow browser send input, you need to add you name attribute in your input for.

So you just need to add name=&quot;id&quot; to your:

&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;id&quot; placeholder=&quot;ID accounting...&quot; value=&quot;{{ old(&#39;id&#39;, $accounting_group-&gt;id) }}&quot;&gt;

so your code should be:

&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;id&quot; name=&quot;id&quot; placeholder=&quot;ID accounting...&quot; value=&quot;{{ old(&#39;id&#39;, $accounting_group-&gt;id) }}&quot;&gt;

and don't forgot to add name attribute to other fields like name=&quot;name&quot; and name=&quot;description&quot;

答案2

得分: 1

当您使用Route::resource并调用showupdate方法时,您将在控制器函数中获得请求的ID,称为属性提升

在这里,我已经增强了您的代码:

public function update(Request $request, AccountingGroup $accounting_group): RedirectResponse
{
    $input = $request->validate([
        'name' => 'required',
        'description' => 'required',
    ]);

    $accounting_group->update($input->validated());

    return redirect()->route('accounting_group.index')->with(['success' => 'Data telah disimpan']);
}

我在参数中添加了AccountingGroup $accounting_group。如果找不到这个ID记录,那么会调用ModelNotFoundException

我添加了**$input->validated()**,这个方法只用于返回经过验证的数据,用于安全目的。

英文:

When you are using Route::resource and call the show or update method then you will get the requested ID in the record in the controller function it is called property promotion

Here I have enhanced your code

public function update(Request $request, AccountingGroup $accounting_group):RedirectResponse
{
    $input = $request-&gt;validate([
        &#39;name&#39;=&gt;&#39;required&#39;,
        &#39;description&#39;=&gt;&#39;required&#39;,
    ]);

    $accounting_group-&gt;update($input-&gt;validated());

    return redirect()-&gt;route(&#39;accounting_group.index&#39;)-&gt;with([&#39;success&#39; =&gt; &#39;Data telah disimpan&#39;]);
}

> I have added the AccountingGroup $accounting_group in the
> argument. If this id record is not found then call the
> ModelNotFoundException

> And I have added the $input->validated() this method is only for
> returning the validated data, It's used for security purposes.

huangapple
  • 本文由 发表于 2023年5月25日 11:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328788.html
匿名

发表评论

匿名网友

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

确定