用户编辑器,只编辑第一条记录

Posted

技术标签:

【中文标题】用户编辑器,只编辑第一条记录【英文标题】:user editor, only edit the first record 【发布时间】:2021-10-01 01:42:51 【问题描述】:

我正在使用用户编辑器,我有问题,只需编辑第一条记录。

当我想编辑另一个不同的用户时,它只向我发送来自第一个用户的数据 i.imgur.com/nhP8iKz.png

  public function usuarios(Request $request)
    $data = [
        'category_name' => 'apps',
        'page_name' => 'contacts',
        'has_scrollspy' => 0,
        'scrollspy_offset' => '',
    ];
 $usuarios = User::All();

    // $pageName = 'contacts';
    return view('usuarios',compact('usuarios'))->with($data);

public function userupdate(Request $request)
 

 foreach($request->service as $key=>$value)

    $nuevoIngreso = User::find($value['id']);
    if($nuevoIngreso)
    $nuevoIngreso->name = $value['name'];
    $nuevoIngreso->email = $value['email'];
    $nuevoIngreso->telefono = $value['telefono'];

    $nuevoIngreso->tipo = $value['tipo'];
    $nuevoIngreso->save();
    
   

    // $pageName = 'contacts';
    return redirect('usuarios')->with('mensaje', 'Ficha Actualizada.');

Blade.php

    @extends('layouts.app')



@section('content')
<div id="tableCheckbox" class="col-lg-12 col-12 layout-spacing">
                            <div class="statbox widget box box-shadow">
                                <div class="widget-header">
                                    <div class="row">
                                        <div class="col-xl-12 col-md-12 col-sm-12 col-12 mt-3 text-center">
                                            <h2>Usuarios</h2>
                                        </div>                       
                                    </div>
                                    
                                </div>
                                <div class="widget-content widget-content-area">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-hover table-striped table-checkable table-highlight-head mb-4">
                                            <thead>
                                                <tr>
                                                    <th class="checkbox-column">
                                                        <label class="new-control new-checkbox checkbox-primary" style="height: 18px; margin: 0 auto;">
                                                            <input type="checkbox" class="new-control-input todochkbox" id="todoAll">
                                                            <span class="new-control-indicator"></span>
                                                        </label>
                                                    </th>
                                                    <th class="">Nombre</th>
                                                    <th class="">Email</th>
                                                    <th class="">Telefono</th>
                                                    <th class="text-center">ACCIÓNES</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                @foreach($usuarios as $key=>$users)
                                                <tr>
                                                    <td class="checkbox-column">
                                                        <label class="new-control new-checkbox checkbox-primary" style="height: 18px; margin: 0 auto;">
                                                            <input type="checkbox" class="new-control-input todochkbox" id="todo-1">
                                                            <span class="new-control-indicator"></span>
                                                        </label>
                                                    </td>
                                                    <td>
                                                        <p class="mb-0">$users->name</p>
                                                    </td>
                                                    <td>$users->email</td>
                                                    <td>$users->telefono</td>

                                                    <td class="text-center">
                                                        <ul class="table-controls">
                                                            <li><a href="route('userupdate', $users)"  data-placement="top" title="" data-original-title="Settings" data-toggle="modal" data-target="#registerModal"><svg xmlns="http://www.w3.org/2000/svg"   viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke- stroke-linecap="round" stroke-linejoin="round" class="feather feather-settings text-primary"><circle cx="12" cy="12" r="3"></circle><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path></svg></a> </li>
                                                            <li><a href="javascript:void(0);" data-toggle="tooltip" data-placement="top" title="" data-original-title="Edit"><svg xmlns="http://www.w3.org/2000/svg"   viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke- stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit-2 text-success"><path d="M17 3a2.828 2.828 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5L17 3z"></path></svg></a></li>
                                                            <li><a href="javascript:void(0);" data-toggle="tooltip" data-placement="top" title="" data-original-title="Delete"><svg xmlns="http://www.w3.org/2000/svg"   viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke- stroke-linecap="round" stroke-linejoin="round" class="feather feather-trash-2 text-danger"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></svg></a></li>
                                                        </ul>
                                                    </td>
                                                </tr>
                                                <div class="modal fade register-modal" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="registerModalLabel" aria-hidden="true">
                                      <div class="modal-dialog" role="document">
                                        <div class="modal-content">

                                          <div class="modal-header" id="registerModalLabel">
                                            <h4 class="modal-title">Editar Usuario</h4>
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg"   viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke- stroke-linecap="round" stroke-linejoin="round" class="feather feather-x"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg></button>
                                          </div>
                                          <div class="modal-body">
                                      
                                            <form id="addContactModalTitle" class="mt-0" method="POST" action=" route('userupdate') ">
                                                    method_field('PUT')
@csrf
<input class="form-control" type="hidden" value="$users->id" name="service[$key][id]">
                                               <div class="form-group">
                                                <svg xmlns="http://www.w3.org/2000/svg"   viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke- stroke-linecap="round" stroke-linejoin="round" class="feather feather-user"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>
                                                <input type="text" class="form-control mb-2" name="service[$key][name]" id="exampleInputUsername1" value="$users->name">
                                              </div>
                                              <div class="form-group">
                                                <svg xmlns="http://www.w3.org/2000/svg"   viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke- stroke-linecap="round" stroke-linejoin="round" class="feather feather-at-sign"><circle cx="12" cy="12" r="4"></circle><path d="M16 8v5a3 3 0 0 0 6 0v-1a10 10 0 1 0-3.92 7.94"></path></svg>
                                                <input type="email" class="form-control mb-2" name="service[$key][email]" id="exampleInputEmail2" value="$users->email">
                                              </div>
                                              <div class="form-group">
                                                <svg xmlns="http://www.w3.org/2000/svg"   viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke- stroke-linecap="round" stroke-linejoin="round" class="feather feather-lock"><rect x="3" y="11"   rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
                                                <input type="phone" class="form-control mb-4" name="service[$key][telefono]" id="exampleInputPassword2" value="$users->telefono">
                                              </div>
                                              <div class="form-group">
                                                <svg xmlns="http://www.w3.org/2000/svg"   viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke- stroke-linecap="round" stroke-linejoin="round" class="feather feather-lock"><rect x="3" y="11"   rx="2" ry="2"></rect><path d="M7 11V7a5 5 0 0 1 10 0v4"></path></svg>
                                                <input type="phone" class="form-control mb-4" name="service[$key][tipo]" id="exampleInputPassword2" value="$users->tipo">
                                              </div>
                                              <button type="submit" class="btn btn-primary mt-2 mb-2 btn-block">Actualizar</button>
                                            </form>

                                           

                                          </div>
                                         
                                        </div>
                                      </div>
                                    </div>
                                             @endforeach
                                            </tbody>
                                        </table>
                                    </div>

                                  

                                </div>
                            </div>
                        </div>


      

   @endsection

路线:

 Route::get('usuarios','HomeController@usuarios');
Route::put('/usuarios', 'HomeController@userupdate' )->name('userupdate');

如有必要,我可以上传问题的屏幕截图以便更好地理解。

当我单击编辑时,用户 ID = 2,或 3 仅向我发送来自用户 ID 1 的数据。在列表中它打印所有记录,但在表单中它只打印第一个用户

【问题讨论】:

您要编辑所有用户吗?还是只有一个用户? i.imgur.com/nhP8iKz.png我想编辑我在列表中选择的用户 【参考方案1】:

您正在获得所有用户。尝试传递用户 ID 并仅获取该用户数据。

Route::get('usuarios/id','HomeController@usuarios');

public function usuarios($id)
 $data = [
    'category_name' => 'apps',
    'page_name' => 'contacts',
    'has_scrollspy' => 0,
    'scrollspy_offset' => '',
 ];

 $usuarios = User::findOrFail($id);

 // $pageName = 'contacts';
 return view('usuarios',compact('usuarios'))->with($data); 

将 id 作为 URL 中的参数传递,并获取特定于该 id 的数据。然后更新该 ID。

【讨论】:

以上是关于用户编辑器,只编辑第一条记录的主要内容,如果未能解决你的问题,请参考以下文章

Python 2.7 fnmatch 不编辑文本

页面刷新不清除ueditor编辑器内容如何设置 这些个财富是我的全部家当,先谢谢啦!

PHP中如何在查询结果的每一条记录后加个编辑和删除按钮,实现编辑和删除操作。

访问子表单,选择下一条记录时停止自动保存到链接表

编辑组合框时访问表单不会更新,必须移动到下一条记录

来自弹出编辑器的Kendo UI绑定下拉值