首页 > 解决方案 > LaravelCollective 没有收到数据

问题描述

我从 Laravel 5.5 开始。尝试将数据发送到编辑表单时出现错误:

`非法字符串偏移'name'(查看:C:\xampp\htdocs\laravel-ads\resources\views\admin\edit.blade.php)

 public function edit($id)
    {
        //
        $user = User::findOrFail($id);
        //var_dump($user['name']);
       return view('admin.edit', compact('user'));
    }
    <div class="container">
    <h1 class="text center">Editor de Usuario</h1>

    {!! Form::model($user, ['method' => 'PATCH', 'action' => ['AdminController@update', $user->id], 'files' => true]) !!}
        
    <div class="form-group">
        {!! Form::label('id', 'Id') !!}
        {!! Form::number('id', '', ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('id_rol', 'Rol') !!}
        {!! Form::number('id_rol', '', ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('imagen', 'Imagen') !!}
        {!! Form::file('imagen', '', ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('name', 'Nombre') !!}
        {!! Form::text('name', '', ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('email', 'Email') !!}
        {!! Form::email('email', '', ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
        {!! Form::label('password', 'Password') !!}
        {!! Form::password('password', '', ['class' => 'form-control']) !!}
    </div>


            {!! Form::submit('Editar', ['class' => 'btn btn-success']) !!}

    {!! Form::close() !!}
   </div>

路线:

Route::resource('admin', 'AdminController');

我的 index.blade.php:

  <div class="container-fluid"></div>
        <h1 class="text-center"> Usuarios</h1>
        <div class="table-responsive">
            <table class="table table-dark">
                <thead>
                    <tr>
                        <th scope="col">#</th>
                        <th scope="col">Rol</th>
                        <th scope="col">Foto</th>
                        <th scope="col">Nombre</th>
                        <th scope="col">Email</th>
                        <th scope="col">Ultima actualizacion</th>
                        <th scope="col">Creado</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($users as $user)
                    <tr>
                        <th scope="row">{{$user->id}}</th>
                        <td>{{$user->id_rol}}</td>
                        <td><img src="images/{{$user->imagen}}" alt="imagen de usuario" width="150"></td>
                        <td><a href="{{route('admin.edit',$user->id)}}"> {{$user->name}}</a></td>
                        <td>{{$user->email}}</td>
                        <td>{{$user->updated_at}}</td>
                        <td>{{$user->created_at}}</td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

问题跳到了 Laravel 的这个地方:

 * @param  string $value
     * @param  array  $options
     *
     * @return \Illuminate\Support\HtmlString
     */
    public function input($type, $name, $value = null, $options = [])
    {
        $this->type = $type;
 
        if (! isset($options['name'])) {
            $options['name'] = $name;
        }
 
        // We will get the appropriate value for the given field. We will look for the
        // value in the session for the value in the old input data then we'll look
        // in the model instance if one is set. Otherwise we will just use empty.
        $id = $this->getIdAttribute($name, $options);
 
        if (! in_array($type, $this->skipValueTypes)) {
            $value = $this->getValueAttribute($name, $value);
        }
 
        // Once we have the type, value, and ID we can merge them into the rest of the
        // attributes array so we can convert them into their HTML attribute format
        // when creating the HTML element. Then, we will return the entire input.
        $merge = compact('type', 'value', 'id'); 

这个答案对我不起作用

标签: laravel-5laravelcollective

解决方案


我已经解决了这个问题,但没有发现它的原因。我在这里留下了有效的表格,看看是否有任何用户可以告诉我错误在哪里。

我不知道出了什么问题。

作为一个奇怪的事实,甚至以前的表格和所有被评论的东西都继续给出问题。

   <form method="POST" action="{{route('admin.update',$user->id)}}" enctype="multipart/form-data">
            @csrf
            @method('PUT')

            <div class="form-group row">
                <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

                <div class="col-md-6">
                    <input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ $user->name }}" autocomplete="name" autofocus>

                    @error('name')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                    @enderror
                </div>
            </div>

            <div class="form-group row">
                <label for="id_rol" class="col-md-4 col-form-label text-md-right">{{ __('Rol') }}</label>

                <div class="col-md-6">
                    <input id="id_rol" type="number" class="form-control @error('id_rol') is-invalid @enderror" name="id_rol" value="{{ $user->id_rol }}" autocomplete="id_rol" autofocus>

                    @error('id_rol')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                    @enderror
                </div>
            </div>

            <div class="form-group row">
                <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                <div class="col-md-6">
                    <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ $user->email }}" autocomplete="email">

                    @error('email')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                    @enderror
                </div>
            </div>

            <div class="form-group row">
                <img class="col-md-4 col-form-label text-md-right" src="../../../public/images/{{$user->imagen}}" alt="imagen de usuario" width="150">

                <div class="col-md-6">
                    <input id="imagen" type="file" class="form-control @error('imagen') is-invalid @enderror" name="imagen" value="{{ $user->imagen }}" autocomplete="imagen">

                    @error('imagen')
                    <span class="invalid-feedback" role="alert">
                        <strong>{{ $message }}</strong>
                    </span>
                    @enderror
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary">
                        {{ __('Guardar') }}
                    </button>
                </div>
            </div>
        </form>


推荐阅读