非法字符串偏移'名称'laravel php

时间:2018-04-18 14:40:12

标签: laravel

这是我的注册页面的视图和控制器。我正在使用laravel框架。我已经检查了这个因为密码字段而给出了错误。我编译它没有密码字段,它工作。我无法理解它中的问题。我从laravel集体表格页面复制了它。

查看:

{!! Form::open(['url' => 'signup/submit']) !!}
        <div class="form-group">
            {{Form::label('name', 'Name')}}
            {{Form::text('name', '', ['class' => 'form-control', 'placeholder' => 'Full Name'])}}
        </div>
        <div class="form-group">
            {{Form::label('username', 'Username')}}
            {{Form::text('username', '', ['class' => 'form-control', 'placeholder' => 'Username'])}}
        </div>
        <div class="form-group">
            {{Form::label('email', 'E-Mail Address')}}
            {{Form::email('email', '', ['class' => 'form-control', 'placeholder' => 'Your email..'])}}
        </div>
        <div class="form-group">
            {{Form::label('password', 'Password')}}
            {{Form::password('password', '123', ['class' => 'form-control', 'placeholder' => 'Password...'])}}
        </div>
        <div>
            {{Form::submit('Submit', ['class' => 'btn btn-primary'])}}
        </div>
    {!! Form::close() !!}

控制器:

public function submit(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'username' => 'username',
            'email' => 'email',
            'password' => 'password'
        ]);

        //Getting the info and creating new message.
        $message = new Message;
        $message->name = $request->input('name');
        $message->username = $request->input('username');
        $message->message = $request->input('email');
        $message->message = $request->input('password');

        $message->save();

        //redirecting
        return redirect('/')->with('success', 'Registeration successful');
    }

如果有人能理解这个问题吗?

这是错误屏幕截图: Error screenshot

2 个答案:

答案 0 :(得分:2)

好的,我发现了,这是因为密码字段没有显示值。

更改此

{{Form::password('password', '123', ['class' => 'form-control', 'placeholder' => 'Password...'])}}

到这个

{{Form::password('password', ['class' => 'form-control', 'placeholder' => 'Password...'])}}

来自https://laravel.io/forum/08-12-2014-illegal-string-offset-name-when-validating-against-unique

答案 1 :(得分:0)

您在此处保存两次 - &gt;此处消息

 $message->message = $request->input('email');
 $message->message = $request->input('password');

因此您需要更改保存密码输入的字段,如:

 $message->message = $request->input('email');
 $message->password= $request->input('password');