【问题标题】:How to fetch and update data in Laravel如何在 Laravel 中获取和更新数据
【发布时间】:2021-10-07 15:15:42
【问题描述】:

所以,我有两个字段。在第一个字段中我想添加旧数据,在第二个字段中我想添加新数据,在这些字段下方是一个按钮,单击该按钮将从 Mongo Table 中获取数据并更新其中的数据。 我已经尝试实现各种逻辑,但仍然没有运气。任何形式的帮助将不胜感激。

这是我的控制器代码:

public function updatedockets($dockets){
    if (!Session::has('user_id')){
        return Redirect::to('/login');
    }
    $cart = \App\Models\Cart::where('tracking_no',$dockets)->get()->toArray();
    foreach ($cart as $value) {
        $cart = \App\Models\Cart::where('tracking_no')->update(['tracking_no' => $dockets]);
     }   
 }

这是我的视图代码:

<div role="tabpanel" class="tab-pane" id="docket-replace">
                    <form method="post" id="frmDocket" action="/admin/viewdockets">
                    {!! csrf_field() !!}
                    <div>
                        <div class="form-group">
                            <textarea id="txtreploaceDockets" name="replacedockets" class="form-control" rows="15" placeholder="Enter old docket(s) here. Don't put more than 1000 docket numbers, all dockets should be comma(,) or new line separated"></textarea>
                        </div>
                    </div>
                    </form> 

                    <form method="post" id="frmDocket" action="/admin/viewdockets">
                    {!! csrf_field() !!}
                    <div>
                        <div class="form-group">
                            <textarea id="txtreplaceDockets" name="replacedockets" class="form-control" rows="15" placeholder="Enter new docket(s) here. Don't put more than 1000 docket numbers, all dockets should be comma(,) or new line separated"></textarea>
                        </div>
                        <div class="form-group">
                            <div class="text-right pr-sm-15 pb-sm-15">
                                <button type="button" class="btn btn-primary btn-xs" id="btnReplaceDocketData"  onclick="return confirm('Are you sure that you have entered the right docket number?')"> Replace Docket </button>
                            </div>
                        </div>
                    </div>
                    </form>

                </div>

【问题讨论】:

  • 试试这个 $cart = \App\Models\Cart::where('tracking_no',$dockets)->update(['tracking_no' => $dockets]);
  • 你的控制器代码中有一些奇怪的东西,添加到@JohnLobo,你获取'tracking_no' = $dockets的购物车,然后你用'tracking_no' = $dockets更新购物车,另外你只保存你取到的最后一个购物车!
  • 我要做的是,从表中获取数据,然后更新相同 id 中的数据。例如,如果 a=1 并且我想将其更改为 5 然后 a=5,这就是我要实现的,我知道代码看起来很乱,现在让我很头疼。

标签: php laravel backend laravel-blade laravel-8


【解决方案1】:

由于您没有使用$value,也许下面的代码可以帮助您:

$cart = \App\Models\Cart::where('tracking_no', $dockets)
    ->update(['tracking_no' => $dockets]);

【讨论】:

    【解决方案2】:

    示例:

    $course->statuses()->updateOrCreate(
        ['status_id' => $id],
        ['status' => $request->get('status'), 'status_type' => Course::class]
    );`
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2017-10-30
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2017-03-15
      相关资源
      最近更新 更多