简体   繁体   English

如何修复错误:Laravel中数组到字符串的转换

[英]How to fix error : Array to string conversion in laravel

I have a radio button like this 我有一个单选按钮


    <div class="card border-grey border-lighten-3 px-2 py-2 box-shadow-1 mt-1">
        <h4 class="content-header-title">Lesson</h4>
            @foreach($lesson as $key=>$less)
                <div class="form-check">
                    <input type="radio" name="lesson[]" value="{{$key}}"> <label>{{$less}}</label>
                </div>
            @endforeach
    </div>

I have function to save value from radio button, but when i click save it appears error like this 我具有从单选按钮保存值的功能,但是当我单击“保存”时,会出现这样的错误

Array to string conversion 数组到字符串的转换

Bellow is function to save 波纹管具有保存功能


    function saveLess(Request $req){
            $post = new Post;
            $post->title        = $req->title;
            $post->content      = $req->content;
            $post->file_video   = $req->video;
            $post->tags         = $req->tags;
            $post->lesson       = $req->input('lesson');
            $post->save(); 
        }

You are passing the radio button name as lesson[] which refers as array() . 您正在将单选按钮名称作为lesson[]传递,它表示为array() You have to set the name as lession . 您必须将名称设置为lession Or if you want to pass multiple variables in radio button , Then use any of the following method . 或者,如果要在单选按钮中传递多个变量,则使用以下任何一种方法。

implode() or serialize or json_encode . implode()serializejson_encode

If you want to pass only one variable , set , 如果您只想传递一个变量,请设置,

<input type="radio" name="lesson" value="{{$key}}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM