沃梦达 / 编程问答 / php问题 / 正文

非法字符串偏移'name' laravel

Illegal string offset #39;name#39; laravel(非法字符串偏移name laravel)

本文介绍了非法字符串偏移'name' laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想为我的表编辑一个值时,我得到了一个非法的字符串偏移量,但是当我尝试添加一个新值时,它可以添加一个新值.如果我注释掉表单的 form::file 部分,它可以返回 view 所以我猜问题出在那里.

<div class="portlet light 边框"><div class="portlet-body"><form action="{{ $formIs === 'add' ? route('campus-media.save', $campusId) : route('campus-media.update', [$campusId, $campusMedia->id]) }}" method="post" class="form-horizo​​ntal" enctype="multipart/form-data">{{ csrf_field() }}<div class="form-b​​ody"><div class="form-group"><label class="control-label col-md-3">Title</label><div class="col-md-4">{!!Form::text('title', $campusMedia->title, array('class' => 'form-control', 'placeholder' => 'Campus Media Title')) !!}

<div class="form-b​​ody"><div class="form-group"><label class="control-label col-md-3">Media</label><div class="col-md-4">{!!Form::file('media', $campusMedia->media, array('class' =>'form-control')) !!}

<div class="form-b​​ody"><div class="form-group"><label class="control-label col-md-3">媒体类型</label><div class="col-md-4">{!!Form::select('media_type', $fileType, $campusMedia->media_type,array('placeholder' => 'Choose media type', 'class' => 'form-control')) !!}<span class="help-block">0:图片,1:视频,2:标志</span>

<div class="form-b​​ody"><div class="form-group"><label class="control-label col-md-3">描述</label><div class="col-md-4">{!!Form::textarea('description', $campusMedia->description, array('class' => 'form-control', 'placeholder' => 'Campus Media Description', 'rows' => 3)) !!}

<div class="form-b​​ody"><div class="form-group"><label class="control-label col-md-3">状态</label><div class="col-md-4">{!!Form::select('status', $status, $campusMedia->status,array('placeholder' => 'Choose status', 'class' => 'form-control')) !!}<span class="help-block">0:不活动,1:活动</span>

<div class="form-b​​ody"><div class="form-group"><label class="control-label col-md-3">标签</label><div class="col-md-4">{!!Form::text('tags', $campusMedia->tags, array('class' => 'form-control', 'placeholder' => 'Campus Media Tags')) !!}

<div class="form-actions"><div class="row"><div class="col-md-offset-3 col-md-9"><input type="submit" name="save" class="btn btn-primary" value="Save"/><a href="{{ route('campus-media.index', $campusId) }}" class="btn btn-default">取消

</表单>

这是我的控制器

class CampusMediaController 扩展控制器{//公共函数索引($campusId){$campusMedia = CampusMedia::ofCampus($campusId)->get();$数据 = ['校园媒体' =>$校园媒体,'校园ID' =>$campusId];return view('campus-media.index', $data);}公共函数添加($campusId){$campusMedia = 新的 CampusMedia;$数据 = ['校园媒体' =>$校园媒体,'校园ID' =>$campusId,'文件类型' =>CampusMedia::getMediaType(),'状态' =>CampusMedia::getStatus(),'formIs' =>'添加'];return view('campus-media.form', $data);}公共功能保存(请求 $request, $campusId){$this->validation($request);$post = $request->all();$fileName = $this->upload($request, $campusId);$campusMedia = $this->bindToObject($post, $fileName, $campusId);$campusMedia->save();return redirect()->route('campus-media.index', $campusId)->with('message','Item Succesfully added');}公共功能编辑($campusId,$id){$campusMedia = CampusMedia::find($id);$数据 = ['校园媒体' =>$校园媒体,'校园ID' =>$campusId,'文件类型' =>CampusMedia::getMediaType(),'状态' =>CampusMedia::getStatus(),'formIs' =>'编辑'];return view('campus-media.form', $data);}公共功能更新(请求 $request,$campusId,$id){$this->validation($request);$post = $request->all();$fileName = $this->upload($request, $campusId);$campusMedia = $this->bindToObject($post, $fileName,$campusId,$id);$campusMedia->save();return redirect()->route('campus-media.index', $campusId)->with('message','项目更新成功');}公共函数上传($request, $campusId){$this->validate($request, ['媒体' =>'哑剧:png,jpeg,mp4,avi,mpeg,flv,m4v']);if($request->input('media_type') == 0){$type = "图片";}elseif($request->input('media_type') == 1){$type = "视频";}别的{$type = "标志";}$file = $request->file('media');如果(is_null($文件))返回空;$ext = $file->getClientOriginalExtension();$fileName = sprintf('%s-%s-%s.%s',$campusId,$类型,时间(),$ext);$upload = config('app.upload_path').'/campus/'.$campusId;$file->move($upload,$fileName);返回 $ 文件名;}公共函数删除($campusId,$id){$campusMedia = CampusMedia::find($id);$campusMedia->delete();return redirect()->route('campus-media.index', $campusId)->with('message','项目删除成功');}私有函数验证($request){$this->validate($request, ['媒体' =>'必需的']);}私有函数 bindToObject($post, $fileName,$campusId,$id=null){if(is_null($id)){$campusMedia = 新的 CampusMedia;} 别的 {$campusMedia = CampusMedia::find($id);}$campusMedia->campus_id = $campusId;$campusMedia->title = $post['title'];$campusMedia->media = $fileName;$campusMedia->media_type = $post['media_type'];$campusMedia->description = $post['description'];$campusMedia->status = $post['status'];$campusMedia->tags = $post['tags'];返回 $campusMedia;}

}

我希望有人能帮我解决这个问题,我对上传文件很陌生.

解决方案

您将一个对象作为第二个参数传递给 Form::file,它需要是一个数组.

试试这个:

<代码>{!!Form::file('media', array('class' =>'form-control')) !!}

I'm getting a illegal string offset when I want to edit a value for my table, but it can add a new value when I try to add a new one. If I comment out the form::file part of my form, it can return the view so i'm guessing the problem is somewhere over there.

<div class="col-md-12">
            <div class="portlet light bordered">
                <div class="portlet-body">
                    <form action="{{ $formIs === 'add' ? route('campus-media.save', $campusId) : route('campus-media.update', [$campusId, $campusMedia->id]) }}" method="post" class="form-horizontal" enctype="multipart/form-data">
                    {{ csrf_field() }}
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Title</label>
                                <div class="col-md-4">
                                    {!! Form::text('title', $campusMedia->title, array('class' => 'form-control', 'placeholder' => 'Campus Media Title')) !!}                                        
                                </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Media</label>
                                <div class="col-md-4">
                                    {!! Form::file('media', $campusMedia->media, array('class' => 'form-control')) !!}                                
                                </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Media type</label>
                                    <div class="col-md-4">
                                        {!! Form::select('media_type', $fileType, $campusMedia->media_type,array('placeholder' => 'Choose media type', 'class' => 'form-control')) !!}
                                        <span class="help-block">0: Image, 1: Video, 2: Logo</span>
                                    </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Description</label>
                                <div class="col-md-4">
                                    {!! Form::textarea('description', $campusMedia->description, array('class' => 'form-control', 'placeholder' => 'Campus Media Description', 'rows' => 3)) !!}              
                                </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Status</label>
                                    <div class="col-md-4">
                                        {!! Form::select('status', $status, $campusMedia->status,array('placeholder' => 'Choose status', 'class' => 'form-control')) !!}
                                        <span class="help-block">0: Inactive, 1: Active</span>
                                    </div>
                            </div>
                        </div>
                        <div class="form-body">
                            <div class="form-group">
                                <label class="control-label col-md-3">Tags</label>
                                <div class="col-md-4">
                                    {!! Form::text('tags', $campusMedia->tags, array('class' => 'form-control', 'placeholder' => 'Campus Media Tags')) !!}                                        
                                </div>
                            </div>
                        </div>
                        <div class="form-actions">
                            <div class="row">
                                <div class="col-md-offset-3 col-md-9">
                                    <input type="submit" name="save" class="btn btn-primary" value="Save"/>
                                    <a href="{{ route('campus-media.index', $campusId) }}" class="btn btn-default"> Cancel </a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

And here is my controller

class CampusMediaController extends Controller
{
//
public function index($campusId)
{
    $campusMedia = CampusMedia::ofCampus($campusId)->get();
    $data = [
        'campusMedia'   => $campusMedia,
        'campusId'      => $campusId
    ];

    return view('campus-media.index', $data);
}

public function add($campusId)
{
    $campusMedia = new CampusMedia;
    $data = [
        'campusMedia' => $campusMedia,
        'campusId'    => $campusId,
        'fileType'    => CampusMedia::getMediaType(),
        'status'      => CampusMedia::getStatus(),
        'formIs'      => 'add'
    ];

    return view('campus-media.form', $data);
}

public function save(Request $request, $campusId)
{
    $this->validation($request);
    $post = $request->all();
    $fileName = $this->upload($request, $campusId);
    $campusMedia = $this->bindToObject($post, $fileName, $campusId);
    $campusMedia->save();

    return redirect()->route('campus-media.index', $campusId)->with('message','Item Succesfully Added');
}

public function edit($campusId, $id)
{
    $campusMedia = CampusMedia::find($id);

    $data = [
        'campusMedia'             => $campusMedia,
        'campusId'                => $campusId,
        'fileType'                => CampusMedia::getMediaType(),
        'status'                  => CampusMedia::getStatus(),
        'formIs'                  => 'edit'
    ];

    return view('campus-media.form', $data);
}

public function update(Request $request,$campusId,$id)
{
    $this->validation($request);
    $post = $request->all();
    $fileName = $this->upload($request, $campusId);
    $campusMedia = $this->bindToObject($post, $fileName,$campusId,$id);
    $campusMedia->save();

    return redirect()->route('campus-media.index', $campusId)->with('message','Item updated Succesfully');
}

public function upload($request, $campusId)
{
    $this->validate($request, [
            'media' => 'mimes:png,jpeg,mp4,avi,mpeg,flv,m4v'
        ]
    );

    if($request->input('media_type') == 0){
        $type = "image";
    }elseif($request->input('media_type') == 1){
            $type = "video";
    }else{
        $type = "logo";
    }

    $file   = $request->file('media');
    if(is_null($file))
        return null;

    $ext = $file->getClientOriginalExtension();


    $fileName    = sprintf('%s-%s-%s.%s',
        $campusId,
        $type,
        time(),
        $ext
    );

    $upload      = config('app.upload_path').'/campus/'.$campusId;
    $file->move($upload,$fileName);

    return $fileName;
}

public function delete($campusId,$id)
{
    $campusMedia = CampusMedia::find($id);
    $campusMedia->delete();

    return redirect()->route('campus-media.index', $campusId)->with('message','Item deleted Succesfully');
}

private function validation($request){
    $this->validate($request, [
            'media' => 'required'
        ]
    );
}

private function bindToObject($post, $fileName,$campusId,$id=null)
{
    if(is_null($id)){
        $campusMedia = new CampusMedia;
    } else {
        $campusMedia = CampusMedia::find($id);
    }

    $campusMedia->campus_id = $campusId;
    $campusMedia->title = $post['title'];
    $campusMedia->media = $fileName;
    $campusMedia->media_type = $post['media_type'];
    $campusMedia->description = $post['description'];
    $campusMedia->status = $post['status'];
    $campusMedia->tags = $post['tags'];

    return $campusMedia;
}

}

I was hoping someone could help me with this, i rather new in upload files.

解决方案

You are passing an object as second parameter to Form::file and it need be an array.

Try this:

{!! Form::file('media', array('class' => 'form-control')) !!} 

这篇关于非法字符串偏移'name' laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:非法字符串偏移'name' laravel